[ruby/csv] CSV.generate_line: use the encoding of the first non ASCII field as the expected encoding
See also: https://github.com/ruby/stringio/issues/13#issuecomment-660543554 https://github.com/ruby/csv/commit/004cf49d18
This commit is contained in:
parent
178649e6dc
commit
4fcfa85cb6
Notes:
git
2020-07-20 03:35:31 +09:00
16
lib/csv.rb
16
lib/csv.rb
@ -1289,8 +1289,20 @@ class CSV
|
|||||||
str = +""
|
str = +""
|
||||||
if options[:encoding]
|
if options[:encoding]
|
||||||
str.force_encoding(options[:encoding])
|
str.force_encoding(options[:encoding])
|
||||||
elsif field = row.find {|f| f.is_a?(String)}
|
else
|
||||||
str.force_encoding(field.encoding)
|
fallback_encoding = nil
|
||||||
|
output_encoding = nil
|
||||||
|
row.each do |field|
|
||||||
|
next unless field.is_a?(String)
|
||||||
|
fallback_encoding ||= field.encoding
|
||||||
|
next if field.ascii_only?
|
||||||
|
output_encoding = field.encoding
|
||||||
|
break
|
||||||
|
end
|
||||||
|
output_encoding ||= fallback_encoding
|
||||||
|
if output_encoding
|
||||||
|
str.force_encoding(output_encoding)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
(new(str, **options) << row).string
|
(new(str, **options) << row).string
|
||||||
end
|
end
|
||||||
|
@ -242,6 +242,18 @@ class TestCSVEncodings < Test::Unit::TestCase
|
|||||||
assert_equal("UTF-8", data.to_csv.encoding.name)
|
assert_equal("UTF-8", data.to_csv.encoding.name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_encoding_is_not_upgraded_for_non_ascii_content_during_writing_as_needed
|
||||||
|
data = ["\u00c0".encode("ISO-8859-1"), "\u3042"]
|
||||||
|
assert_equal([
|
||||||
|
"ISO-8859-1",
|
||||||
|
"UTF-8",
|
||||||
|
],
|
||||||
|
data.collect {|field| field.encoding.name})
|
||||||
|
assert_raise(Encoding::CompatibilityError) do
|
||||||
|
data.to_csv
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_explicit_encoding
|
def test_explicit_encoding
|
||||||
bug9766 = '[ruby-core:62113] [Bug #9766]'
|
bug9766 = '[ruby-core:62113] [Bug #9766]'
|
||||||
s = CSV.generate(encoding: "Windows-31J") do |csv|
|
s = CSV.generate(encoding: "Windows-31J") do |csv|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user