aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-17 17:10:01 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-17 17:10:01 +0000
commita14a58b959405ca4f7b8472f331a404269daeb2e (patch)
tree18efe622116c20aebc0ce93dccb270652b4941b0
parent3d0850932ab8842845fb9279754e41acf4380e2f (diff)
downloadruby-a14a58b959405ca4f7b8472f331a404269daeb2e.tar.gz
* re.c (match_values_at): MatchData#values_at supports named captures
[Feature #9179] * re.c (namev_to_backref_number): separeted. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55036 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog7
-rw-r--r--NEWS1
-rw-r--r--re.c74
-rw-r--r--test/ruby/test_regexp.rb7
4 files changed, 59 insertions, 30 deletions
diff --git a/ChangeLog b/ChangeLog
index 20dab87235..d4cc4e7ded 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Wed May 18 01:57:43 2016 NARUSE, Yui <naruse@ruby-lang.org>
+
+ * re.c (match_values_at): MatchData#values_at supports named captures
+ [Feature #9179]
+
+ * re.c (namev_to_backref_number): separeted.
+
Tue May 18 00:05:00 2016 Kenta Murata <mrkn@mrkn.jp>
* enum.c (enum_sum): Optimize for a range from int to int.
diff --git a/NEWS b/NEWS
index 21a8342443..4ca91819c8 100644
--- a/NEWS
+++ b/NEWS
@@ -59,6 +59,7 @@ with all sufficient information, see the ChangeLog file or Redmine
* MatchData
* MatchData#named_captures [Feature #11999]
+ * MatchData#values_at supports named captures[Feature #9179]
=== Stdlib updates (outstanding ones only)
diff --git a/re.c b/re.c
index 2cb430542e..da600fbad4 100644
--- a/re.c
+++ b/re.c
@@ -1829,6 +1829,28 @@ name_to_backref_error(VALUE name)
name);
}
+static int
+namev_to_backref_number(struct re_registers *regs, VALUE re, VALUE name)
+{
+ int num;
+
+ switch (TYPE(name)) {
+ case T_SYMBOL:
+ name = rb_sym2str(name);
+ /* fall through */
+ case T_STRING:
+ if (NIL_P(re) || !rb_enc_compatible(RREGEXP_SRC(re), name) ||
+ (num = name_to_backref_number(regs, re,
+ RSTRING_PTR(name), RSTRING_END(name))) < 1) {
+ name_to_backref_error(name);
+ }
+ return num;
+
+ default:
+ return -1;
+ }
+}
+
/*
* call-seq:
* mtch[i] -> str or nil
@@ -1859,7 +1881,7 @@ name_to_backref_error(VALUE name)
static VALUE
match_aref(int argc, VALUE *argv, VALUE match)
{
- VALUE idx, rest, re;
+ VALUE idx, rest;
match_check(match);
rb_scan_args(argc, argv, "11", &idx, &rest);
@@ -1871,25 +1893,9 @@ match_aref(int argc, VALUE *argv, VALUE match)
}
}
else {
- const char *p;
- int num;
-
- switch (TYPE(idx)) {
- case T_SYMBOL:
- idx = rb_sym2str(idx);
- /* fall through */
- case T_STRING:
- p = StringValuePtr(idx);
- re = RMATCH(match)->regexp;
- if (NIL_P(re) || !rb_enc_compatible(RREGEXP_SRC(re), idx) ||
- (num = name_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp,
- p, p + RSTRING_LEN(idx))) < 1) {
- name_to_backref_error(idx);
- }
+ int num = namev_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp, idx);
+ if (num >= 0) {
return rb_reg_nth_match(num, match);
-
- default:
- break;
}
}
}
@@ -1897,14 +1903,6 @@ match_aref(int argc, VALUE *argv, VALUE match)
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:
*
@@ -1916,16 +1914,32 @@ match_entry(VALUE match, long n)
* 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 = /(?<a>\d+) *(?<op>[+\-*\/]) *(?<b>\d+)/.match("1 + 2")
+ * m.to_a #=> ["1 + 2", "1", "+", "2"]
+ * m.values_at(:a, :b, :op) #=> ["1", "2", "+"]
*/
static VALUE
match_values_at(int argc, VALUE *argv, VALUE match)
{
- struct re_registers *regs;
+ VALUE result;
+ int i;
match_check(match);
- regs = RMATCH_REGS(match);
- return rb_get_values_at(match, regs->num_regs, argc, argv, match_entry);
+ result = rb_ary_new2(argc);
+
+ for (i=0; i<argc; i++) {
+ if (FIXNUM_P(argv[i])) {
+ rb_ary_push(result, rb_reg_nth_match(FIX2INT(argv[i]), match));
+ }
+ else {
+ int num = namev_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp, argv[i]);
+ if (num < 0) num = NUM2INT(argv[i]);
+ rb_ary_push(result, rb_reg_nth_match(num, match));
+ }
+ }
+ return result;
}
diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb
index 1eb7e24b8c..aea2f2f6ea 100644
--- a/test/ruby/test_regexp.rb
+++ b/test/ruby/test_regexp.rb
@@ -383,6 +383,13 @@ class TestRegexp < Test::Unit::TestCase
def test_match_values_at
m = /(...)(...)(...)(...)?/.match("foobarbaz")
assert_equal(["foo", "bar", "baz"], m.values_at(1, 2, 3))
+
+ m = /(?<a>\d+) *(?<op>[+\-*\/]) *(?<b>\d+)/.match("1 + 2")
+ assert_equal(["1", "2", "+"], m.values_at(:a, 'b', :op))
+ idx = Object.new
+ def idx.to_int; 2; end
+ assert_equal(["+"], m.values_at(idx))
+ assert_raise(TypeError){ m.values_at(nil) }
end
def test_match_string