Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding::CompatibilityError when csv in UTF-16 format #17

Open
makarychev opened this issue May 12, 2023 · 3 comments
Open

Encoding::CompatibilityError when csv in UTF-16 format #17

makarychev opened this issue May 12, 2023 · 3 comments

Comments

@makarychev
Copy link

In PR Use String#start_with? to detect leading special character #12 the starts_with_special_character? implementation was changed to use start_with? which throw an error in case CSV in UTF-16LE format

Encoding::CompatibilityError:
       incompatible character encodings: UTF-16LE and UTF-8
@zvory
Copy link
Owner

zvory commented May 13, 2023 via email

@makarychev
Copy link
Author

Hey @zvory,
Thank you for the prompt response.
Here is the example

require 'csv'
require 'csv-safe'

class MyCSV
  def self.generate_multiplatform
    CSVSafe.generate(col_sep: "\t") { |csv| yield csv }.encode('utf-16le').prepend("\xFF\xFE".force_encoding('utf-16le'))
  end
end

csv = MyCSV.generate_multiplatform do |c|
  c << ['Вода', 'Water', '💧']
end

CSVSafe.parse(csv, col_sep: "\t", encoding: 'utf-16le')

which throw the error

csv-safe-3.2.1/lib/csv-safe.rb:24:in `start_with?': incompatible character encodings: UTF-16LE and UTF-8 (Encoding::CompatibilityError)
        from ruby-3.2.1/gems/csv-safe-3.2.1/lib/csv-safe.rb:24:in `starts_with_special_character?'
        from ruby-3.2.1/gems/csv-safe-3.2.1/lib/csv-safe.rb:36:in `prefix_if_necessary'
        from ruby-3.2.1/gems/csv-safe-3.2.1/lib/csv-safe.rb:47:in `sanitize_field'
        from ruby-3.2.1/lib/ruby/3.2.0/csv/fields_converter.rb:59:in `block (2 levels) in convert'
        from ruby-3.2.1/lib/ruby/3.2.0/csv/fields_converter.rb:56:in `each'
        from ruby-3.2.1/lib/ruby/3.2.0/csv/fields_converter.rb:56:in `block in convert'
        from ruby-3.2.1/lib/ruby/3.2.0/csv/fields_converter.rb:50:in `collect'
        from ruby-3.2.1/lib/ruby/3.2.0/csv/fields_converter.rb:50:in `with_index'
        from ruby-3.2.1/lib/ruby/3.2.0/csv/fields_converter.rb:50:in `convert'
        from ruby-3.2.1/lib/ruby/3.2.0/csv/parser.rb:1267:in `emit_row'
        from ruby-3.2.1/lib/ruby/3.2.0/csv/parser.rb:1017:in `block in parse_quotable_loose'
        from ruby-3.2.1/lib/ruby/3.2.0/csv/parser.rb:52:in `block in each_line'
        from ruby-3.2.1/lib/ruby/3.2.0/csv/parser.rb:49:in `each_line'
        from ruby-3.2.1/lib/ruby/3.2.0/csv/parser.rb:49:in `each_line'
        from ruby-3.2.1/lib/ruby/3.2.0/csv/parser.rb:963:in `parse_quotable_loose'
        from ruby-3.2.1/lib/ruby/3.2.0/csv/parser.rb:406:in `parse'

@zvory
Copy link
Owner

zvory commented May 19, 2023

Ah unfortunately I'm way busier than I expected and really don't have a moment to spare. I'm going to try to make time for fixing this but I don't know when I'll be able to. If you put up a PR though I'll review it and try to get it merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants