Project

General

Profile

Feature #11100 » 11100-Multiple-captures-with-string-regexp-array.patch

avit (Andrew Vit), 01/24/2016 08:41 AM

View differences:

string.c
static VALUE
rb_str_subpat(VALUE str, VALUE re, VALUE backref)
{
int i, nth;
if (rb_reg_search(re, str, 0, 0) >= 0) {
VALUE match = rb_backref_get();
int nth = rb_reg_backref_number(match, backref);
return rb_reg_nth_match(nth, match);
if (RB_TYPE_P(backref, T_ARRAY)) {
VALUE result = rb_ary_new();
for (i = 0; i < RARRAY_LEN(backref); i++) {
nth = rb_reg_backref_number(match, rb_ary_entry(backref, i));
rb_ary_push(result, rb_reg_nth_match(nth, match));
}
return result;
}
nth = rb_reg_backref_number(match, backref);
return rb_reg_nth_match(nth, match);
}
return Qnil;
}
......
rb_str_aref_m(int argc, VALUE *argv, VALUE str)
{
if (argc == 2) {
if (RB_TYPE_P(argv[0], T_REGEXP)) {
return rb_str_subpat(str, argv[0], argv[1]);
}
return rb_str_substr(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]));
if (RB_TYPE_P(argv[0], T_REGEXP)) {
return rb_str_subpat(str, argv[0], argv[1]);
}
return rb_str_substr(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]));
}
rb_check_arity(argc, 1, 2);
return rb_str_aref(str, argv[0]);
test/ruby/test_string.rb
assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -1])
assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -2])
assert_equal(nil, S("FooBar")[/([A-Z]..)([A-Z]..)/, -3])
assert_equal([S("Bar"), S("Foo")], S("FooBar")[/(?<f>F\w\w)(?<b>B\w\w)/, [:b, :f]])
assert_equal([S("Bar"), S("Foo")], S("FooBar")[/(F\w\w)(B\w\w)/, [2, 1]])
end
o = Object.new
(2-2/2)