From 8659de2e0f2ee5401d0486d7efaeb7c831564ac1 Mon Sep 17 00:00:00 2001 From: tenderlove Date: Thu, 1 Sep 2011 19:07:44 +0000 Subject: * 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33160 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ ext/psych/lib/psych/visitors/yaml_tree.rb | 9 ++++++++- test/psych/test_string.rb | 8 ++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 0a1909ee1d..6ee79fd8c0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Fri Sep 2 04:05:25 2011 Aaron Patterson + + * 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 * numeric.c (flo_round): substitute machine dependent magic number. diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb index eef6125ff1..70db181a95 100644 --- a/ext/psych/lib/psych/visitors/yaml_tree.rb +++ b/ext/psych/lib/psych/visitors/yaml_tree.rb @@ -214,12 +214,19 @@ module Psych 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' diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb index 96d77e0f42..51f1280abf 100644 --- a/test/psych/test_string.rb +++ b/test/psych/test_string.rb @@ -2,6 +2,14 @@ require 'psych/helper' 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 -- cgit v1.2.3