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

ruby-changes:34265

From: nobu <ko1@a...>
Date: Wed, 4 Jun 2014 22:10:03 +0900 (JST)
Subject: [ruby-changes:34265] nobu:r46346 (trunk): re.c: consider name encoding

nobu	2014-06-04 22:09:57 +0900 (Wed, 04 Jun 2014)

  New Revision: 46346

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

  Log:
    re.c: consider name encoding
    
    * re.c (match_aref, rb_reg_regsub): consider encoding of captured
      names, encoding-incompatible should not match.
      [ruby-dev:48278] [Bug #9903]

  Modified files:
    trunk/ChangeLog
    trunk/re.c
    trunk/test/ruby/test_regexp.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46345)
+++ ChangeLog	(revision 46346)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Jun  4 22:09:53 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* re.c (match_aref, rb_reg_regsub): consider encoding of captured
+	  names, encoding-incompatible should not match.
+	  [ruby-dev:48278] [Bug #9903]
+
 Wed Jun  4 21:23:52 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* re.c (match_aref): should not ignore name after NUL byte.
Index: re.c
===================================================================
--- re.c	(revision 46345)
+++ re.c	(revision 46346)
@@ -1796,9 +1796,9 @@ match_aref(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/re.c#L1796
 		/* fall through */
 	      case T_STRING:
 		p = StringValuePtr(idx);
-		num = name_to_backref_number(RMATCH_REGS(match),
-					     RMATCH(match)->regexp, p, p + RSTRING_LEN(idx));
-		if (num < 1) {
+		if (!rb_enc_compatible(RREGEXP(RMATCH(match)->regexp)->src, idx) ||
+		    (num = name_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp,
+						  p, p + RSTRING_LEN(idx))) < 1) {
 		    name_to_backref_error(idx);
 		}
 		return rb_reg_nth_match(num, match);
@@ -3417,9 +3417,10 @@ rb_reg_regsub(VALUE str, VALUE src, stru https://github.com/ruby/ruby/blob/trunk/re.c#L3417
                     name_end += c == -1 ? mbclen(name_end, e, str_enc) : clen;
                 }
                 if (name_end < e) {
-                    no = name_to_backref_number(regs, regexp, name, name_end);
-		    if (no < 1) {
-			VALUE n = rb_str_subseq(str, (long)(name - RSTRING_PTR(str)), (long)(name_end - name));
+		    VALUE n = rb_str_subseq(str, (long)(name - RSTRING_PTR(str)),
+					    (long)(name_end - name));
+		    if (!rb_enc_compatible(RREGEXP(regexp)->src, n) ||
+			(no = name_to_backref_number(regs, regexp, name, name_end)) < 1) {
 			name_to_backref_error(n);
 		    }
                     p = s = name_end + clen;
Index: test/ruby/test_regexp.rb
===================================================================
--- test/ruby/test_regexp.rb	(revision 46345)
+++ test/ruby/test_regexp.rb	(revision 46346)
@@ -158,6 +158,15 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L158
     }
   end
 
+  def test_named_capture_nonascii
+    bug9903 = '[ruby-dev:48278] [Bug #9903]'
+
+    key = "\xb1\xb2".force_encoding(Encoding::EUC_JP)
+    m = /(?<#{key}>.*)/.match("xxx")
+    assert_equal("xxx", m[key])
+    assert_raise(IndexError, bug9903) {m[key.dup.force_encoding(Encoding::Shift_JIS)]}
+  end
+
   def test_assign_named_capture
     assert_equal("a", eval('/(?<foo>.)/ =~ "a"; foo'))
     assert_equal("a", eval('foo = 1; /(?<foo>.)/ =~ "a"; foo'))

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

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