ruby-changes:41537
From: naruse <ko1@a...>
Date: Fri, 22 Jan 2016 10:43:25 +0900 (JST)
Subject: [ruby-changes:41537] naruse:r53610 (trunk): * regparse.c (fetch_name_with_level): allow non word characters
naruse 2016-01-22 01:09:09 +0900 (Fri, 22 Jan 2016) New Revision: 53610 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53610 Log: * regparse.c (fetch_name_with_level): allow non word characters at the first character. [Feature #11949] * regparse.c (fetch_name): ditto. Modified files: trunk/ChangeLog trunk/regparse.c trunk/test/ruby/test_regexp.rb Index: test/ruby/test_regexp.rb =================================================================== --- test/ruby/test_regexp.rb (revision 53609) +++ test/ruby/test_regexp.rb (revision 53610) @@ -142,6 +142,8 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L142 assert_equal("a[b]c", "abc".sub(/(?<x>[bc])/, "[\\k<x>]")) assert_equal("o", "foo"[/(?<bar>o)/, "bar"]) + assert_equal("o", "foo"[/(?<@bar>o)/, "@bar"]) + assert_equal("o", "foo"[/(?<@bar>.)\g<@bar>\k<@bar>/, "@bar"]) s = "foo" s[/(?<bar>o)/, "bar"] = "baz" @@ -175,6 +177,7 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L177 def test_assign_named_capture assert_equal("a", eval('/(?<foo>.)/ =~ "a"; foo')) + assert_equal(nil, eval('/(?<@foo>.)/ =~ "a"; defined?(@foo)')) assert_equal("a", eval('foo = 1; /(?<foo>.)/ =~ "a"; foo')) assert_equal("a", eval('1.times {|foo| /(?<foo>.)/ =~ "a"; break foo }')) assert_nothing_raised { eval('/(?<Foo>.)/ =~ "a"') } @@ -939,6 +942,10 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L942 h = {a => 42} assert_equal(42, h[b], '[ruby-core:24748]') assert_match(/#<TestRegexp::MatchData_\u{3042}:/, MatchData_A.allocate.inspect) + + h = /^(?<@time>\d+): (?<body>.*)/.match("123456: hoge fuga") + assert_equal("123456", h["@time"]) + assert_equal("hoge fuga", h["body"]) end def test_regexp_poped Index: ChangeLog =================================================================== --- ChangeLog (revision 53609) +++ ChangeLog (revision 53610) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Jan 22 00:25:57 2016 NARUSE, Yui <naruse@r...> + + * regparse.c (fetch_name_with_level): allow non word characters + at the first character. [Feature #11949] + + * regparse.c (fetch_name): ditto. + Thu Jan 21 17:34:01 2016 NARUSE, Yui <naruse@r...> * marshal.c (r_object0): honor Marshal.load post proc Index: regparse.c =================================================================== --- regparse.c (revision 53609) +++ regparse.c (revision 53610) @@ -2617,6 +2617,7 @@ get_name_end_code_point(OnigCodePoint st https://github.com/ruby/ruby/blob/trunk/regparse.c#L2617 } #ifdef USE_NAMED_GROUP +#define ONIGENC_IS_CODE_NAME(enc, c) TRUE #ifdef USE_BACKREF_WITH_LEVEL /* \k<name+n>, \k<name-n> @@ -2662,7 +2663,7 @@ fetch_name_with_level(OnigCodePoint star https://github.com/ruby/ruby/blob/trunk/regparse.c#L2663 sign = -1; pnum_head = p; } - else if (!ONIGENC_IS_CODE_WORD(enc, c)) { + else if (!ONIGENC_IS_CODE_NAME(enc, c)) { r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME; } } @@ -2684,7 +2685,7 @@ fetch_name_with_level(OnigCodePoint star https://github.com/ruby/ruby/blob/trunk/regparse.c#L2685 is_num = 0; } } - else if (!ONIGENC_IS_CODE_WORD(enc, c)) { + else if (!ONIGENC_IS_CODE_NAME(enc, c)) { r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME; } } @@ -2785,7 +2786,7 @@ fetch_name(OnigCodePoint start_code, UCh https://github.com/ruby/ruby/blob/trunk/regparse.c#L2786 is_num = 0; } } - else if (!ONIGENC_IS_CODE_WORD(enc, c)) { + else if (!ONIGENC_IS_CODE_NAME(enc, c)) { r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME; } } @@ -2795,8 +2796,11 @@ fetch_name(OnigCodePoint start_code, UCh https://github.com/ruby/ruby/blob/trunk/regparse.c#L2796 name_end = p; PFETCH_S(c); if (c == end_code || c == ')') { - if (is_num == 2) r = ONIGERR_INVALID_GROUP_NAME; - break; + if (is_num == 2) { + r = ONIGERR_INVALID_GROUP_NAME; + goto teardown; + } + break; } if (is_num != 0) { @@ -2808,12 +2812,13 @@ fetch_name(OnigCodePoint start_code, UCh https://github.com/ruby/ruby/blob/trunk/regparse.c#L2812 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME; else r = ONIGERR_INVALID_GROUP_NAME; - is_num = 0; + goto teardown; } } else { - if (!ONIGENC_IS_CODE_WORD(enc, c)) { + if (!ONIGENC_IS_CODE_NAME(enc, c)) { r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME; + goto teardown; } } } @@ -2821,6 +2826,7 @@ fetch_name(OnigCodePoint start_code, UCh https://github.com/ruby/ruby/blob/trunk/regparse.c#L2826 if (c != end_code) { r = ONIGERR_INVALID_GROUP_NAME; name_end = end; + goto err; } if (is_num != 0) { @@ -2839,6 +2845,7 @@ fetch_name(OnigCodePoint start_code, UCh https://github.com/ruby/ruby/blob/trunk/regparse.c#L2845 return 0; } else { + teardown: while (!PEND) { name_end = p; PFETCH_S(c); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/