Project

General

Profile

Bug #13135

Regexp.last_match returns nil with s.rindex(//)

Added by shugo (Shugo Maeda) almost 4 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:79110]

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?

Updated by shugo (Shugo Maeda) almost 4 years ago

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;

#2

Updated by shugo (Shugo Maeda) almost 4 years ago

  • 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]

Updated by naruse (Yui NARUSE) over 3 years ago

  • 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.

Updated by nagachika (Tomoyuki Chikanaga) over 3 years ago

  • Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: DONE to 2.2: REQUIRED, 2.3: REQUIRED, 2.4: DONE

Updated by usa (Usaku NAKAMURA) over 3 years ago

  • 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.

Updated by nagachika (Tomoyuki Chikanaga) over 3 years ago

  • 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