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
3867 3867
static VALUE
3868 3868
rb_str_subpat(VALUE str, VALUE re, VALUE backref)
3869 3869
{
3870
    int i, nth;
3871

  
3870 3872
    if (rb_reg_search(re, str, 0, 0) >= 0) {
3871 3873
        VALUE match = rb_backref_get();
3872
        int nth = rb_reg_backref_number(match, backref);
3873
	return rb_reg_nth_match(nth, match);
3874
        if (RB_TYPE_P(backref, T_ARRAY)) {
3875
            VALUE result = rb_ary_new();
3876
            for (i = 0; i < RARRAY_LEN(backref); i++) {
3877
                nth = rb_reg_backref_number(match, rb_ary_entry(backref, i));
3878
                rb_ary_push(result, rb_reg_nth_match(nth, match));
3879
            }
3880
            return result;
3881
        }
3882
        nth = rb_reg_backref_number(match, backref);
3883
        return rb_reg_nth_match(nth, match);
3874 3884
    }
3875 3885
    return Qnil;
3876 3886
}
......
3998 4008
rb_str_aref_m(int argc, VALUE *argv, VALUE str)
3999 4009
{
4000 4010
    if (argc == 2) {
4001
	if (RB_TYPE_P(argv[0], T_REGEXP)) {
4002
	    return rb_str_subpat(str, argv[0], argv[1]);
4003
	}
4004
	return rb_str_substr(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]));
4011
        if (RB_TYPE_P(argv[0], T_REGEXP)) {
4012
            return rb_str_subpat(str, argv[0], argv[1]);
4013
        }
4014
        return rb_str_substr(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]));
4005 4015
    }
4006 4016
    rb_check_arity(argc, 1, 2);
4007 4017
    return rb_str_aref(str, argv[0]);
test/ruby/test_string.rb
81 81
      assert_equal(S("Bar"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -1])
82 82
      assert_equal(S("Foo"), S("FooBar")[/([A-Z]..)([A-Z]..)/, -2])
83 83
      assert_equal(nil,      S("FooBar")[/([A-Z]..)([A-Z]..)/, -3])
84

  
85
      assert_equal([S("Bar"), S("Foo")], S("FooBar")[/(?<f>F\w\w)(?<b>B\w\w)/, [:b, :f]])
86
      assert_equal([S("Bar"), S("Foo")], S("FooBar")[/(F\w\w)(B\w\w)/, [2, 1]])
84 87
    end
85 88

  
86 89
    o = Object.new