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

ruby-changes:41790

From: sorah <ko1@a...>
Date: Thu, 18 Feb 2016 14:28:54 +0900 (JST)
Subject: [ruby-changes:41790] sorah:r53863 (trunk): * re.c: Add MatchData#named_captures

sorah	2016-02-18 14:29:18 +0900 (Thu, 18 Feb 2016)

  New Revision: 53863

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

  Log:
    * re.c: Add MatchData#named_captures
      [Feature #11999] [ruby-core:72897]
    
    * test/ruby/test_regexp.rb(test_match_data_named_captures): Test for above.
    
    * NEWS: News about MatchData#named_captures.

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/re.c
    trunk/test/ruby/test_regexp.rb
Index: NEWS
===================================================================
--- NEWS	(revision 53862)
+++ NEWS	(revision 53863)
@@ -24,6 +24,10 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L24
 
   * String.new(capacity: size) [Feature #12024]
 
+* MatchData
+
+  * MatchData#named_captures [Feature #11999]
+
 === Stdlib updates (outstanding ones only)
 
 * CSV
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53862)
+++ ChangeLog	(revision 53863)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Feb 18 14:15:38 2016  Shota Fukumori  <her@s...>
+
+	* re.c: Add MatchData#named_captures
+	  [Feature #11999] [ruby-core:72897]
+	
+	* test/ruby/test_regexp.rb(test_match_data_named_captures): Test for above.
+
+	* NEWS: News about MatchData#named_captures.
+
 Wed Feb 17 21:41:29 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* defs/id.def (predefined): add idLASTLINE and idBACKREF for $_
Index: test/ruby/test_regexp.rb
===================================================================
--- test/ruby/test_regexp.rb	(revision 53862)
+++ test/ruby/test_regexp.rb	(revision 53863)
@@ -175,6 +175,20 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L175
     assert_raise(IndexError, bug9903) {m[key.dup.force_encoding(Encoding::Shift_JIS)]}
   end
 
+  def test_match_data_named_captures
+    assert_equal({'a' => '1', 'b' => '2', 'c' => nil}, /^(?<a>.)(?<b>.)(?<c>.)?/.match('12').named_captures)
+    assert_equal({'a' => '1', 'b' => '2', 'c' => '3'}, /^(?<a>.)(?<b>.)(?<c>.)?/.match('123').named_captures)
+    assert_equal({'a' => '1', 'b' => '2', 'c' => ''}, /^(?<a>.)(?<b>.)(?<c>.?)/.match('12').named_captures)
+
+    assert_equal({'a' => 'x'}, /(?<a>x)|(?<a>y)/.match('x').named_captures)
+    assert_equal({'a' => 'y'}, /(?<a>x)|(?<a>y)/.match('y').named_captures)
+
+    assert_equal({'a' => '1', 'b' => '2'}, /^(.)(?<a>.)(?<b>.)/.match('012').named_captures)
+    assert_equal({'a' => '2'}, /^(?<a>.)(?<a>.)/.match('12').named_captures)
+
+    assert_equal({}, /^(.)/.match('123').named_captures)
+  end
+
   def test_assign_named_capture
     assert_equal("a", eval('/(?<foo>.)/ =~ "a"; foo'))
     assert_equal(nil, eval('/(?<@foo>.)/ =~ "a"; defined?(@foo)'))
Index: re.c
===================================================================
--- re.c	(revision 53862)
+++ re.c	(revision 53863)
@@ -1937,6 +1937,72 @@ match_to_s(VALUE match) https://github.com/ruby/ruby/blob/trunk/re.c#L1937
     return str;
 }
 
+static int
+match_named_captures_iter(const OnigUChar *name, const OnigUChar *name_end,
+	int back_num, int *back_refs, OnigRegex regex, void *arg) {
+    struct MEMO *memo = MEMO_CAST(arg);
+    VALUE hash = memo->v1;
+    VALUE match = memo->v2;
+
+    VALUE key = rb_enc_str_new((const char *)name, name_end-name, regex->enc);
+    VALUE value;
+
+    int i;
+    int found = 0;
+
+    for (i = 0; i < back_num; i++) {
+	value = rb_reg_nth_match(back_refs[i], match);
+	if (RTEST(value)) {
+	    rb_hash_aset(hash, key, value);
+	    found = 1;
+	}
+    }
+
+    if (found == 0) {
+	rb_hash_aset(hash, key, Qnil);
+    }
+
+    return 0;
+}
+
+/*
+ *  call-seq:
+ *     mtch.named_captures -> hash
+ *
+ *  Returns a Hash using named capture.
+ *
+ *  A key of the hash is a name of the named captures.
+ *  A value of the hash is a string of last successful capture of corresponding
+ *  group.
+ *
+ *     m = /(?<a>.)(?<b>.)/.match("01")
+ *     m.named_captures #=> {"a" => "0", "b" => "1"}
+ *
+ *     m = /(?<a>.)(?<b>.)?/.match("0")
+ *     m.named_captures #=> {"a" => "0", "b" => nil}
+ *
+ *     m = /(?<a>.)(?<a>.)/.match("01")
+ *     m.named_captures #=> {"a" => "1"}
+ *
+ *     m = /(?<a>x)|(?<a>y)/.match("x")
+ *     m.named_captures #=> {"a" => "x"}
+ */
+
+static VALUE
+match_named_captures(VALUE match)
+{
+    VALUE hash;
+    struct MEMO *memo;
+
+    match_check(match);
+
+    hash = rb_hash_new();
+    memo = MEMO_NEW(hash, match, 0);
+
+    onig_foreach_name(RREGEXP(RMATCH(match)->regexp)->ptr, match_named_captures_iter, (void*)memo);
+
+    return hash;
+}
 
 /*
  *  call-seq:
@@ -3751,6 +3817,7 @@ Init_Regexp(void) https://github.com/ruby/ruby/blob/trunk/re.c#L3817
     rb_define_method(rb_cMatch, "to_a", match_to_a, 0);
     rb_define_method(rb_cMatch, "[]", match_aref, -1);
     rb_define_method(rb_cMatch, "captures", match_captures, 0);
+    rb_define_method(rb_cMatch, "named_captures", match_named_captures, 0);
     rb_define_method(rb_cMatch, "values_at", match_values_at, -1);
     rb_define_method(rb_cMatch, "pre_match", rb_reg_match_pre, 0);
     rb_define_method(rb_cMatch, "post_match", rb_reg_match_post, 0);

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

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