Skip to content

Commit f8854d2

Browse files
committed
[Truffle] Move String#starts_with? out to Ruby.
While the savings looks small, it'll be much greater once we support more encoding compatibility checks and taintedness.
1 parent 1b11ec9 commit f8854d2

File tree

3 files changed

+12
-31
lines changed

3 files changed

+12
-31
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fails:String#start_with? works for multibyte strings

truffle/src/main/java/org/jruby/truffle/nodes/core/StringNodes.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,37 +1542,6 @@ private RubyArray splitHelper(RubyString string, String sep) {
15421542
}
15431543
}
15441544

1545-
@CoreMethod(names = "start_with?", argumentsAsArray = true)
1546-
public abstract static class StartWithNode extends CoreMethodNode {
1547-
1548-
@Child private ToStrNode toStrNode;
1549-
1550-
public StartWithNode(RubyContext context, SourceSection sourceSection) {
1551-
super(context, sourceSection);
1552-
toStrNode = insert(ToStrNodeFactory.create(getContext(), getSourceSection(), null));
1553-
}
1554-
1555-
public StartWithNode(StartWithNode prev) {
1556-
super(prev);
1557-
toStrNode = prev.toStrNode;
1558-
}
1559-
1560-
@Specialization
1561-
public boolean startWith(VirtualFrame frame, RubyString string, Object... prefixes) {
1562-
notDesignedForCompilation();
1563-
1564-
for (Object prefix : prefixes) {
1565-
final RubyString coerced = toStrNode.executeRubyString(frame, prefix);
1566-
1567-
if (string.toString().startsWith(coerced.toString())) {
1568-
return true;
1569-
}
1570-
}
1571-
1572-
return false;
1573-
}
1574-
}
1575-
15761545
@CoreMethod(names = "sub", required = 2)
15771546
public abstract static class SubNode extends RegexpNodes.EscapingNode {
15781547

truffle/src/main/ruby/jruby/truffle/core/rubinius/kernel/common/string.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,15 @@ def chomp(separator=$/)
4747
str.chomp!(separator) || str
4848
end
4949

50+
def start_with?(*prefixes)
51+
prefixes.each do |original_prefix|
52+
prefix = Rubinius::Type.check_convert_type original_prefix, String, :to_str
53+
unless prefix
54+
raise TypeError, "no implicit conversion of #{original_prefix.class} into String"
55+
end
56+
return true if self[0, prefix.length] == prefix
57+
end
58+
false
59+
end
60+
5061
end

0 commit comments

Comments
 (0)