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

ruby-changes:26702

From: tenderlove <ko1@a...>
Date: Thu, 10 Jan 2013 04:26:47 +0900 (JST)
Subject: [ruby-changes:26702] tenderlove:r38753 (trunk): * ext/psych/lib/psych/visitors/yaml_tree.rb: ascii only binary strings

tenderlove	2013-01-10 04:26:36 +0900 (Thu, 10 Jan 2013)

  New Revision: 38753

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

  Log:
    * ext/psych/lib/psych/visitors/yaml_tree.rb: ascii only binary strings
      will be dumped as unicode.  Thanks Paul Kunysch!
    
    * test/psych/test_string.rb: appropriate test

  Modified files:
    trunk/ChangeLog
    trunk/ext/psych/lib/psych/visitors/yaml_tree.rb
    trunk/test/psych/test_string.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38752)
+++ ChangeLog	(revision 38753)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Jan 10 03:38:40 2013  Aaron Patterson <aaron@t...>
+
+	* ext/psych/lib/psych/visitors/yaml_tree.rb: ascii only binary strings
+	  will be dumped as unicode.  Thanks Paul Kunysch!
+
+	* test/psych/test_string.rb: appropriate test
+
 Thu Jan 10 03:29:55 2013  Koichi Sasada  <ko1@a...>
 
 	* compile.c (compile_array_): modify wrong optimization.
Index: ext/psych/lib/psych/visitors/yaml_tree.rb
===================================================================
--- ext/psych/lib/psych/visitors/yaml_tree.rb	(revision 38752)
+++ ext/psych/lib/psych/visitors/yaml_tree.rb	(revision 38753)
@@ -221,9 +221,10 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L221
       end
 
       def binary? string
-        string.encoding == Encoding::ASCII_8BIT ||
+        (string.encoding == Encoding::ASCII_8BIT && !string.ascii_only?) ||
           string.index("\x00") ||
-          string.count("\x00-\x7F", "^ -~\t\r\n").fdiv(string.length) > 0.3
+          string.count("\x00-\x7F", "^ -~\t\r\n").fdiv(string.length) > 0.3 ||
+          string.class != String
       end
       private :binary?
 
Index: test/psych/test_string.rb
===================================================================
--- test/psych/test_string.rb	(revision 38752)
+++ test/psych/test_string.rb	(revision 38753)
@@ -9,6 +9,12 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_string.rb#L9
       attr_accessor :val
     end
 
+    class Z < String
+      def initialize
+        force_encoding Encoding::US_ASCII
+      end
+    end
+
     def test_another_subclass_with_attributes
       y = Psych.load Psych.dump Y.new("foo").tap {|y| y.val = 1}
       assert_equal "foo", y
@@ -28,6 +34,12 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_string.rb#L34
       assert_equal X, x.class
     end
 
+    def test_empty_character_subclass
+      assert_match "!ruby/string:#{Z}", Psych.dump(Z.new)
+      x = Psych.load Psych.dump Z.new
+      assert_equal Z, x.class
+    end
+
     def test_subclass_with_attributes
       y = Psych.load Psych.dump Y.new.tap {|y| y.val = 1}
       assert_equal Y, y.class
@@ -40,8 +52,8 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_string.rb#L52
       assert_equal '01:03:05', Psych.load(yaml)
     end
 
-    def test_tagged_binary_should_be_dumped_as_binary
-      string = "hello world!"
+    def test_nonascii_string_as_binary
+      string = "hello \x80 world!"
       string.force_encoding 'ascii-8bit'
       yml = Psych.dump string
       assert_match(/binary/, yml)
@@ -67,6 +79,13 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_string.rb#L79
       yml = Psych.dump string
       refute_match(/binary/, yml)
       assert_equal string, Psych.load(yml)
+    end
+
+    def test_ascii_only_8bit_string
+      string = "abc".encode(Encoding::ASCII_8BIT)
+      yml = Psych.dump string
+      refute_match(/binary/, yml)
+      assert_equal string, Psych.load(yml)
     end
 
     def test_string_with_ivars

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

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