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

ruby-changes:21111

From: tenderlove <ko1@a...>
Date: Fri, 2 Sep 2011 04:07:54 +0900 (JST)
Subject: [ruby-changes:21111] tenderlove:r33160 (trunk): * ext/psych/lib/psych/visitors/yaml_tree.rb: emit strings tagged as

tenderlove	2011-09-02 04:07:44 +0900 (Fri, 02 Sep 2011)

  New Revision: 33160

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

  Log:
    * ext/psych/lib/psych/visitors/yaml_tree.rb: emit strings tagged as
      ascii-8bit as binary in YAML.
    * test/psych/test_string.rb: corresponding test.

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 33159)
+++ ChangeLog	(revision 33160)
@@ -1,3 +1,9 @@
+Fri Sep  2 04:05:25 2011  Aaron Patterson <aaron@t...>
+
+	* ext/psych/lib/psych/visitors/yaml_tree.rb: emit strings tagged as
+	  ascii-8bit as binary in YAML.
+	* test/psych/test_string.rb: corresponding test.
+
 Fri Sep  2 01:07:14 2011  Nobuyoshi Nakada  <nobu@r...>
 
 	* numeric.c (flo_round): substitute machine dependent magic number.
Index: ext/psych/lib/psych/visitors/yaml_tree.rb
===================================================================
--- ext/psych/lib/psych/visitors/yaml_tree.rb	(revision 33159)
+++ ext/psych/lib/psych/visitors/yaml_tree.rb	(revision 33160)
@@ -214,12 +214,19 @@
         end
       end
 
+      def binary? string
+        string.encoding == Encoding::ASCII_8BIT ||
+          string.index("\x00") ||
+          string.count("\x00-\x7F", "^ -~\t\r\n").fdiv(string.length) > 0.3
+      end
+      private :binary?
+
       def visit_String o
         plain = false
         quote = false
         style = Nodes::Scalar::ANY
 
-        if o.index("\x00") || o.count("\x00-\x7F", "^ -~\t\r\n").fdiv(o.length) > 0.3
+        if binary?(o)
           str   = [o].pack('m').chomp
           tag   = '!binary' # FIXME: change to below when syck is removed
           #tag   = 'tag:yaml.org,2002:binary'
Index: test/psych/test_string.rb
===================================================================
--- test/psych/test_string.rb	(revision 33159)
+++ test/psych/test_string.rb	(revision 33160)
@@ -2,6 +2,14 @@
 
 module Psych
   class TestString < TestCase
+    def test_tagged_binary_should_be_dumped_as_binary
+      string = "hello world!"
+      string.force_encoding 'ascii-8bit'
+      yml = Psych.dump string
+      assert_match(/binary/, yml)
+      assert_equal string, Psych.load(yml)
+    end
+
     def test_binary_string_null
       string = "\x00"
       yml = Psych.dump string

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

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