diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2017-06-29 15:03:15 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2017-06-29 15:03:15 -0700 |
commit | 8949a47b8cee31e03e21608406ba116adcf74054 (patch) | |
tree | f82f2abc044b5e59bc4a8d01177c615b6f9eb42b | |
parent | 0ecb87ce7169f945c9fce55ef3f1b9d859283a73 (diff) | |
download | psych-8949a47b8cee31e03e21608406ba116adcf74054.tar.gz |
Rely on encoding tags to determine if string should be dumped as binary
If a string is tagged as binary, then we should dump it as binary.
Fixes #278
-rw-r--r-- | lib/psych/visitors/yaml_tree.rb | 8 | ||||
-rw-r--r-- | test/psych/test_scalar.rb | 5 | ||||
-rw-r--r-- | test/psych/test_string.rb | 10 | ||||
-rw-r--r-- | test/psych/visitors/test_yaml_tree.rb | 2 |
4 files changed, 12 insertions, 13 deletions
diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb index e197a30..03fdc8b 100644 --- a/lib/psych/visitors/yaml_tree.rb +++ b/lib/psych/visitors/yaml_tree.rb @@ -411,15 +411,9 @@ module Psych end private - # FIXME: Remove the index and count checks in Psych 3.0 - NULL = "\x00" - BINARY_RANGE = "\x00-\x7F" - WS_RANGE = "^ -~\t\r\n" def binary? string - (string.encoding == Encoding::ASCII_8BIT && !string.ascii_only?) || - string.index(NULL) || - string.count(BINARY_RANGE, WS_RANGE).fdiv(string.length) > 0.3 + string.encoding == Encoding::ASCII_8BIT && !string.ascii_only? end def visit_array_subclass o diff --git a/test/psych/test_scalar.rb b/test/psych/test_scalar.rb index 4353ec3..2435a2e 100644 --- a/test/psych/test_scalar.rb +++ b/test/psych/test_scalar.rb @@ -8,5 +8,10 @@ module Psych def test_utf_8 assert_equal "日本語", Psych.load("--- 日本語") end + + def test_some_bytes # Ticket #278 + x = "\xEF\xBF\xBD\x1F" + assert_cycle x + end end end diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb index 4aa6016..4739147 100644 --- a/test/psych/test_string.rb +++ b/test/psych/test_string.rb @@ -174,7 +174,7 @@ string: &70121654388580 !ruby/string end def test_binary_string_null - string = "\x00" + string = "\x00\x92".b yml = Psych.dump string assert_match(/binary/, yml) assert_equal string, Psych.load(yml) @@ -187,8 +187,8 @@ string: &70121654388580 !ruby/string assert_equal string, Psych.load(yml) end - def test_non_binary_string - string = binary_string(0.29) + def test_ascii_only_binary_string + string = "non bnry string".b yml = Psych.dump string refute_match(/binary/, yml) assert_equal string, Psych.load(yml) @@ -220,9 +220,9 @@ string: &70121654388580 !ruby/string end def binary_string percentage = 0.31, length = 100 - string = '' + string = ''.b (percentage * length).to_i.times do |i| - string << "\b" + string << "\x92".b end string << 'a' * (length - string.length) string diff --git a/test/psych/visitors/test_yaml_tree.rb b/test/psych/visitors/test_yaml_tree.rb index 8fc18f2..0ee789b 100644 --- a/test/psych/visitors/test_yaml_tree.rb +++ b/test/psych/visitors/test_yaml_tree.rb @@ -37,7 +37,7 @@ module Psych end def test_binary_formatting - gif = "GIF89a\f\x00\f\x00\x84\x00\x00\xFF\xFF\xF7\xF5\xF5\xEE\xE9\xE9\xE5fff\x00\x00\x00\xE7\xE7\xE7^^^\xF3\xF3\xED\x8E\x8E\x8E\xE0\xE0\xE0\x9F\x9F\x9F\x93\x93\x93\xA7\xA7\xA7\x9E\x9E\x9Eiiiccc\xA3\xA3\xA3\x84\x84\x84\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9!\xFE\x0EMade with GIMP\x00,\x00\x00\x00\x00\f\x00\f\x00\x00\x05, \x8E\x810\x9E\xE3@\x14\xE8i\x10\xC4\xD1\x8A\b\x1C\xCF\x80M$z\xEF\xFF0\x85p\xB8\xB01f\r\e\xCE\x01\xC3\x01\x1E\x10' \x82\n\x01\x00;" + gif = "GIF89a\f\x00\f\x00\x84\x00\x00\xFF\xFF\xF7\xF5\xF5\xEE\xE9\xE9\xE5fff\x00\x00\x00\xE7\xE7\xE7^^^\xF3\xF3\xED\x8E\x8E\x8E\xE0\xE0\xE0\x9F\x9F\x9F\x93\x93\x93\xA7\xA7\xA7\x9E\x9E\x9Eiiiccc\xA3\xA3\xA3\x84\x84\x84\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9!\xFE\x0EMade with GIMP\x00,\x00\x00\x00\x00\f\x00\f\x00\x00\x05, \x8E\x810\x9E\xE3@\x14\xE8i\x10\xC4\xD1\x8A\b\x1C\xCF\x80M$z\xEF\xFF0\x85p\xB8\xB01f\r\e\xCE\x01\xC3\x01\x1E\x10' \x82\n\x01\x00;".b @v << gif scalar = @v.tree.children.first.children.first assert_equal Psych::Nodes::Scalar::LITERAL, scalar.style |