Skip to content

Commit

Permalink
Showing 2 changed files with 34 additions and 4 deletions.
26 changes: 26 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/core/rope/RopeNodes.java
Original file line number Diff line number Diff line change
@@ -627,4 +627,30 @@ protected int getCacheLimit() {
}

}

@NodeChildren({
@NodeChild(type = RubyNode.class, value = "rope"),
@NodeChild(type = RubyNode.class, value = "index")
})
public abstract static class GetByteNode extends RubyNode {

public GetByteNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public abstract int executeGetByte(Rope rope, int index);

@Specialization(guards = "rope.getRawBytes() != null")
public int getByte(Rope rope, int index) {
return rope.getRawBytes()[index] & 0xff;
}

@Specialization(guards = "rope.getRawBytes() == null")
@TruffleBoundary
public int getByteSlow(Rope rope, int index) {
return rope.getBytes()[index] & 0xff;
}

}

}
Original file line number Diff line number Diff line change
@@ -1260,26 +1260,30 @@ public DynamicObject forceEncoding(VirtualFrame frame, DynamicObject string, Obj
@CoreMethod(names = "getbyte", required = 1, lowerFixnumParameters = 0)
public abstract static class GetByteNode extends CoreMethodArrayArgumentsNode {

@Child private RopeNodes.GetByteNode ropeGetByteNode;

public GetByteNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
ropeGetByteNode = RopeNodesFactory.GetByteNodeGen.create(context, sourceSection, null, null);
}

@Specialization
public Object getByte(DynamicObject string, int index,
@Cached("createBinaryProfile()") ConditionProfile negativeIndexProfile,
@Cached("createBinaryProfile()") ConditionProfile indexOutOfBoundsProfile) {
final byte[] bytes = rope(string).getBytes();
final Rope rope = rope(string);

if (negativeIndexProfile.profile(index < 0)) {
index += bytes.length;
index += rope.byteLength();
}

if (indexOutOfBoundsProfile.profile((index < 0) || (index >= bytes.length))) {
if (indexOutOfBoundsProfile.profile((index < 0) || (index >= rope.byteLength()))) {
return nil();
}

return bytes[index] & 0xff;
return ropeGetByteNode.executeGetByte(rope, index);
}

}

@CoreMethod(names = "hash")

0 comments on commit 0034504

Please sign in to comment.