diff options
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | encoding.c | 9 | ||||
-rw-r--r-- | string.c | 8 | ||||
-rw-r--r-- | test/ruby/test_utf16.rb | 42 |
4 files changed, 62 insertions, 3 deletions
@@ -1,3 +1,9 @@ +Sat Feb 16 18:00:13 2008 Tanaka Akira <akr@fsij.org> + + * 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@fsij.org> * string.c (rb_enc_strlen): UTF-8 character count moved to str_strlen. diff --git a/encoding.c b/encoding.c index da89d8ac70..335d1ae00c 100644 --- a/encoding.c +++ b/encoding.c @@ -664,8 +664,15 @@ rb_enc_compatible(VALUE str1, VALUE str2) } 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; @@ -597,7 +597,6 @@ rb_enc_strlen(const char *p, const char *e, rb_encoding *enc) 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_enc_cr_str_buf_cat(VALUE str, const char *ptr, long len, 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) { diff --git a/test/ruby/test_utf16.rb b/test/ruby/test_utf16.rb index 6e4fb40161..9074aa7233 100644 --- a/test/ruby/test_utf16.rb +++ b/test/ruby/test_utf16.rb @@ -169,9 +169,49 @@ EOT 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 } |