Permalink
Browse files

Merge pull request #113 from pck/improve-string-encoding

Improve string encoding
  • Loading branch information...
2 parents 6a3ac11 + ef4f041 commit 4f403bc0f1176e56854f4d994bf622dafaec5ea0 @tenderlove tenderlove committed Jan 6, 2013
Showing with 24 additions and 4 deletions.
  1. +3 −2 lib/psych/visitors/yaml_tree.rb
  2. +21 −2 test/psych/test_string.rb
@@ -221,9 +221,10 @@ def visit_BigDecimal o
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?
@@ -9,6 +9,12 @@ class Y < String
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 @@ def test_empty_subclass
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 @@ def test_string_with_base_60
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 @@ def test_non_binary_string
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"

0 comments on commit 4f403bc

Please sign in to comment.