csv.rb: honor encoding option
* 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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46391 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
47c64fdf54
commit
f9a5335ed4
@ -1,3 +1,9 @@
|
|||||||
|
Tue Jun 10 10:57:07 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* 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]
|
||||||
|
|
||||||
Mon Jun 9 20:40:48 2014 Koichi Sasada <ko1@atdot.net>
|
Mon Jun 9 20:40:48 2014 Koichi Sasada <ko1@atdot.net>
|
||||||
|
|
||||||
* gc.c: change full GC timing to keep lower memory usage.
|
* gc.c: change full GC timing to keep lower memory usage.
|
||||||
|
13
lib/csv.rb
13
lib/csv.rb
@ -1148,9 +1148,9 @@ class CSV
|
|||||||
io.seek(0, IO::SEEK_END)
|
io.seek(0, IO::SEEK_END)
|
||||||
args.unshift(io)
|
args.unshift(io)
|
||||||
else
|
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 = ""
|
||||||
str.encode!(encoding) if encoding
|
str.force_encoding(encoding) if encoding
|
||||||
args.unshift(str)
|
args.unshift(str)
|
||||||
end
|
end
|
||||||
csv = new(*args) # wrap
|
csv = new(*args) # wrap
|
||||||
@ -1524,7 +1524,7 @@ class CSV
|
|||||||
init_headers(options)
|
init_headers(options)
|
||||||
init_comments(options)
|
init_comments(options)
|
||||||
|
|
||||||
options.delete(:encoding)
|
@force_encoding = !!(encoding || options.delete(:encoding))
|
||||||
options.delete(:internal_encoding)
|
options.delete(:internal_encoding)
|
||||||
options.delete(:external_encoding)
|
options.delete(:external_encoding)
|
||||||
unless options.empty?
|
unless options.empty?
|
||||||
@ -1664,11 +1664,14 @@ class CSV
|
|||||||
|
|
||||||
output = 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
|
if @io.is_a?(StringIO) and
|
||||||
output.encoding != raw_encoding and
|
output.encoding != (encoding = raw_encoding)
|
||||||
(compatible_encoding = Encoding.compatible?(@io.string, output))
|
if @force_encoding
|
||||||
|
output = output.encode(encoding)
|
||||||
|
elsif (compatible_encoding = Encoding.compatible?(@io.string, output))
|
||||||
@io.set_encoding(compatible_encoding)
|
@io.set_encoding(compatible_encoding)
|
||||||
@io.seek(0, IO::SEEK_END)
|
@io.seek(0, IO::SEEK_END)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
@io << output
|
@io << output
|
||||||
|
|
||||||
self # for chaining
|
self # for chaining
|
||||||
|
@ -247,6 +247,14 @@ class TestCSV::Encodings < TestCSV
|
|||||||
assert_equal("UTF-8", data.to_csv.encoding.name)
|
assert_equal("UTF-8", data.to_csv.encoding.name)
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def assert_parses(fields, encoding, options = { })
|
def assert_parses(fields, encoding, options = { })
|
||||||
|
Loading…
x
Reference in New Issue
Block a user