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

ruby-changes:4016

From: ko1@a...
Date: Sat, 16 Feb 2008 18:02:32 +0900 (JST)
Subject: [ruby-changes:4016] akr - Ruby:r15506 (trunk): * encoding.c (rb_enc_compatible): empty strings are always compatible.

akr	2008-02-16 18:02:12 +0900 (Sat, 16 Feb 2008)

  New Revision: 15506

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

  Log:
    * encoding.c (rb_enc_compatible): empty strings are always compatible.
    
    * string.c (rb_enc_cr_str_buf_cat): ditto.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/string.c?r1=15506&r2=15505&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15506&r2=15505&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_utf16.rb?r1=15506&r2=15505&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/encoding.c?r1=15506&r2=15505&diff_format=u

Index: encoding.c
===================================================================
--- encoding.c	(revision 15505)
+++ encoding.c	(revision 15506)
@@ -664,8 +664,15 @@
     }
     enc1 = rb_enc_from_index(idx1);
     enc2 = rb_enc_from_index(idx2);
-    if (!rb_enc_asciicompat(enc1) || !rb_enc_asciicompat(enc2))
+
+    if (!rb_enc_asciicompat(enc1) || !rb_enc_asciicompat(enc2)) {
+        if (TYPE(str2) == T_STRING && RSTRING_LEN(str2) == 0)
+            return enc1;
+        if (TYPE(str1) == T_STRING && RSTRING_LEN(str1) == 0)
+            return enc2;
 	return 0;
+    }
+
     if (BUILTIN_TYPE(str1) != T_STRING) {
 	VALUE tmp = str1;
 	int idx0 = idx1;
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 15505)
+++ ChangeLog	(revision 15506)
@@ -1,3 +1,9 @@
+Sat Feb 16 18:00:13 2008  Tanaka Akira  <akr@f...>
+
+	* encoding.c (rb_enc_compatible): empty strings are always compatible.
+
+	* string.c (rb_enc_cr_str_buf_cat): ditto.
+	  
 Sat Feb 16 16:14:35 2008  Tanaka Akira  <akr@f...>
 
 	* string.c (rb_enc_strlen): UTF-8 character count moved to str_strlen.
Index: string.c
===================================================================
--- string.c	(revision 15505)
+++ string.c	(revision 15506)
@@ -597,7 +597,6 @@
     if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
         return (e - p) / rb_enc_mbminlen(enc);
     }
-
     else if (rb_enc_asciicompat(enc)) {
         c = 0;
         while (p < e) {
@@ -1303,6 +1302,13 @@
         rb_encoding *str_enc = rb_enc_from_index(str_encindex);
         rb_encoding *ptr_enc = rb_enc_from_index(ptr_encindex);
         if (!rb_enc_asciicompat(str_enc) || !rb_enc_asciicompat(ptr_enc)) {
+            if (len == 0)
+                return str;
+            if (RSTRING_LEN(str) == 0) {
+                rb_str_buf_cat(str, ptr, len);
+                ENCODING_CODERANGE_SET(str, ptr_encindex, ptr_cr);
+                return str;
+            }
             goto incompatible;
         }
 	if (ptr_cr == ENC_CODERANGE_UNKNOWN) {
Index: test/ruby/test_utf16.rb
===================================================================
--- test/ruby/test_utf16.rb	(revision 15505)
+++ test/ruby/test_utf16.rb	(revision 15506)
@@ -169,14 +169,54 @@
     enccall("aa".force_encoding("UTF-16BE"), :slice!, -1)
   end
 
-  def test_concat
+  def test_plus_empty1
     s1 = ""
     s2 = "aa".force_encoding("utf-16be")
+    assert_nothing_raised("#{encdump s1} << #{encdump s2}") {
+      s1 + s2
+    }
+  end
+
+  def test_plus_empty2
+    s1 = "aa"
+    s2 = "".force_encoding("utf-16be")
+    assert_nothing_raised("#{encdump s1} << #{encdump s2}") {
+      s1 + s2
+    }
+  end
+
+  def test_plus_nonempty
+    s1 = "aa"
+    s2 = "bb".force_encoding("utf-16be")
     assert_raise(ArgumentError, "#{encdump s1} << #{encdump s2}") {
+      s1 + s2
+    }
+  end
+
+  def test_concat_empty1
+    s1 = ""
+    s2 = "aa".force_encoding("utf-16be")
+    assert_nothing_raised("#{encdump s1} << #{encdump s2}") {
       s1 << s2
     }
   end
 
+  def test_concat_empty2
+    s1 = "aa"
+    s2 = "".force_encoding("utf-16be")
+    assert_nothing_raised("#{encdump s1} << #{encdump s2}") {
+      s1 << s2
+    }
+  end
+
+  def test_concat_nonempty
+    s1 = "aa"
+    s2 = "bb".force_encoding("utf-16be")
+    assert_raise(ArgumentError, "#{encdump s1} << #{encdump s2}") {
+      s1 << s2
+    }
+  end
+
   def test_chomp
     s = "\1\n".force_encoding("utf-16be")
     assert_str_equal(s, s.chomp, "#{encdump s}.chomp")

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

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