* re.c (match_values_at): fix regression at r55036.
  MatchData#values_at accepts Range.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55039 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-05-17 18:16:08 +00:00
Родитель 093c389390
Коммит 7f860741b9
3 изменённых файлов: 27 добавлений и 3 удалений

Просмотреть файл

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

24
re.c
Просмотреть файл

@ -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;
}

Просмотреть файл

@ -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))