Skip to content
Merged
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
2 changes: 1 addition & 1 deletion lib/csv/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def quote(field)
else
field = String(field) # Stringify fields
# represent empty fields as empty quoted fields
if (@quote_empty and field.empty?) or @quotable_pattern.match?(field)
if (@quote_empty and field.empty?) or (field.valid_encoding? and @quotable_pattern.match?(field))
quote_field(field)
else
field # unquoted field
Expand Down
10 changes: 10 additions & 0 deletions test/csv/interface/test_read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ def test_open_encoding_utf_8_with_bom
end
end

def test_open_invalid_byte_sequence_in_utf_8
CSV.open(@input.path, "w", encoding: Encoding::CP932) do |rows|
error = assert_raise(Encoding::InvalidByteSequenceError) do
rows << ["\x82\xa0"]
end
assert_equal('"\x82" on UTF-8',
error.message)
end
end

def test_open_with_undef_replace
# U+00B7 Middle Dot
CSV.open(@input.path, "w", encoding: Encoding::CP932, undef: :replace) do |rows|
Expand Down