Skip to content

Commit b110106

Browse files
committed
Clean up and align logic in String#start_with? and end_with?.
Whitespace is your friend.
1 parent 72234f9 commit b110106

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5272,7 +5272,7 @@ private boolean start_with_pCommon(IRubyObject arg) {
52725272
return true;
52735273
}
52745274

5275-
if (value.getRealSize() < otherString.value.getRealSize()) return false;
5275+
if (value.getRealSize() < otherLength) return false;
52765276

52775277
return value.startsWith(otherString.value);
52785278
}
@@ -5296,20 +5296,36 @@ public IRubyObject end_with_p(ThreadContext context, IRubyObject[]args) {
52965296
}
52975297

52985298
private boolean end_with_pCommon(IRubyObject arg) {
5299-
IRubyObject tmp = arg.checkStringType();
5300-
if (tmp.isNil()) return false;
5301-
RubyString otherString = (RubyString)tmp;
5302-
int otherLength = otherString.value.getRealSize();
5299+
Ruby runtime = getRuntime();
5300+
RubyString otherString;
5301+
5302+
if (!runtime.is2_0()) {
5303+
// 1.8 and 1.9 ignores uncoercible argument
5304+
IRubyObject tmp = arg.checkStringType();
5305+
if (tmp.isNil()) return false;
5306+
otherString = (RubyString) tmp;
5307+
} else {
5308+
// 2.0+ requires coersion to succeed
5309+
otherString = arg.convertToString();
5310+
}
5311+
53035312
Encoding enc = checkEncoding(otherString);
5304-
if (value.getRealSize() < otherLength) return false;
5305-
int p = value.getBegin();
5306-
int end = p + value.getRealSize();
5313+
5314+
int otherLength = otherString.value.getRealSize();
5315+
53075316
if (otherLength == 0) {
53085317
// other is '', so return true
53095318
return true;
53105319
}
5320+
5321+
if (value.getRealSize() < otherLength) return false;
5322+
5323+
int p = value.getBegin();
5324+
int end = p + value.getRealSize();
53115325
int s = end - otherLength;
5326+
53125327
if (enc.leftAdjustCharHead(value.getUnsafeBytes(), p, s, end) != s) return false;
5328+
53135329
return value.endsWith(otherString.value);
53145330
}
53155331

0 commit comments

Comments
 (0)