Skip to content

Commit

Permalink
Don't enable BOM detection automatically on Windows
Browse files Browse the repository at this point in the history
It seems that it may have a bug on Windows:
https://bugs.ruby-lang.org/issues/20526
  • Loading branch information
kou committed Jun 6, 2024
1 parent a727f84 commit 47bf76f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1582,12 +1582,7 @@ def open(filename, mode="r", **options)
# wrap a File opened with the remaining +args+ with no newline
# decorator
file_opts = {}
have_encoding_options = (options.key?(:encoding) or
options.key?(:external_encoding) or
mode.include?(":"))
if not have_encoding_options and Encoding.default_external == Encoding::UTF_8
file_opts[:encoding] = "bom|utf-8"
end
may_enable_bom_deletection_automatically(mode, options, file_opts)
file_opts.merge!(options)
unless file_opts.key?(:newline)
file_opts[:universal_newline] ||= false
Expand Down Expand Up @@ -1886,6 +1881,21 @@ def table(path, **options)
options = default_options.merge(options)
read(path, **options)
end

ON_WINDOWS = /mingw|mswin/.match?(RUBY_PLATFORM)
private_constant :ON_WINDOWS

private
def may_enable_bom_deletection_automatically(mode, options, file_opts)
# "bom|utf-8" may be buggy on Windows:
# https://bugs.ruby-lang.org/issues/20526
return if ON_WINDOWS
return unless Encoding.default_external == Encoding::UTF_8
return if options.key?(:encoding)
return if options.key?(:external_encoding)
return if mode.include?(":")
file_opts[:encoding] = "bom|utf-8"
end
end

# :call-seq:
Expand Down
3 changes: 3 additions & 0 deletions test/csv/interface/test_read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ def test_open_with_newline
end

def test_open_with_bom
if /mingw|mswin/.match?(RUBY_PLATFORM)
omit("BOM detection on Windows may be buggy: Bug #20526")
end
csv_data = @input.read
bom = "\ufeff" # U+FEFF ZERO WIDTH NO-BREAK SPACE
File.binwrite(@input.path, "#{bom}#{csv_data}")
Expand Down

0 comments on commit 47bf76f

Please sign in to comment.