summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authoryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-12-20 13:22:40 +0000
committeryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-12-20 13:22:40 +0000
commit35c70b2137fed0ac3c399bb4466190b869687962 (patch)
treef693cbe2876f70a6d476e887384348ecbf66d838 /lib
parent0e1cd3e37c07e65053fafc61df5eb34bf3bf9003 (diff)
merges r29808 and r29822 from trunk into ruby_1_9_2.; workaround for StringIO.
-- * lib/csv.rb: Upgrading output encoding as needed. [ruby-core:33135] -- * lib/csv.rb: Upgrading output encoding with ASCII content as needed. [ruby-core:33229] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_2@30259 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/csv.rb21
1 files changed, 15 insertions, 6 deletions
diff --git a/lib/csv.rb b/lib/csv.rb
index 86209eb19b..9942802c3e 100644
--- a/lib/csv.rb
+++ b/lib/csv.rb
@@ -1703,7 +1703,14 @@ 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, output))
+ @io = StringIO.new(@io.string.force_encoding(compatible_encoding))
+ @io.seek(0, IO::SEEK_END)
+ end
+ @io << output
self # for chaining
end
@@ -2038,11 +2045,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