From 2759cd4fbb9bf5c3cec679a74782918d959fd55f Mon Sep 17 00:00:00 2001 From: Andrew Vit Date: Sun, 24 Jan 2016 00:33:13 -0800 Subject: [PATCH] Multiple captures with string[regexp, array] --- string.c | 22 ++++++++++++++++------ test/ruby/test_string.rb | 3 +++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/string.c b/string.c index b499fa2..ed1ba51 100644 --- a/string.c +++ b/string.c @@ -3867,10 +3867,20 @@ rb_str_include_range_p(VALUE beg, VALUE end, VALUE val, VALUE exclusive) 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; } @@ -3998,10 +4008,10 @@ static VALUE 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]); diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 7eed784..f1fc99d 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -81,6 +81,9 @@ def test_AREF # '[]' 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\w\w)(?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.4.6