summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'string.c')
-rw-r--r--string.c28
1 files changed, 17 insertions, 11 deletions
diff --git a/string.c b/string.c
index e7f9b44324..e09852cdb9 100644
--- a/string.c
+++ b/string.c
@@ -2673,27 +2673,33 @@ rb_str_rindex(VALUE str, VALUE sub, long pos)
long len, slen;
char *s, *sbeg, *e, *t;
rb_encoding *enc;
- int singlebyte = single_byte_optimizable(str);
+ int singlebyte;
enc = rb_enc_check(str, sub);
- if (is_broken_string(sub)) {
- return -1;
- }
- len = str_strlen(str, enc);
+ if (is_broken_string(sub)) return -1;
+ singlebyte = single_byte_optimizable(str);
+ len = singlebyte ? RSTRING_LEN(str) : str_strlen(str, enc);
slen = str_strlen(sub, enc);
+
/* substring longer than string */
if (len < slen) return -1;
- if (len - pos < slen) {
- pos = len - slen;
- }
- if (len == 0) {
- return pos;
- }
+ if (len - pos < slen) pos = len - slen;
+ if (len == 0) return pos;
+
sbeg = RSTRING_PTR(str);
e = RSTRING_END(str);
t = RSTRING_PTR(sub);
slen = RSTRING_LEN(sub);
+
+ if (pos == 0) {
+ if (memcmp(sbeg, t, slen) == 0)
+ return 0;
+ else
+ return -1;
+ }
+
s = str_nth(sbeg, e, pos, enc, singlebyte);
+
while (s) {
if (memcmp(s, t, slen) == 0) {
return pos;