ruby-changes:18852
From: naruse <ko1@a...>
Date: Tue, 15 Feb 2011 16:08:24 +0900 (JST)
Subject: [ruby-changes:18852] Ruby:r30877 (trunk): * encoding.c (rb_enc_compatible): change the rule for empty strings:
naruse 2011-02-15 16:08:15 +0900 (Tue, 15 Feb 2011) New Revision: 30877 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=30877 Log: * encoding.c (rb_enc_compatible): change the rule for empty strings: remove the special treatment of the US-ASCII encoded empty string. Now Encoding.compatible? usually respect the encoding of the receiver. Modified files: trunk/ChangeLog trunk/encoding.c trunk/test/ruby/test_m17n.rb trunk/test/ruby/test_m17n_comb.rb Index: encoding.c =================================================================== --- encoding.c (revision 30876) +++ encoding.c (revision 30877) @@ -754,10 +754,10 @@ enc1 = rb_enc_from_index(idx1); enc2 = rb_enc_from_index(idx2); - if (TYPE(str2) == T_STRING && RSTRING_LEN(str2) == 0) - return (idx1 == ENCINDEX_US_ASCII && rb_enc_asciicompat(enc2)) ? enc2 : enc1; - if (TYPE(str1) == T_STRING && RSTRING_LEN(str1) == 0) - return (idx2 == ENCINDEX_US_ASCII && rb_enc_asciicompat(enc1)) ? enc1 : enc2; + if (BUILTIN_TYPE(str2) == T_STRING && RSTRING_LEN(str2) == 0) + return enc1; + if (BUILTIN_TYPE(str1) == T_STRING && RSTRING_LEN(str1) == 0) + return (rb_enc_asciicompat(enc1) && rb_enc_str_asciionly_p(str2)) ? enc1 : enc2; if (!rb_enc_asciicompat(enc1) || !rb_enc_asciicompat(enc2)) { return 0; } Index: ChangeLog =================================================================== --- ChangeLog (revision 30876) +++ ChangeLog (revision 30877) @@ -1,3 +1,10 @@ +Tue Feb 15 15:43:29 2011 NARUSE, Yui <naruse@r...> + + * encoding.c (rb_enc_compatible): change the rule for empty strings: + remove the special treatment of the US-ASCII encoded empty string. + Now Encoding.compatible? usually respect the encoding of the + receiver. + Tue Feb 15 15:39:37 2011 NARUSE, Yui <naruse@r...> * string.c (rb_enc_cr_str_buf_cat): remove special treatment of Index: test/ruby/test_m17n.rb =================================================================== --- test/ruby/test_m17n.rb (revision 30876) +++ test/ruby/test_m17n.rb (revision 30877) @@ -811,15 +811,15 @@ end def test_sprintf_p - enc = "".inspect.encoding - asc = Encoding::US_ASCII Encoding.list.each do |e| format = "%p".force_encoding(e) ['', 'a', "\xC2\xA1", "\x00"].each do |s| s.force_encoding(e) - assert_strenc(s.inspect, e.ascii_compatible? && enc == asc ? e : enc, format % s) + enc = (''.force_encoding(e) + s.inspect).encoding + assert_strenc(s.inspect, enc, format % s) end s = "\xC2\xA1".force_encoding(e) + enc = ('' + s.inspect).encoding assert_strenc('%10s' % s.inspect, enc, "%10p" % s) end end @@ -1086,7 +1086,6 @@ assert_equal(false, s.ascii_only?, "[ruby-core:14566] reported by Sam Ruby") s = "abc".force_encoding(Encoding::ASCII_8BIT) - t = s.gsub(/b/, "\xa1\xa1".force_encoding("euc-jp")) assert_equal(Encoding::ASCII_8BIT, s.encoding) assert_raise(Encoding::CompatibilityError) { @@ -1400,7 +1399,7 @@ Encoding.list.each do |enc| next if enc.dummy? strs = strings.map {|s| s.encode(enc)} rescue next - yield *strs + yield(*strs) end end end Index: test/ruby/test_m17n_comb.rb =================================================================== --- test/ruby/test_m17n_comb.rb (revision 30876) +++ test/ruby/test_m17n_comb.rb (revision 30877) @@ -286,7 +286,7 @@ assert_strenc(a(s), s.encoding, "%s".force_encoding(s.encoding) % s) if !s.empty? # xxx t = enccall(a("%s"), :%, s) - assert_strenc(a(s), s.encoding, t) + assert_strenc(a(s), (a('')+s).encoding, t) end } end @@ -633,13 +633,9 @@ def test_str_casecmp combination(STRINGS, STRINGS) {|s1, s2| #puts "#{encdump(s1)}.casecmp(#{encdump(s2)})" - begin - r = s1.casecmp(s2) - rescue ArgumentError - assert(!s1.valid_encoding? || !s2.valid_encoding?) - next - end - #assert_equal(s1.upcase <=> s2.upcase, r) + next unless s1.valid_encoding? && s2.valid_encoding? && Encoding.compatible?(s1, s2) + r = s1.casecmp(s2) + assert_equal(s1.upcase <=> s2.upcase, r) } end @@ -1031,7 +1027,7 @@ t1.insert(nth, s2) slen = s2.length assert_equal(t1[nth-slen+1,slen], s2, "t=#{encdump s1}; t.insert(#{nth},#{encdump s2}); t") - rescue Encoding::CompatibilityError, IndexError => e + rescue Encoding::CompatibilityError, IndexError end } end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/