Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/psych/visitors/yaml_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ def visit_String o
style = Nodes::Scalar::PLAIN
tag = nil

# the checks below match ASCII regexps, which UTF-16/32 strings cannot match against
o = o.encode(Encoding::UTF_8) unless o.encoding.ascii_compatible?

if binary?(o)
o = [o].pack('m0')
tag = '!binary' # FIXME: change to below when syck is removed
Expand Down
9 changes: 9 additions & 0 deletions test/psych/test_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ def test_transcode_utf16be
assert_equal str, loaded
end

def test_dump_non_ascii_compatible_encoding
['UTF-16LE', 'UTF-16BE', 'UTF-32LE', 'UTF-32BE'].each do |encoding|
['', 'hello', "multi\nline", 'こんにちは!', 'yes', '<<', '0123'].each do |str|
assert_equal Psych.dump(str), Psych.dump(str.encode(encoding)),
"#{str.inspect} in #{encoding} should dump as it does in UTF-8"
end
end
end

def test_io_shiftjis
Tempfile.create(['shiftjis', 'yml'], :encoding => 'SHIFT_JIS') {|t|
t.write '--- こんにちは!'
Expand Down