[前][次][番号順一覧][スレッド一覧]

ruby-changes:18236

From: yugui <ko1@a...>
Date: Mon, 20 Dec 2010 22:30:12 +0900 (JST)
Subject: [ruby-changes:18236] Ruby:r30259 (ruby_1_9_2): merges r29808 and r29822 from trunk into ruby_1_9_2.; workaround for StringIO.

yugui	2010-12-20 22:22:40 +0900 (Mon, 20 Dec 2010)

  New Revision: 30259

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=30259

  Log:
    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]

  Modified files:
    branches/ruby_1_9_2/ChangeLog
    branches/ruby_1_9_2/lib/csv.rb
    branches/ruby_1_9_2/test/csv/test_encodings.rb
    branches/ruby_1_9_2/version.h

Index: ruby_1_9_2/ChangeLog
===================================================================
--- ruby_1_9_2/ChangeLog	(revision 30258)
+++ ruby_1_9_2/ChangeLog	(revision 30259)
@@ -1,3 +1,16 @@
+Thu Nov 18 00:02:17 2010  James Edward Gray II  <jeg2@r...>
+
+	* 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@r...>
+
+	* lib/csv.rb: Upgrading output encoding as needed.  [ruby-core:33135]
+
+Tue Nov 17 08:54:04 2010  James Edward Gray II  <jeg2@r...>
+
+	* lib/csv.rb: Upgrading output encoding as needed.  [ruby-core:33135]
+
 Tue Nov 16 22:30:39 2010  Yusuke Endoh  <mame@t...>
 
 	* vm_insnhelper.c (vm_throw): remove fear of undefined behavior :-)
Index: ruby_1_9_2/lib/csv.rb
===================================================================
--- ruby_1_9_2/lib/csv.rb	(revision 30258)
+++ ruby_1_9_2/lib/csv.rb	(revision 30259)
@@ -1703,7 +1703,14 @@
     @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 @@
     @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
Index: ruby_1_9_2/version.h
===================================================================
--- ruby_1_9_2/version.h	(revision 30258)
+++ ruby_1_9_2/version.h	(revision 30259)
@@ -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
Index: ruby_1_9_2/test/csv/test_encodings.rb
===================================================================
--- ruby_1_9_2/test/csv/test_encodings.rb	(revision 30258)
+++ ruby_1_9_2/test/csv/test_encodings.rb	(revision 30259)
@@ -217,6 +217,22 @@
       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
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]