Skip to content

Commit

Permalink
Split rb_str_rindex like MRI and fix COW offset bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Mar 30, 2015
1 parent 94e7bf9 commit 46a29c2
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions core/src/main/java/org/jruby/util/StringSupport.java
Expand Up @@ -911,20 +911,28 @@ public static int rindex(ByteList source, int sourceChars, int subChars, int pos
}

int s = nth(enc, sourceBytes, sbeg, end, pos);
byte[] subBytes = subString.unsafeBytes();

// switch to byte size here; this is now MRI:str_rindex
if (subSize == 0) return pos;
int t = subString.begin();
return strRindex(source, subString, s, pos, enc);
}

private static int strRindex(ByteList str, ByteList sub, int s, int pos, Encoding enc) {
int slen;
byte[] strBytes = str.unsafeBytes();
byte[] subBytes = sub.unsafeBytes();
int sbeg, e, t;

sbeg = str.begin();
e = str.begin() + str.realSize();
t = sub.begin();
slen = sub.realSize();

// s >= 0 because -1 is our OOB, where MRI's is null pointer (0)
while (s >= 0 && s + subSize <= sourceSize) {
if (ByteList.memcmp(sourceBytes, s, subBytes, t, subSize) == 0) {
while (s >= sbeg) {
if (ByteList.memcmp(strBytes, s, subBytes, t, slen) == 0) {
return pos;
}
if (pos == 0) break;
pos--;
s = enc.prevCharHead(sourceBytes, sbeg, s, end);
s = enc.prevCharHead(strBytes, sbeg, s, e);
}

return -1;
Expand Down

0 comments on commit 46a29c2

Please sign in to comment.