diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | re.c | 24 | ||||
-rw-r--r-- | test/ruby/test_regexp.rb | 1 |
3 files changed, 27 insertions, 3 deletions
@@ -1,3 +1,8 @@ +Wed May 18 03:16:06 2016 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * re.c (match_values_at): fix regression at r55036. + MatchData#values_at accepts Range. + Wed May 18 02:02:58 2016 NARUSE, Yui <naruse@ruby-lang.org> * re.c (match_aref): remove useless condition and call rb_fix2int. @@ -1921,6 +1921,7 @@ match_aref(int argc, VALUE *argv, VALUE match) static VALUE match_values_at(int argc, VALUE *argv, VALUE match) { + struct re_registers *regs = RMATCH_REGS(match); VALUE result; int i; @@ -1928,14 +1929,31 @@ match_values_at(int argc, VALUE *argv, VALUE match) result = rb_ary_new2(argc); for (i=0; i<argc; i++) { + VALUE tmp; + int num; + long beg, len, olen; if (FIXNUM_P(argv[i])) { rb_ary_push(result, rb_reg_nth_match(FIX2INT(argv[i]), match)); + continue; } - else { - int num = namev_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp, argv[i]); - if (num < 0) num = NUM2INT(argv[i]); + num = namev_to_backref_number(regs, RMATCH(match)->regexp, argv[i]); + if (num >= 0) { rb_ary_push(result, rb_reg_nth_match(num, match)); + continue; + } + /* check if idx is Range */ + olen = regs->num_regs; + if (rb_range_beg_len(argv[i], &beg, &len, olen, 1)) { + long j, end = olen < beg+len ? olen : beg+len; + for (j = beg; j < end; j++) { + rb_ary_push(result, rb_reg_nth_match((int)j, match)); + } + if (beg + len > j) + rb_ary_resize(result, RARRAY_LEN(result) + (beg + len) - j); + continue; } + tmp = rb_to_int(argv[i]); + rb_ary_push(result, rb_reg_nth_match(NUM2INT(tmp), match)); } return result; } diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb index aea2f2f6ea..a8a120cf3f 100644 --- a/test/ruby/test_regexp.rb +++ b/test/ruby/test_regexp.rb @@ -383,6 +383,7 @@ class TestRegexp < Test::Unit::TestCase def test_match_values_at m = /(...)(...)(...)(...)?/.match("foobarbaz") assert_equal(["foo", "bar", "baz"], m.values_at(1, 2, 3)) + assert_equal(["foo", "bar", "baz"], m.values_at(1..3)) m = /(?<a>\d+) *(?<op>[+\-*\/]) *(?<b>\d+)/.match("1 + 2") assert_equal(["1", "2", "+"], m.values_at(:a, 'b', :op)) |