1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/csv.rb: Upgrading output encoding as needed. [ruby-core:33135]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29808 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
jeg2 2010-11-16 23:55:29 +00:00
parent 6cba0f0663
commit 40e5b39336
3 changed files with 62 additions and 38 deletions

View file

@ -1,3 +1,7 @@
Tue Nov 17 08:54:04 2010 James Edward Gray II <jeg2@ruby-lang.org>
* lib/csv.rb: Upgrading output encoding as needed. [ruby-core:33135]
Tue Nov 16 22:30:39 2010 Yusuke Endoh <mame@tsg.ne.jp> Tue Nov 16 22:30:39 2010 Yusuke Endoh <mame@tsg.ne.jp>
* vm_insnhelper.c (vm_throw): remove fear of undefined behavior :-) * vm_insnhelper.c (vm_throw): remove fear of undefined behavior :-)

View file

@ -1562,9 +1562,10 @@ class CSV
options = DEFAULT_OPTIONS.merge(options) options = DEFAULT_OPTIONS.merge(options)
# create the IO object we will read from # create the IO object we will read from
@io = if data.is_a? String then StringIO.new(data) else data end @io = data.is_a?(String) ? StringIO.new(data) : data
# honor the IO encoding if we can, otherwise default to ASCII-8BIT # honor the IO encoding if we can, otherwise default to ASCII-8BIT
@encoding = raw_encoding || Encoding.default_internal || Encoding.default_external @encoding = raw_encoding || Encoding.default_internal ||
Encoding.default_external
# #
# prepare for building safe regular expressions in the target encoding, # prepare for building safe regular expressions in the target encoding,
# if we can transcode the needed characters # if we can transcode the needed characters
@ -1711,7 +1712,15 @@ class CSV
@headers = row if header_row? @headers = row if header_row?
@lineno += 1 @lineno += 1
@io << row.map(&@quote).join(@col_sep) + @row_sep # quote and separate output = row.map(&@quote).join(@col_sep) + @row_sep # quote and separate
if @io.is_a?(StringIO) and
output.encoding != raw_encoding and
( compatible_encoding = Encoding.compatible?( @io.string.encoding,
output.encoding ) )
@io = StringIO.new(@io.string.force_encoding(compatible_encoding))
@io.seek(0, IO::SEEK_END)
end
@io << output
self # for chaining self # for chaining
end end
@ -2043,11 +2052,13 @@ class CSV
@row_sep = @row_sep.to_s.encode(@encoding) @row_sep = @row_sep.to_s.encode(@encoding)
# establish quoting rules # establish quoting rules
@force_quotes = options.delete(:force_quotes) @force_quotes = options.delete(:force_quotes)
do_quote = lambda do |field| do_quote = lambda do |field|
@quote_char + field = String(field)
String(field).gsub(@quote_char, @quote_char * 2) + encoded_quote = @quote_char.encode(field.encoding)
@quote_char encoded_quote +
field.gsub(encoded_quote, encoded_quote * 2) +
encoded_quote
end end
quotable_chars = encode_str("\r\n", @col_sep, @quote_char) quotable_chars = encode_str("\r\n", @col_sep, @quote_char)
@quote = if @force_quotes @quote = if @force_quotes
@ -2297,6 +2308,7 @@ class CSV
end end
private private
def raw_encoding def raw_encoding
if @io.respond_to? :internal_encoding if @io.respond_to? :internal_encoding
@io.internal_encoding || @io.external_encoding @io.internal_encoding || @io.external_encoding

View file

@ -218,6 +218,14 @@ class TestEncodings < Test::Unit::TestCase
end end
end end
def test_encoding_is_upgraded_during_writing_as_needed
data = ["foo".force_encoding("US-ASCII"), "\u3042"]
assert_equal("US-ASCII", data.first.encoding.name)
assert_equal("UTF-8", data.last.encoding.name)
assert_equal("UTF-8", data.join.encoding.name)
assert_equal("UTF-8", data.to_csv.encoding.name)
end
private private
def assert_parses(fields, encoding, options = { }) def assert_parses(fields, encoding, options = { })