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

ruby-changes:34310

From: nobu <ko1@a...>
Date: Tue, 10 Jun 2014 10:57:14 +0900 (JST)
Subject: [ruby-changes:34310] nobu:r46391 (trunk): csv.rb: honor encoding option

nobu	2014-06-10 10:57:10 +0900 (Tue, 10 Jun 2014)

  New Revision: 46391

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

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

  Modified files:
    trunk/ChangeLog
    trunk/lib/csv.rb
    trunk/test/csv/test_encodings.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46390)
+++ ChangeLog	(revision 46391)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Jun 10 10:57:07 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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@a...>
 
 	* gc.c: change full GC timing to keep lower memory usage.
Index: lib/csv.rb
===================================================================
--- lib/csv.rb	(revision 46390)
+++ lib/csv.rb	(revision 46391)
@@ -1148,9 +1148,9 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L1148
       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
@@ -1524,7 +1524,7 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L1524
     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?
@@ -1664,10 +1664,13 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L1664
 
     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.set_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
 
Index: test/csv/test_encodings.rb
===================================================================
--- test/csv/test_encodings.rb	(revision 46390)
+++ test/csv/test_encodings.rb	(revision 46391)
@@ -247,6 +247,14 @@ class TestCSV::Encodings < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_encodings.rb#L247
     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 = { })

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

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