Project

General

Profile

Feature #9179 ยป matchdata-values_at-named_capture.diff

vzvu3k6k (vzvu3k6k _), 11/29/2013 11:23 PM

View differences:

re.c (working copy)
return rb_ary_aref(argc, argv, match_to_a(match));
}
static VALUE
match_entry(VALUE match, long n)
{
/* n should not exceed num_regs */
return rb_reg_nth_match((int)n, match);
}
/*
* call-seq:
*
* mtch.values_at([index]*) -> array
* mtch.values_at(key, ...) -> array
*
* Uses each <i>index</i> to access the matching values, returning an array of
* the corresponding matches.
* Return an array containing the matches associated with the given keys.
*
* m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
* m.to_a #=> ["HX1138", "H", "X", "113", "8"]
* m.values_at(0, 2, -2) #=> ["HX1138", "X", "113"]
*
* m = /(?<actor>.+) as (?<role>.+)/.match("Robert Duvall as THX 1138")
* m.values_at(:actor, "role") #=> ["Robert Duvall", "THX 1138"]
*/
static VALUE
match_values_at(int argc, VALUE *argv, VALUE match)
{
struct re_registers *regs;
VALUE result = rb_ary_new2(argc);
long i;
match_check(match);
regs = RMATCH_REGS(match);
return rb_get_values_at(match, regs->num_regs, argc, argv, match_entry);
for (i=0; i<argc; i++) {
rb_ary_push(result, match_aref(1, &argv[i], match));
}
return result;
}
test/ruby/test_regexp.rb (working copy)
def test_match_values_at
m = /(...)(...)(...)(...)?/.match("foobarbaz")
assert_equal(["foo", "bar", "baz"], m.values_at(1, 2, 3))
m = /(?<a>...)(?<b>...)(?<c>...)(...)?/.match("foobarbaz")
assert_equal(["foo", "bar", "baz"], m.values_at(:a, "b", "c"))
end
def test_match_string
    (1-1/1)