[前][次][番号順一覧][スレッド一覧]

ruby-changes:42962

From: naruse <ko1@a...>
Date: Wed, 18 May 2016 02:10:06 +0900 (JST)
Subject: [ruby-changes:42962] naruse:r55036 (trunk): * re.c (match_values_at): MatchData#values_at supports named captures

naruse	2016-05-18 02:10:01 +0900 (Wed, 18 May 2016)

  New Revision: 55036

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=55036

  Log:
    * re.c (match_values_at): MatchData#values_at supports named captures
      [Feature #9179]
    
    * re.c (namev_to_backref_number): separeted.

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/re.c
    trunk/test/ruby/test_regexp.rb
Index: test/ruby/test_regexp.rb
===================================================================
--- test/ruby/test_regexp.rb	(revision 55035)
+++ test/ruby/test_regexp.rb	(revision 55036)
@@ -383,6 +383,13 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L383
   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
Index: re.c
===================================================================
--- re.c	(revision 55035)
+++ re.c	(revision 55036)
@@ -1829,6 +1829,28 @@ name_to_backref_error(VALUE name) https://github.com/ruby/ruby/blob/trunk/re.c#L1829
 	     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) https://github.com/ruby/ruby/blob/trunk/re.c#L1881
 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 https://github.com/ruby/ruby/blob/trunk/re.c#L1893
 	    }
 	}
 	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 https://github.com/ruby/ruby/blob/trunk/re.c#L1903
     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) https://github.com/ruby/ruby/blob/trunk/re.c#L1914
  *     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;
 }
 
 
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55035)
+++ ChangeLog	(revision 55036)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed May 18 01:57:43 2016  NARUSE, Yui  <naruse@r...>
+
+	* 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@m...>
 
 	* enum.c (enum_sum): Optimize for a range from int to int.
Index: NEWS
===================================================================
--- NEWS	(revision 55035)
+++ NEWS	(revision 55036)
@@ -59,6 +59,7 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L59
 * MatchData
 
   * MatchData#named_captures [Feature #11999]
+  * MatchData#values_at supports named captures[Feature #9179]
 
 === Stdlib updates (outstanding ones only)
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]