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

ruby-changes:30115

From: naruse <ko1@a...>
Date: Thu, 25 Jul 2013 17:52:50 +0900 (JST)
Subject: [ruby-changes:30115] naruse:r42167 (trunk): * re.c (rb_reg_to_s): convert closing parenthes to the target encoding

naruse	2013-07-25 17:52:32 +0900 (Thu, 25 Jul 2013)

  New Revision: 42167

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

  Log:
    * re.c (rb_reg_to_s): convert closing parenthes to the target encoding
      if it is ASCII incompatible encoding. [ruby-core:56063] [Bug #8650]

  Modified files:
    trunk/ChangeLog
    trunk/re.c
    trunk/test/ruby/test_regexp.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42166)
+++ ChangeLog	(revision 42167)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Jul 25 17:49:42 2013  NARUSE, Yui  <naruse@r...>
+
+	* re.c (rb_reg_to_s): convert closing parenthes to the target encoding
+	  if it is ASCII incompatible encoding. [ruby-core:56063] [Bug #8650]
+
 Thu Jul 25 17:21:21 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* encoding.c (is_obj_encoding): new macro to check if obj is an
Index: re.c
===================================================================
--- re.c	(revision 42166)
+++ re.c	(revision 42167)
@@ -597,8 +597,30 @@ rb_reg_to_s(VALUE re) https://github.com/ruby/ruby/blob/trunk/re.c#L597
     }
 
     rb_str_buf_cat2(str, ":");
-    rb_reg_expr_str(str, (char*)ptr, len, enc, NULL);
-    rb_str_buf_cat2(str, ")");
+    if (rb_enc_asciicompat(enc)) {
+	rb_reg_expr_str(str, (char*)ptr, len, enc, NULL);
+	rb_str_buf_cat2(str, ")");
+    }
+    else {
+	const char *s, *e;
+	char *paren;
+	ptrdiff_t n;
+	rb_str_buf_cat2(str, ")");
+	rb_enc_associate(str, rb_usascii_encoding());
+	str = rb_str_encode(str, rb_enc_from_encoding(enc), 0, Qnil);
+
+	/* backup encoded ")" to paren */
+	s = RSTRING_PTR(str);
+	e = RSTRING_END(str);
+	s = rb_enc_left_char_head(s, e-1, e, enc);
+	n = e - s;
+	paren = ALLOCA_N(char, n);
+	memcpy(paren, s, n);
+	rb_str_resize(str, RSTRING_LEN(str) - n);
+
+	rb_reg_expr_str(str, (char*)ptr, len, enc, NULL);
+	rb_str_buf_cat(str, paren, n);
+    }
     rb_enc_copy(str, re);
 
     OBJ_INFECT(str, re);
Index: test/ruby/test_regexp.rb
===================================================================
--- test/ruby/test_regexp.rb	(revision 42166)
+++ test/ruby/test_regexp.rb	(revision 42167)
@@ -64,6 +64,14 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L64
 
   def test_to_s
     assert_equal '(?-mix:\x00)', Regexp.new("\0").to_s
+
+    str = "abcd\u3042"
+    [:UTF_16BE, :UTF_16LE, :UTF_32BE, :UTF_32LE].each do |es|
+      enc = Encoding.const_get(es)
+      rs = Regexp.new(str.encode(enc)).to_s
+      assert_equal("(?-mix:abcd\u3042)".encode(enc), rs)
+      assert_equal(enc, rs.encoding)
+    end
   end
 
   def test_union

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

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