Skip to content

Commit

Permalink
[Truffle] Completed String#dump.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Mar 20, 2015
1 parent 82ebb84 commit 9b1c709
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
1 change: 0 additions & 1 deletion spec/truffle/tags/core/string/dump_tags.txt

This file was deleted.

Expand Up @@ -19,4 +19,8 @@ public static boolean isSingleByteOptimizable(RubyString string) {
return StringSupport.isSingleByteOptimizable(string, string.getBytes().getEncoding());
}

public static boolean isAsciiCompatible(RubyString string) {
return string.getByteList().getEncoding().isAsciiCompatible();
}

}
Expand Up @@ -1672,6 +1672,7 @@ public RubyString rstrip(RubyString string) {
}

@CoreMethod(names = "dump", taintFromSelf = true)
@ImportGuards(StringGuards.class)
public abstract static class DumpNode extends CoreMethodNode {

public DumpNode(RubyContext context, SourceSection sourceSection) {
Expand All @@ -1682,13 +1683,41 @@ public DumpNode(DumpNode prev) {
super(prev);
}

@Specialization
public RubyString rstrip(RubyString string) {
notDesignedForCompilation();
@Specialization(guards = "isAsciiCompatible")
public RubyString dumpAsciiCompatible(RubyString string) {
// Taken from org.jruby.RubyString#dump

ByteList outputBytes = dumpCommon(string);

final RubyString result = getContext().makeString(string.getLogicalClass(), outputBytes);
result.getByteList().setEncoding(string.getByteList().getEncoding());
result.setCodeRange(StringSupport.CR_7BIT);

return result;
}

@Specialization(guards = "!isAsciiCompatible")
public RubyString dump(RubyString string) {
// Taken from org.jruby.RubyString#dump

ByteList outputBytes = dumpCommon(string);

outputBytes.append(".force_encoding(\"".getBytes());

This comment has been minimized.

Copy link
@bjfish

bjfish Mar 21, 2015

Contributor

Hi @nirvdrum
I think I'm seeing a findbugs error around here

H I Dm: Found reliance on default encoding in org.jruby.truffle.nodes.core.StringNodes$DumpNode.dump(RubyString): String.getBytes()  At StringNodes.java:[line 1765]

This comment has been minimized.

Copy link
@nirvdrum

nirvdrum Mar 21, 2015

Author Contributor

Thanks. I copied it from JRuby and didn't even think to run FindBugs on it. My bad. I've pushed a fix now.

outputBytes.append(string.getByteList().getEncoding().getName());
outputBytes.append((byte) '"');
outputBytes.append((byte) ')');

return string.dump();
final RubyString result = getContext().makeString(string.getLogicalClass(), outputBytes);
result.getByteList().setEncoding(ASCIIEncoding.INSTANCE);
result.setCodeRange(StringSupport.CR_7BIT);

return result;
}

@TruffleBoundary
private ByteList dumpCommon(RubyString string) {
return StringSupport.dumpCommon(getContext().getRuntime(), string.getByteList());
}
}

@CoreMethod(names = "scan", required = 1, needsBlock = true, taintFromParameters = 0)
Expand Down
Expand Up @@ -123,14 +123,6 @@ public int count(RubyString[] otherStrings) {
return StringSupport.countCommon19(getBytes(), getContext().getRuntime(), table, tables, enc);
}

public RubyString dump() {
ByteList outputBytes = StringSupport.dumpCommon(getContext().getRuntime(), bytes);

final RubyString result = getContext().makeString(getLogicalClass(), outputBytes);

return result;
}

@Override
@TruffleBoundary
public String toString() {
Expand Down

0 comments on commit 9b1c709

Please sign in to comment.