[Bug #19746] String#index with regexp should clear $~ unless matched

This commit is contained in:
Nobuyoshi Nakada 2023-06-28 12:39:52 +09:00
parent 9e709d0f4a
commit 0cbfeb8210
Notes: git 2023-06-28 05:06:47 +00:00
2 changed files with 10 additions and 2 deletions

View File

@ -3880,8 +3880,10 @@ rb_str_index_m(int argc, VALUE *argv, VALUE str)
}
if (RB_TYPE_P(sub, T_REGEXP)) {
if (pos > str_strlen(str, NULL))
if (pos > str_strlen(str, NULL)) {
rb_backref_set(Qnil);
return Qnil;
}
pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
rb_enc_check(str, sub), single_byte_optimizable(str));
@ -3989,8 +3991,10 @@ rb_str_byteindex_m(int argc, VALUE *argv, VALUE str)
}
if (RB_TYPE_P(sub, T_REGEXP)) {
if (pos > RSTRING_LEN(str))
if (pos > RSTRING_LEN(str)) {
rb_backref_set(Qnil);
return Qnil;
}
if (rb_reg_search(sub, str, pos, 0) < 0) {
return Qnil;
}

View File

@ -1375,6 +1375,7 @@ CODE
assert_raise(TypeError) { S("foo").index(Object.new) }
assert_index(nil, S("foo"), //, -100)
assert_index(nil, S("foo"), //, 4)
assert_index(2, S("abcdbce"), /b\Kc/)
@ -1562,6 +1563,7 @@ CODE
m = assert_rindex(3, S("foo"), //)
assert_equal([3, 3], m.offset(0))
assert_rindex(3, S("foo"), //, 4)
assert_rindex(5, S("abcdbce"), /b\Kc/)
@ -3380,6 +3382,7 @@ CODE
assert_raise(TypeError) { S("foo").byteindex(Object.new) }
assert_byteindex(nil, S("foo"), //, -100)
assert_byteindex(nil, S("foo"), //, -4)
assert_byteindex(2, S("abcdbce"), /b\Kc/)
@ -3427,6 +3430,7 @@ CODE
m = assert_byterindex(3, S("foo"), //)
assert_equal([3, 3], m.offset(0))
assert_byterindex(3, S("foo"), //, 4)
assert_byterindex(5, S("abcdbce"), /b\Kc/)