summaryrefslogtreecommitdiff
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
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
-rw-r--r--ChangeLog13
-rw-r--r--lib/csv.rb21
-rw-r--r--test/csv/test_encodings.rb16
-rw-r--r--version.h2
4 files changed, 45 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index 454db4cadd..47a7a66b28 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+Thu Nov 18 00:02:17 2010 James Edward Gray II <jeg2@ruby-lang.org>
+
+ * lib/csv.rb: Upgrading output encoding with ASCII content
+ as needed. [ruby-core:33229]
+
+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 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>
* vm_insnhelper.c (vm_throw): remove fear of undefined behavior :-)
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
diff --git a/test/csv/test_encodings.rb b/test/csv/test_encodings.rb
index 5d29ac1fa9..3dc80ccdb1 100644
--- a/test/csv/test_encodings.rb
+++ b/test/csv/test_encodings.rb
@@ -217,6 +217,22 @@ class TestEncodings < Test::Unit::TestCase
assert_equal(data, CSV.read(@temp_csv_path, encoding: encoding.name))
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
+
+ def test_encoding_is_upgraded_for_ascii_content_during_writing_as_needed
+ data = ["foo".force_encoding("ISO-8859-1"), "\u3042"]
+ assert_equal("ISO-8859-1", 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
diff --git a/version.h b/version.h
index e042492c97..9148cccb65 100644
--- a/version.h
+++ b/version.h
@@ -1,5 +1,5 @@
#define RUBY_VERSION "1.9.2"
-#define RUBY_PATCHLEVEL 99
+#define RUBY_PATCHLEVEL 100
#define RUBY_VERSION_MAJOR 1
#define RUBY_VERSION_MINOR 9
#define RUBY_VERSION_TEENY 1