From 40e5b3933662ef3cd41f57fe8e8003b3f7c04b05 Mon Sep 17 00:00:00 2001 From: jeg2 Date: Tue, 16 Nov 2010 23:55:29 +0000 Subject: * 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 --- lib/csv.rb | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'lib/csv.rb') diff --git a/lib/csv.rb b/lib/csv.rb index fed35d2a81..fe7309009b 100644 --- a/lib/csv.rb +++ b/lib/csv.rb @@ -1562,9 +1562,10 @@ class CSV options = DEFAULT_OPTIONS.merge(options) # 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 - @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, # if we can transcode the needed characters @@ -1711,7 +1712,15 @@ class CSV @headers = row if header_row? @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 end @@ -2043,11 +2052,13 @@ class CSV @row_sep = @row_sep.to_s.encode(@encoding) # establish quoting rules - @force_quotes = options.delete(:force_quotes) - do_quote = lambda do |field| - @quote_char + - String(field).gsub(@quote_char, @quote_char * 2) + - @quote_char + @force_quotes = options.delete(:force_quotes) + do_quote = lambda do |field| + field = String(field) + encoded_quote = @quote_char.encode(field.encoding) + encoded_quote + + field.gsub(encoded_quote, encoded_quote * 2) + + encoded_quote end quotable_chars = encode_str("\r\n", @col_sep, @quote_char) @quote = if @force_quotes @@ -2297,6 +2308,7 @@ class CSV end private + def raw_encoding if @io.respond_to? :internal_encoding @io.internal_encoding || @io.external_encoding -- cgit v1.2.3