diff --git a/re.c b/re.c index f8ea30f..21a50ef 100644 --- a/re.c +++ b/re.c @@ -1931,6 +1931,54 @@ match_to_s(VALUE match) return str; } +static int +match_to_h_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 = rb_reg_nth_match(back_refs[back_num-1], match); + + rb_hash_aset(hash, key, value); + return 0; +} + +/* + * call-seq: + * mtch.to_h -> 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 capture of corresponding group. + * + * m = /(?.)(?.)/.match("01") + * m.to_h #=> {"a" => "0", "b" => "1"} + * + * m = /(?.)(?.)?/.match("0") + * m.to_h #=> {"a" => "0", "b" => nil} + * + * m = /(?.)(?.)/.match("01") + * m.to_h #=> {"a" => "1"} + */ + +static VALUE +match_to_h(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_to_h_iter, (void*)memo); + + return hash; +} /* * call-seq: @@ -3750,6 +3798,7 @@ Init_Regexp(void) rb_define_method(rb_cMatch, "pre_match", rb_reg_match_pre, 0); rb_define_method(rb_cMatch, "post_match", rb_reg_match_post, 0); rb_define_method(rb_cMatch, "to_s", match_to_s, 0); + rb_define_method(rb_cMatch, "to_h", match_to_h, 0); rb_define_method(rb_cMatch, "inspect", match_inspect, 0); rb_define_method(rb_cMatch, "string", match_string, 0); rb_define_method(rb_cMatch, "hash", match_hash, 0); diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb index 96b0bfa..9d83d88 100644 --- a/test/ruby/test_regexp.rb +++ b/test/ruby/test_regexp.rb @@ -173,6 +173,15 @@ def test_named_capture_nonascii assert_raise(IndexError, bug9903) {m[key.dup.force_encoding(Encoding::Shift_JIS)]} end + def test_named_capture_to_h + assert_equal({'a' => '1', 'b' => '2', 'c' => nil}, /^(?.)(?.)(?.)?/.match('12').to_h) + assert_equal({'a' => '1', 'b' => '2', 'c' => '3'}, /^(?.)(?.)(?.)?/.match('123').to_h) + assert_equal({'a' => '1', 'b' => '2', 'c' => ''}, /^(?.)(?.)(?.?)/.match('12').to_h) + + assert_equal({'a' => '1', 'b' => '2'}, /^(.)(?.)(?.)/.match('012').to_h) + assert_equal({'a' => '2'}, /^(?.)(?.)/.match('12').to_h) + end + def test_assign_named_capture assert_equal("a", eval('/(?.)/ =~ "a"; foo')) assert_equal("a", eval('foo = 1; /(?.)/ =~ "a"; foo'))