Skip to content
Permalink
Browse files
[Truffle] - Implements String#clear
  • Loading branch information
lucasallan committed Dec 22, 2014
1 parent 2090571 commit e9444fff0338590fe7fbc3fcf0d16e2e482b4c0e
Showing with 21 additions and 0 deletions.
  1. +21 −0 core/src/main/java/org/jruby/truffle/nodes/core/StringNodes.java
@@ -1249,4 +1249,25 @@ public RubyString capitalize(RubyString string) {

}


@CoreMethod(names = "clear")
public abstract static class ClearNode extends CoreMethodNode {

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

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

@Specialization
public RubyString clear(RubyString string) {
notDesignedForCompilation();

string.set(ByteList.create(""));
return string;
}
}

}

1 comment on commit e9444ff

@nirvdrum
Copy link
Contributor

@nirvdrum nirvdrum commented on e9444ff Dec 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this loses the encoding of the string. We need to improve this across the board, but figured I'd mention it here:

$ JRUBY_OPTS="-X+T" bin/jruby -e 's = "abc".force_encoding("UTF-8"); p s.encoding.name; s.clear; p s.encoding.name'
"UTF-8"
"ASCII-8BIT"

The encoding of the cleared string should remain UTF-8.

Please sign in to comment.