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

ruby-changes:3815

From: ko1@a...
Date: Tue, 29 Jan 2008 03:04:56 +0900 (JST)
Subject: [ruby-changes:3815] usa - Ruby:r15304 (trunk): * parse.y (reg_fragment_setenc_gen): US-ASCII script special code.

usa	2008-01-29 03:04:39 +0900 (Tue, 29 Jan 2008)

  New Revision: 15304

  Modified files:
    trunk/ChangeLog
    trunk/parse.y
    trunk/test/ruby/test_m17n.rb

  Log:
    * parse.y (reg_fragment_setenc_gen): US-ASCII script special code.
    
    * parse.y (reg_fragment_check_len, reg_compile_gen): no need such
      trick.
      [ruby-dev:33399]
    
    * test/ruby/test_m17n.rb (test_regexp_usacii_literal): add tests.
    


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/parse.y?r1=15304&r2=15303&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15304&r2=15303&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_m17n.rb?r1=15304&r2=15303&diff_format=u

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 15303)
+++ ChangeLog	(revision 15304)
@@ -1,3 +1,13 @@
+Tue Jan 29 03:01:29 2008  NAKAMURA Usaku  <usa@r...>
+
+	* parse.y (reg_fragment_setenc_gen): US-ASCII script special code.
+
+	* parse.y (reg_fragment_check_len, reg_compile_gen): no need such
+	  trick.
+	  [ruby-dev:33399]
+
+	* test/ruby/test_m17n.rb (test_regexp_usacii_literal): add tests.
+
 Tue Jan 29 01:38:02 2008  NAKAMURA Usaku  <usa@r...>
 
 	* common.mk ($(srcdir)/revision.h): no need to show ifchange execution
Index: parse.y
===================================================================
--- parse.y	(revision 15303)
+++ parse.y	(revision 15304)
@@ -8501,6 +8501,15 @@
         }
 	rb_enc_associate(str, rb_ascii8bit_encoding());
     }
+    else if (parser->enc == rb_usascii_encoding()) {
+	if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
+	    /* raise in re.c */
+	    rb_enc_associate(str, rb_usascii_encoding());
+	}
+	else {
+	    rb_enc_associate(str, rb_ascii8bit_encoding());
+	}
+    }
     return;
 
 error:
@@ -8513,10 +8522,6 @@
 reg_fragment_check_gen(struct parser_params* parser, VALUE str, int options)
 {
     VALUE err;
-    if (!RE_OPTION_ENCODING_IDX(options) &&
-	parser->enc == rb_usascii_encoding()) {
-	options |= RE_OPTION_ARG_ENCODING_NONE;
-    }
     reg_fragment_setenc_gen(parser, str, options);
     err = rb_reg_check_preprocess(str);
     if (err != Qnil) {
@@ -8610,10 +8615,6 @@
 {
     VALUE re;
 
-    if (!RE_OPTION_ENCODING_IDX(options) &&
-	parser->enc == rb_usascii_encoding()) {
-	options |= RE_OPTION_ARG_ENCODING_NONE;
-    }
     reg_fragment_setenc(str, options);
     re = rb_reg_compile(str, options & RE_OPTION_MASK);
     if (NIL_P(re)) {
Index: test/ruby/test_m17n.rb
===================================================================
--- test/ruby/test_m17n.rb	(revision 15303)
+++ test/ruby/test_m17n.rb	(revision 15304)
@@ -78,6 +78,15 @@
     assert_regexp_fixed_encoding(r)
   end
 
+  def assert_regexp_usascii_literal(r, enc, ex = nil)
+    code = "# -*- encoding: US-ASCII -*-\n#{r}.encoding"
+    if ex
+      assert_raise(ex) { eval(code) }
+    else
+      assert_equal(enc, eval(code))
+    end
+  end
+
   def encdump(str)
     d = str.dump
     if /\.force_encoding\("[A-Za-z0-9.:_+-]*"\)\z/ =~ d
@@ -1045,4 +1054,32 @@
     assert_equal(Encoding::US_ASCII, eval("# -*- encoding: US-ASCII -*-\n__ENCODING__".force_encoding("ASCII-8BIT")))
     assert_equal(Encoding::ASCII_8BIT, eval("# -*- encoding: ASCII-8BIT -*-\n__ENCODING__".force_encoding("US-ASCII")))
   end
+
+  def test_regexp_usascii
+    assert_regexp_usascii_literal('//', Encoding::US_ASCII)
+    assert_regexp_usascii_literal('/#{}/', Encoding::US_ASCII)
+    assert_regexp_usascii_literal('/#{"a"}/', Encoding::US_ASCII)
+    assert_regexp_usascii_literal('/#{%q"\x80"}/', Encoding::ASCII_8BIT)
+    assert_regexp_usascii_literal('/#{"\x80"}/', nil, SyntaxError)
+
+    assert_regexp_usascii_literal('/a/', Encoding::US_ASCII)
+    assert_regexp_usascii_literal('/a#{}/', Encoding::US_ASCII)
+    assert_regexp_usascii_literal('/a#{"a"}/', Encoding::US_ASCII)
+    assert_regexp_usascii_literal('/a#{%q"\x80"}/', Encoding::ASCII_8BIT)
+    assert_regexp_usascii_literal('/a#{"\x80"}/', nil, SyntaxError)
+
+    assert_regexp_usascii_literal('/\x80/', Encoding::ASCII_8BIT)
+    assert_regexp_usascii_literal('/\x80#{}/', Encoding::ASCII_8BIT)
+    assert_regexp_usascii_literal('/\x80#{"a"}/', Encoding::ASCII_8BIT)
+    assert_regexp_usascii_literal('/\x80#{%q"\x80"}/', Encoding::ASCII_8BIT)
+    assert_regexp_usascii_literal('/\x80#{"\x80"}/', nil, SyntaxError)
+
+    assert_regexp_usascii_literal('/\u1234/', Encoding::UTF_8)
+    assert_regexp_usascii_literal('/\u1234#{}/', Encoding::UTF_8)
+    assert_regexp_usascii_literal('/\u1234#{"a"}/', Encoding::UTF_8)
+    assert_regexp_usascii_literal('/\u1234#{%q"\x80"}/', nil, SyntaxError)
+    assert_regexp_usascii_literal('/\u1234#{"\x80"}/', nil, SyntaxError)
+    assert_regexp_usascii_literal('/\u1234\x80/', nil, SyntaxError)
+    assert_regexp_usascii_literal('/\u1234#{}\x80/', nil, RegexpError)
+  end
 end

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

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