Bug #13135
closed
Regexp.last_match returns nil with s.rindex(//)
Added by shugo (Shugo Maeda) over 7 years ago.
Updated over 7 years ago.
Description
Regexp.last_match returns nil, if // is given to String#rindex:
lexington:ruby$ ruby -ve 'p "foo".rindex(//); p Regexp.last_match'
ruby 2.5.0dev (2017-01-17 trunk 57359) [x86_64-linux]
3
nil
while it returns a MatchData with String#index:
lexington:ruby$ ruby -ve 'p "foo".index(//); p Regexp.last_match'
ruby 2.5.0dev (2017-01-17 trunk 57359) [x86_64-linux]
0
#<MatchData "">
Is this intended behavior?
Shugo Maeda wrote:
Regexp.last_match returns nil, if // is given to String#rindex:
The following patch seems to fix it:
diff --git a/string.c b/string.c
index 919a46d..b5aae67 100644
--- a/string.c
+++ b/string.c
@@ -3516,10 +3516,8 @@ rb_str_rindex_m(int argc, VALUE *argv, VALUE str)
pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
enc, single_byte_optimizable(str));
- if (!RREGEXP_PTR(sub) || RREGEXP_SRC_LEN(sub)) {
- pos = rb_reg_search(sub, str, pos, 1);
- pos = rb_str_sublen(str, pos);
- }
+ pos = rb_reg_search(sub, str, pos, 1);
+ pos = rb_str_sublen(str, pos);
if (pos >= 0) return LONG2NUM(pos);
break;
- Status changed from Open to Closed
Applied in changeset r57374.
string.c: rindex(//) should set $~.
This seems a bug introduced by r520 (1.4.0). [ruby-core:79110] [Bug #13135]
- Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN to 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: DONE
ruby_2_4 r57864 merged revision(s) 57374.
- Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: DONE to 2.2: REQUIRED, 2.3: REQUIRED, 2.4: DONE
- Backport changed from 2.2: REQUIRED, 2.3: REQUIRED, 2.4: DONE to 2.2: DONE, 2.3: REQUIRED, 2.4: DONE
ruby_2_2 r58098 merged revision(s) 57374.
- Backport changed from 2.2: DONE, 2.3: REQUIRED, 2.4: DONE to 2.2: DONE, 2.3: DONE, 2.4: DONE
ruby_2_3 r58176 merged revision(s) 57374.
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0