Skip to content

Commit

Permalink
Updated port of String#end_with?.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Mar 20, 2015
1 parent 7f2f4d0 commit 34b5582
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions core/src/main/java/org/jruby/RubyString.java
Expand Up @@ -3835,27 +3835,25 @@ public IRubyObject end_with_p(ThreadContext context, IRubyObject[]args) {
return context.runtime.getFalse();
}

private boolean end_with_pCommon(IRubyObject arg) {
RubyString otherString = arg.convertToString();

Encoding enc = checkEncoding(otherString);

int otherLength = otherString.value.getRealSize();
// MRI: rb_str_end_with, loop body
private boolean end_with_pCommon(IRubyObject tmp) {
int p, s, e;
Encoding enc;

if (otherLength == 0) {
// other is '', so return true
tmp = tmp.convertToString();
ByteList tmpBL = ((RubyString)tmp).value;
enc = checkEncoding((RubyString)tmp);
if (value.realSize() < tmpBL.realSize()) return false;
p = value.begin();
e = p + value.realSize();
s = e - tmpBL.realSize();
if (enc.leftAdjustCharHead(value.unsafeBytes(), p, s, e) != s) {
return false;
}
if (ByteList.memcmp(value.unsafeBytes(), s, tmpBL.unsafeBytes(), tmpBL.begin(), tmpBL.realSize()) == 0) {
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);
return false;
}

private static final ByteList SPACE_BYTELIST = new ByteList(ByteList.plain(" "));
Expand Down

0 comments on commit 34b5582

Please sign in to comment.