Skip to content

Commit 46a29c2

Browse files
committed
Split rb_str_rindex like MRI and fix COW offset bug.
1 parent 94e7bf9 commit 46a29c2

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

core/src/main/java/org/jruby/util/StringSupport.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -911,20 +911,28 @@ public static int rindex(ByteList source, int sourceChars, int subChars, int pos
911911
}
912912

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

916-
// switch to byte size here; this is now MRI:str_rindex
917-
if (subSize == 0) return pos;
918-
int t = subString.begin();
915+
return strRindex(source, subString, s, pos, enc);
916+
}
917+
918+
private static int strRindex(ByteList str, ByteList sub, int s, int pos, Encoding enc) {
919+
int slen;
920+
byte[] strBytes = str.unsafeBytes();
921+
byte[] subBytes = sub.unsafeBytes();
922+
int sbeg, e, t;
923+
924+
sbeg = str.begin();
925+
e = str.begin() + str.realSize();
926+
t = sub.begin();
927+
slen = sub.realSize();
919928

920-
// s >= 0 because -1 is our OOB, where MRI's is null pointer (0)
921-
while (s >= 0 && s + subSize <= sourceSize) {
922-
if (ByteList.memcmp(sourceBytes, s, subBytes, t, subSize) == 0) {
929+
while (s >= sbeg) {
930+
if (ByteList.memcmp(strBytes, s, subBytes, t, slen) == 0) {
923931
return pos;
924932
}
925933
if (pos == 0) break;
926934
pos--;
927-
s = enc.prevCharHead(sourceBytes, sbeg, s, end);
935+
s = enc.prevCharHead(strBytes, sbeg, s, e);
928936
}
929937

930938
return -1;

0 commit comments

Comments
 (0)