summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-01-09 19:26:36 +0000
committertenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-01-09 19:26:36 +0000
commitac8b1869c0b80fd948a6a50d2a737123b5fa5bd2 (patch)
tree0c9d08c05d1b0a6c2c2c874268cc0d5a14589b3d
parent113c81219de64c9e7d40b113b6a6d98d882e4d9f (diff)
* 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 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38753 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog7
-rw-r--r--ext/psych/lib/psych/visitors/yaml_tree.rb5
-rw-r--r--test/psych/test_string.rb23
3 files changed, 31 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index fcdd479..1975d63 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Thu Jan 10 03:38:40 2013 Aaron Patterson <aaron@tenderlovemaking.com>
+
+ * 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@atdot.net>
* compile.c (compile_array_): modify wrong optimization.
diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb
index 5efb654..ce40a17 100644
--- a/ext/psych/lib/psych/visitors/yaml_tree.rb
+++ b/ext/psych/lib/psych/visitors/yaml_tree.rb
@@ -221,9 +221,10 @@ module Psych
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?
diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb
index 77aefc6..0c5d4d2 100644
--- a/test/psych/test_string.rb
+++ b/test/psych/test_string.rb
@@ -9,6 +9,12 @@ module Psych
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
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
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)
@@ -69,6 +81,13 @@ module Psych
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
food = "is delicious"
ivar = "on rock and roll"