Permalink
Browse files

fix the MatchData#[] behavior if passed a negative Fixnum object as i…

…ndex

In below case, MatchData#[] always returns a nil:

	index < 0 &&
		(matched count) + index >= 0

Sample code:

```
str = 'foo bar baz'
m = str.match(/\w+/)
p m[-1]  #=> expect "foo", but nil
```

This fix above behavior.
  • Loading branch information...
1 parent 874b0df commit 998999953decfd1162cef517fa87498e99897c21 @Watson1978 Watson1978 committed Aug 27, 2012
Showing with 4 additions and 1 deletion.
  1. +4 −1 re.c
View
5 re.c
@@ -1787,9 +1787,12 @@ match_aref(VALUE rcv, SEL sel, int argc, VALUE *argv)
if (NIL_P(rest)) {
switch (TYPE(backref)) {
+ case T_FIXNUM:
+ if (FIX2INT(backref) < 0) {
+ break;
+ }
case T_STRING:
case T_SYMBOL:
- case T_FIXNUM:
{
const int pos = match_backref_number(rcv, backref, false);
return rb_reg_nth_match(pos, rcv);

0 comments on commit 9989999

Please sign in to comment.