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

ruby-changes:10969

From: nobu <ko1@a...>
Date: Sun, 22 Feb 2009 22:42:34 +0900 (JST)
Subject: [ruby-changes:10969] Ruby:r22547 (trunk): * string.c (tr_trans): should recalculate coderange.

nobu	2009-02-22 22:42:22 +0900 (Sun, 22 Feb 2009)

  New Revision: 22547

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

  Log:
    * string.c (tr_trans): should recalculate coderange.
      [ruby-core:22326] (reopened at [ruby-core:22328])

  Modified files:
    trunk/ChangeLog
    trunk/string.c
    trunk/test/ruby/test_string.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 22546)
+++ ChangeLog	(revision 22547)
@@ -1,3 +1,8 @@
+Sun Feb 22 22:42:20 2009  Nobuyoshi Nakada  <nobu@r...>
+
+	* string.c (tr_trans): should recalculate coderange.
+	  [ruby-core:22326] (reopened at [ruby-core:22328])
+
 Sun Feb 22 20:09:29 2009  Tanaka Akira  <akr@f...>
 
 	* ext/socket/option.c (inspect_linger): message refined.
Index: string.c
===================================================================
--- string.c	(revision 22546)
+++ string.c	(revision 22547)
@@ -4712,6 +4712,10 @@
     int singlebyte = single_byte_optimizable(str);
     int cr;
 
+#define CHECK_IF_ASCII(c) \
+    (void)((cr == ENC_CODERANGE_7BIT && !rb_isascii(c)) ? \
+	   (cr = ENC_CODERANGE_VALID) : 0)
+
     StringValue(src);
     StringValue(repl);
     if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
@@ -4783,6 +4787,8 @@
 	}
     }
 
+    if (cr == ENC_CODERANGE_VALID)
+	cr = ENC_CODERANGE_7BIT;
     str_modify_keep_cr(str);
     s = RSTRING_PTR(str); send = RSTRING_END(str);
     if (sflag) {
@@ -4813,7 +4819,10 @@
 		c = errc;
 	    }
 	    if (c != -1) {
-		if (save == c) continue;
+		if (save == c) {
+		    CHECK_IF_ASCII(c);
+		    continue;
+		}
 		save = c;
 		tlen = rb_enc_codelen(c, enc);
 		modify = 1;
@@ -4833,6 +4842,7 @@
 	    if (may_modify && memcmp(s, t, tlen) != 0) {
 		modify = 1;
 	    }
+	    CHECK_IF_ASCII(c);
 	    t += tlen;
 	}
 	*t = '\0';
@@ -4855,6 +4865,7 @@
 		    modify = 1;
 		}
 	    }
+	    CHECK_IF_ASCII(c);
 	    s++;
 	}
     }
@@ -4904,6 +4915,7 @@
 		    modify = 1;
 		}
 	    }
+	    CHECK_IF_ASCII(c);
 	    s += clen;
 	    t += tlen;
 	}
@@ -4918,7 +4930,6 @@
     }
     
     if (modify) {
-	cr = ENC_CODERANGE_AND(cr, ENC_CODERANGE(repl));
 	if (cr != ENC_CODERANGE_BROKEN)
 	    ENC_CODERANGE_SET(str, cr);
 	rb_enc_associate(str, enc);
Index: test/ruby/test_string.rb
===================================================================
--- test/ruby/test_string.rb	(revision 22546)
+++ test/ruby/test_string.rb	(revision 22547)
@@ -1432,6 +1432,8 @@
   def test_tr_s
     assert_equal(S("hypo"), S("hello").tr_s(S("el"), S("yp")))
     assert_equal(S("h*o"),  S("hello").tr_s(S("el"), S("*")))
+    assert_equal("a".hash, "\u0101\u0101".tr_s("\u0101", "a").hash)
+    assert_equal(true, "\u3041\u3041".tr("\u3041", "a").ascii_only?)
   end
 
   def test_tr_s!

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

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