Skip to content

Commit d7db423

Browse files
committed
Fix zero-length search string behavior in String#start_with?.
Fixes #2434.
1 parent d0c00ed commit d7db423

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

core/src/main/java/org/jruby/RubyString.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5250,11 +5250,30 @@ public IRubyObject start_with_p(ThreadContext context, IRubyObject[]args) {
52505250
}
52515251

52525252
private boolean start_with_pCommon(IRubyObject arg) {
5253-
IRubyObject tmp = arg.checkStringType();
5254-
if (tmp.isNil()) return false;
5255-
RubyString otherString = (RubyString)tmp;
5253+
Ruby runtime = getRuntime();
5254+
RubyString otherString;
5255+
5256+
if (!runtime.is2_0()) {
5257+
// 1.8 and 1.9 ignores uncoercible argument
5258+
IRubyObject tmp = arg.checkStringType();
5259+
if (tmp.isNil()) return false;
5260+
otherString = (RubyString) tmp;
5261+
} else {
5262+
// 2.0+ requires coersion to succeed
5263+
otherString = arg.convertToString();
5264+
}
5265+
52565266
checkEncoding(otherString);
5267+
5268+
int otherLength = otherString.value.getRealSize();
5269+
5270+
if (otherLength == 0) {
5271+
// other is '', so return true
5272+
return true;
5273+
}
5274+
52575275
if (value.getRealSize() < otherString.value.getRealSize()) return false;
5276+
52585277
return value.startsWith(otherString.value);
52595278
}
52605279

0 commit comments

Comments
 (0)