diff options
author | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-09-17 05:56:35 +0000 |
---|---|---|
committer | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-09-17 05:56:35 +0000 |
commit | 58ff66b18b676293108a625f161342a7a4755df9 (patch) | |
tree | 5c9048e3d392cce71cf2133b2e5e77086e0b5741 | |
parent | 9f67424dc97e2b99d98b71d08178681f0240ee86 (diff) |
merge revision(s) 46391,46395: [Backport #9766]
* lib/csv.rb (CSV#<<): honor explicity given encoding. based on
the patch by DAISUKE TANIWAKI <daisuketaniwaki AT gmail.com> at
[ruby-core:62113]. [Bug #9766]
* lib/csv.rb (CSV#<<): honor explicitly given encoding. based on
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@47608 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | lib/csv.rb | 17 | ||||
-rwxr-xr-x | test/csv/test_encodings.rb | 8 | ||||
-rw-r--r-- | version.h | 6 |
4 files changed, 27 insertions, 10 deletions
@@ -1,3 +1,9 @@ +Wed Sep 17 14:52:38 2014 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * lib/csv.rb (CSV#<<): honor explicitly given encoding. based on + the patch by DAISUKE TANIWAKI <daisuketaniwaki AT gmail.com> at + [ruby-core:62113]. [Bug #9766] + Fri Sep 12 11:34:39 2014 Koichi Sasada <ko1@atdot.net> * test/ruby/test_object.rb: extend timeout. diff --git a/lib/csv.rb b/lib/csv.rb index ce26221f37..ba690dd3e0 100644 --- a/lib/csv.rb +++ b/lib/csv.rb @@ -1146,9 +1146,9 @@ class CSV io.seek(0, IO::SEEK_END) args.unshift(io) else - encoding = (args[-1] = args[-1].dup).delete(:encoding) if args.last.is_a?(Hash) + encoding = args[-1][:encoding] if args.last.is_a?(Hash) str = "" - str.encode!(encoding) if encoding + str.force_encoding(encoding) if encoding args.unshift(str) end csv = new(*args) # wrap @@ -1512,7 +1512,7 @@ class CSV init_headers(options) init_comments(options) - options.delete(:encoding) + @force_encoding = !!(encoding || options.delete(:encoding)) options.delete(:internal_encoding) options.delete(:external_encoding) unless options.empty? @@ -1652,10 +1652,13 @@ class CSV 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) + output.encoding != (encoding = raw_encoding) + if @force_encoding + output = output.encode(encoding) + elsif (compatible_encoding = Encoding.compatible?(@io.string, output)) + @io.set_encoding(compatible_encoding) + @io.seek(0, IO::SEEK_END) + end end @io << output diff --git a/test/csv/test_encodings.rb b/test/csv/test_encodings.rb index 85ed21a9d6..4e2c60545d 100755 --- a/test/csv/test_encodings.rb +++ b/test/csv/test_encodings.rb @@ -257,6 +257,14 @@ class TestCSV::Encodings < TestCSV assert_equal("UTF-8", data.to_csv.encoding.name) end + def test_explicit_encoding + bug9766 = '[ruby-core:62113] [Bug #9766]' + s = CSV.generate(encoding: "Windows-31J") do |csv| + csv << ["foo".force_encoding("ISO-8859-1"), "\u3042"] + end + assert_equal(["foo,\u3042\n".encode(Encoding::Windows_31J), Encoding::Windows_31J], [s, s.encoding], bug9766) + end + private def assert_parses(fields, encoding, options = { }) @@ -1,10 +1,10 @@ #define RUBY_VERSION "2.0.0" -#define RUBY_RELEASE_DATE "2014-09-12" -#define RUBY_PATCHLEVEL 570 +#define RUBY_RELEASE_DATE "2014-09-17" +#define RUBY_PATCHLEVEL 571 #define RUBY_RELEASE_YEAR 2014 #define RUBY_RELEASE_MONTH 9 -#define RUBY_RELEASE_DAY 12 +#define RUBY_RELEASE_DAY 17 #include "ruby/version.h" |