diff options
author | tenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-09-01 19:07:44 +0000 |
---|---|---|
committer | tenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-09-01 19:07:44 +0000 |
commit | 8659de2e0f2ee5401d0486d7efaeb7c831564ac1 (patch) | |
tree | bd27fbb320a91989eacdd9dcd1e0d9cc354d0621 /ext | |
parent | 6e6feaf8da3a0d1f3179aba199221aa375ee9269 (diff) | |
download | ruby-8659de2e0f2ee5401d0486d7efaeb7c831564ac1.tar.gz |
* 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
Diffstat (limited to 'ext')
-rw-r--r-- | ext/psych/lib/psych/visitors/yaml_tree.rb | 9 |
1 files changed, 8 insertions, 1 deletions
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' |