summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-06-28 12:39:52 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2023-06-28 14:06:28 +0900
commit0cbfeb8210cd177bee95c512834c483daec36f74 (patch)
treef49c29d8b1038ddd7d3249300683c619bba15a3c /string.c
parent9e709d0f4a55c6d3e0b66569b287b6bc3451cf23 (diff)
[Bug #19746] `String#index` with regexp should clear `$~` unless matched
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/7988
Diffstat (limited to 'string.c')
-rw-r--r--string.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/string.c b/string.c
index d10eb236c9..555b0a1abd 100644
--- a/string.c
+++ b/string.c
@@ -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;
}