mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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
This commit is contained in:
parent
0e1cd3e37c
commit
35c70b2137
4 changed files with 45 additions and 7 deletions
13
ChangeLog
13
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 :-)
|
||||
|
|
|
|||
21
lib/csv.rb
21
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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue