Skip to content

Commit

Permalink
Clean up and align logic in String#start_with? and end_with?.
Browse files Browse the repository at this point in the history
Whitespace is your friend.
  • Loading branch information
headius committed Jan 6, 2015
1 parent 72234f9 commit b110106
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions core/src/main/java/org/jruby/RubyString.java
Expand Up @@ -5272,7 +5272,7 @@ private boolean start_with_pCommon(IRubyObject arg) {
return true;
}

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

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

private boolean end_with_pCommon(IRubyObject arg) {
IRubyObject tmp = arg.checkStringType();
if (tmp.isNil()) return false;
RubyString otherString = (RubyString)tmp;
int otherLength = otherString.value.getRealSize();
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();
}

Encoding enc = checkEncoding(otherString);
if (value.getRealSize() < otherLength) return false;
int p = value.getBegin();
int end = p + value.getRealSize();

int otherLength = otherString.value.getRealSize();

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

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

int p = value.getBegin();
int end = p + value.getRealSize();
int s = end - otherLength;

if (enc.leftAdjustCharHead(value.getUnsafeBytes(), p, s, end) != s) return false;

return value.endsWith(otherString.value);
}

Expand Down

0 comments on commit b110106

Please sign in to comment.