Skip to content

Commit

Permalink
[Truffle] Move String#starts_with? out to Ruby.
Browse files Browse the repository at this point in the history
While the savings looks small, it'll be much greater once we support more encoding compatibility checks and taintedness.
  • Loading branch information
nirvdrum committed Feb 6, 2015
1 parent 1b11ec9 commit f8854d2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 31 deletions.
1 change: 1 addition & 0 deletions spec/truffle/tags/core/string/start_with_tags.txt
@@ -0,0 +1 @@
fails:String#start_with? works for multibyte strings
Expand Up @@ -1542,37 +1542,6 @@ private RubyArray splitHelper(RubyString string, String sep) {
}
}

@CoreMethod(names = "start_with?", argumentsAsArray = true)
public abstract static class StartWithNode extends CoreMethodNode {

@Child private ToStrNode toStrNode;

public StartWithNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
toStrNode = insert(ToStrNodeFactory.create(getContext(), getSourceSection(), null));
}

public StartWithNode(StartWithNode prev) {
super(prev);
toStrNode = prev.toStrNode;
}

@Specialization
public boolean startWith(VirtualFrame frame, RubyString string, Object... prefixes) {
notDesignedForCompilation();

for (Object prefix : prefixes) {
final RubyString coerced = toStrNode.executeRubyString(frame, prefix);

if (string.toString().startsWith(coerced.toString())) {
return true;
}
}

return false;
}
}

@CoreMethod(names = "sub", required = 2)
public abstract static class SubNode extends RegexpNodes.EscapingNode {

Expand Down
Expand Up @@ -47,4 +47,15 @@ def chomp(separator=$/)
str.chomp!(separator) || str
end

def start_with?(*prefixes)
prefixes.each do |original_prefix|
prefix = Rubinius::Type.check_convert_type original_prefix, String, :to_str
unless prefix
raise TypeError, "no implicit conversion of #{original_prefix.class} into String"
end
return true if self[0, prefix.length] == prefix
end
false
end

end

0 comments on commit f8854d2

Please sign in to comment.