mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
fix off-by-one error in String#rindex(fixnum)
null-terminated string should not be included in search targets.
This commit is contained in:
+1
-1
@@ -1660,7 +1660,7 @@ mrb_str_rindex_m(mrb_state *mrb, mrb_value str)
|
||||
mrb_int len = RSTRING_LEN(str);
|
||||
unsigned char *p = (unsigned char*)RSTRING_PTR(str);
|
||||
|
||||
for (pos=len;pos>=0;pos--) {
|
||||
for (pos=len-1;pos>=0;pos--) {
|
||||
if (p[pos] == c) return mrb_fixnum_value(pos);
|
||||
}
|
||||
return mrb_nil_value();
|
||||
|
||||
@@ -330,6 +330,9 @@ assert('String#rindex', '15.2.10.5.31') do
|
||||
assert_nil 'abc'.rindex('d')
|
||||
assert_equal 0, 'abcabc'.rindex('a', 1)
|
||||
assert_equal 3, 'abcabc'.rindex('a', 4)
|
||||
|
||||
assert_equal 3, 'abcabc'.rindex(97)
|
||||
assert_equal nil, 'abcabc'.rindex(0)
|
||||
end
|
||||
|
||||
# 'String#scan', '15.2.10.5.32' will be tested in mrbgems.
|
||||
|
||||
Reference in New Issue
Block a user