Skip to content

Commit 0034504

Browse files
committed
[Truffle] Added a new node for getting a byte from a rope.
1 parent b38ba18 commit 0034504

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

truffle/src/main/java/org/jruby/truffle/core/rope/RopeNodes.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,4 +627,30 @@ protected int getCacheLimit() {
627627
}
628628

629629
}
630+
631+
@NodeChildren({
632+
@NodeChild(type = RubyNode.class, value = "rope"),
633+
@NodeChild(type = RubyNode.class, value = "index")
634+
})
635+
public abstract static class GetByteNode extends RubyNode {
636+
637+
public GetByteNode(RubyContext context, SourceSection sourceSection) {
638+
super(context, sourceSection);
639+
}
640+
641+
public abstract int executeGetByte(Rope rope, int index);
642+
643+
@Specialization(guards = "rope.getRawBytes() != null")
644+
public int getByte(Rope rope, int index) {
645+
return rope.getRawBytes()[index] & 0xff;
646+
}
647+
648+
@Specialization(guards = "rope.getRawBytes() == null")
649+
@TruffleBoundary
650+
public int getByteSlow(Rope rope, int index) {
651+
return rope.getBytes()[index] & 0xff;
652+
}
653+
654+
}
655+
630656
}

truffle/src/main/java/org/jruby/truffle/core/string/StringNodes.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,26 +1260,30 @@ public DynamicObject forceEncoding(VirtualFrame frame, DynamicObject string, Obj
12601260
@CoreMethod(names = "getbyte", required = 1, lowerFixnumParameters = 0)
12611261
public abstract static class GetByteNode extends CoreMethodArrayArgumentsNode {
12621262

1263+
@Child private RopeNodes.GetByteNode ropeGetByteNode;
1264+
12631265
public GetByteNode(RubyContext context, SourceSection sourceSection) {
12641266
super(context, sourceSection);
1267+
ropeGetByteNode = RopeNodesFactory.GetByteNodeGen.create(context, sourceSection, null, null);
12651268
}
12661269

12671270
@Specialization
12681271
public Object getByte(DynamicObject string, int index,
12691272
@Cached("createBinaryProfile()") ConditionProfile negativeIndexProfile,
12701273
@Cached("createBinaryProfile()") ConditionProfile indexOutOfBoundsProfile) {
1271-
final byte[] bytes = rope(string).getBytes();
1274+
final Rope rope = rope(string);
12721275

12731276
if (negativeIndexProfile.profile(index < 0)) {
1274-
index += bytes.length;
1277+
index += rope.byteLength();
12751278
}
12761279

1277-
if (indexOutOfBoundsProfile.profile((index < 0) || (index >= bytes.length))) {
1280+
if (indexOutOfBoundsProfile.profile((index < 0) || (index >= rope.byteLength()))) {
12781281
return nil();
12791282
}
12801283

1281-
return bytes[index] & 0xff;
1284+
return ropeGetByteNode.executeGetByte(rope, index);
12821285
}
1286+
12831287
}
12841288

12851289
@CoreMethod(names = "hash")

0 commit comments

Comments
 (0)