ruby-changes:42965
From: nobu <ko1@a...>
Date: Wed, 18 May 2016 03:16:13 +0900 (JST)
Subject: [ruby-changes:42965] nobu:r55039 (trunk): re.c: fix up r55036
nobu 2016-05-18 03:16:08 +0900 (Wed, 18 May 2016) New Revision: 55039 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=55039 Log: re.c: fix up r55036 * re.c (match_values_at): fix regression at r55036. MatchData#values_at accepts Range. Modified files: trunk/ChangeLog trunk/re.c trunk/test/ruby/test_regexp.rb Index: re.c =================================================================== --- re.c (revision 55038) +++ re.c (revision 55039) @@ -1921,6 +1921,7 @@ match_aref(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/re.c#L1921 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, V https://github.com/ruby/ruby/blob/trunk/re.c#L1929 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; } Index: ChangeLog =================================================================== --- ChangeLog (revision 55038) +++ ChangeLog (revision 55039) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed May 18 03:16:06 2016 Nobuyoshi Nakada <nobu@r...> + + * re.c (match_values_at): fix regression at r55036. + MatchData#values_at accepts Range. + Wed May 18 02:02:58 2016 NARUSE, Yui <naruse@r...> * re.c (match_aref): remove useless condition and call rb_fix2int. Index: test/ruby/test_regexp.rb =================================================================== --- test/ruby/test_regexp.rb (revision 55038) +++ test/ruby/test_regexp.rb (revision 55039) @@ -383,6 +383,7 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L383 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)) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/