Skip to content

Commit

Permalink
[Truffle] Implemented String#each_byte.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Mar 2, 2015
1 parent 0623029 commit e0eba28
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
3 changes: 0 additions & 3 deletions spec/truffle/tags/core/string/each_byte_tags.txt
@@ -1,4 +1 @@
fails:String#each_byte passes each byte in self to the given block
fails:String#each_byte keeps iterating from the old position (to new string end) when self changes
fails:String#each_byte returns self
fails:String#each_byte returns an enumerator when no block given
Expand Up @@ -852,6 +852,47 @@ public RubyBasicObject downcase(RubyString string) {
}
}

@CoreMethod(names = "each_byte", needsBlock = true)
public abstract static class EachByteNode extends YieldingCoreMethodNode {

@Child private CallDispatchHeadNode toEnumNode;

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

public EachByteNode(EachByteNode prev) {
super(prev);
}

@Specialization
public Object eachByte(VirtualFrame frame, RubyString string, @SuppressWarnings("unused") UndefinedPlaceholder block) {
notDesignedForCompilation();

if (toEnumNode == null) {
CompilerDirectives.transferToInterpreter();
toEnumNode = insert(DispatchHeadNodeFactory.createMethodCall(getContext()));
}

return toEnumNode.call(frame, string, "to_enum", null, getContext().newSymbol("each_byte"));
}

@Specialization
public RubyString eachByte(VirtualFrame frame, RubyString string, RubyProc block) {
notDesignedForCompilation();

final ByteList bytes = string.getBytes();
final int begin = bytes.getBegin();

for (int i = 0; i < bytes.getRealSize(); i++) {
yield(frame, block, bytes.get(begin + i));
}

return string;
}

}

@CoreMethod(names = "each_char", needsBlock = true)
public abstract static class EachCharNode extends YieldingCoreMethodNode {

Expand Down

0 comments on commit e0eba28

Please sign in to comment.