Skip to content

Commit

Permalink
[Truffle] Completed String#getbyte and String#bytes.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Mar 31, 2015
1 parent e358cd7 commit 53d395f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
1 change: 0 additions & 1 deletion spec/truffle/tags/core/string/bytes_tags.txt

This file was deleted.

6 changes: 0 additions & 6 deletions spec/truffle/tags/core/string/getbyte_tags.txt

This file was deleted.

Expand Up @@ -1187,6 +1187,9 @@ public RubyString forceEncoding(VirtualFrame frame, RubyString string, Object en
@CoreMethod(names = "getbyte", required = 1)
public abstract static class GetByteNode extends CoreMethodNode {

private final ConditionProfile negativeIndexProfile = ConditionProfile.createBinaryProfile();
private final ConditionProfile indexOutOfBoundsProfile = ConditionProfile.createBinaryProfile();

public GetByteNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
Expand All @@ -1196,8 +1199,18 @@ public GetByteNode(GetByteNode prev) {
}

@Specialization
public int getByte(RubyString string, int index) {
return string.getBytes().get(index);
public Object getByte(RubyString string, int index) {
final ByteList bytes = string.getByteList();

if (negativeIndexProfile.profile(index < 0)) {
index += bytes.getRealSize();
}

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

return string.getBytes().get(index) & 0xff;
}
}

Expand Down

0 comments on commit 53d395f

Please sign in to comment.