Skip to content
Permalink
Browse files
Fix zero-length search string behavior in String#start_with?.
Fixes #2434.
  • Loading branch information
headius committed Jan 6, 2015
1 parent d0c00ed commit d7db423
Showing 1 changed file with 22 additions and 3 deletions.
@@ -5250,11 +5250,30 @@ public IRubyObject start_with_p(ThreadContext context, IRubyObject[]args) {
}

private boolean start_with_pCommon(IRubyObject arg) {
IRubyObject tmp = arg.checkStringType();
if (tmp.isNil()) return false;
RubyString otherString = (RubyString)tmp;
Ruby runtime = getRuntime();
RubyString otherString;

if (!runtime.is2_0()) {
// 1.8 and 1.9 ignores uncoercible argument
IRubyObject tmp = arg.checkStringType();
if (tmp.isNil()) return false;
otherString = (RubyString) tmp;
} else {
// 2.0+ requires coersion to succeed
otherString = arg.convertToString();
}

checkEncoding(otherString);

int otherLength = otherString.value.getRealSize();

if (otherLength == 0) {
// other is '', so return true
return true;
}

if (value.getRealSize() < otherString.value.getRealSize()) return false;

return value.startsWith(otherString.value);
}

0 comments on commit d7db423

Please sign in to comment.