diff options
author | Sutou Kouhei <kou@clear-code.com> | 2020-07-19 06:25:05 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2020-07-20 02:32:55 +0900 |
commit | 4fcfa85cb6a44b4e8c7a47578b50064711dff404 (patch) | |
tree | bcbabfa80db2a3cebca9551157b784306d57387c /lib/csv.rb | |
parent | 178649e6dcb679f4b42cbf723d4f34f81a591304 (diff) |
[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
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3332
Diffstat (limited to 'lib/csv.rb')
-rw-r--r-- | lib/csv.rb | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/lib/csv.rb b/lib/csv.rb index 83898890c7..63309f3f2e 100644 --- a/lib/csv.rb +++ b/lib/csv.rb @@ -1289,8 +1289,20 @@ class CSV str = +"" if options[:encoding] str.force_encoding(options[:encoding]) - elsif field = row.find {|f| f.is_a?(String)} - str.force_encoding(field.encoding) + else + 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 (new(str, **options) << row).string end |