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

ruby-changes:69367

From: Sutou <ko1@a...>
Date: Sun, 24 Oct 2021 05:57:57 +0900 (JST)
Subject: [ruby-changes:69367] 8ba98f83b0 (master): [ruby/csv] Use "\n" for the default row separator on Ruby 3.0 or later

https://git.ruby-lang.org/ruby.git/commit/?id=8ba98f83b0

From 8ba98f83b0fa8634c68e2d86e71718cc8097bfcf Mon Sep 17 00:00:00 2001
From: Sutou Kouhei <kou@c...>
Date: Sun, 12 Sep 2021 07:34:15 +0900
Subject: [ruby/csv] Use "\n" for the default row separator on Ruby 3.0 or
 later

https://github.com/ruby/csv/commit/1f9cbc170e
---
 lib/csv.rb                        | 10 +++++-----
 lib/csv/input_record_separator.rb | 31 +++++++++++++++++++++++++++++++
 lib/csv/parser.rb                 |  3 ++-
 lib/csv/writer.rb                 |  3 ++-
 4 files changed, 40 insertions(+), 7 deletions(-)
 create mode 100644 lib/csv/input_record_separator.rb

diff --git a/lib/csv.rb b/lib/csv.rb
index 3881ba24c2..91aeb19a3c 100644
--- a/lib/csv.rb
+++ b/lib/csv.rb
@@ -90,11 +90,11 @@ https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L90
 # with any questions.
 
 require "forwardable"
-require "English"
 require "date"
 require "stringio"
 
 require_relative "csv/fields_converter"
+require_relative "csv/input_record_separator"
 require_relative "csv/match_p"
 require_relative "csv/parser"
 require_relative "csv/row"
@@ -1051,7 +1051,7 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L1051
     #   out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
     def filter(input=nil, output=nil, **options)
       # parse options for input, output, or both
-      in_options, out_options = Hash.new, {row_sep: $INPUT_RECORD_SEPARATOR}
+      in_options, out_options = Hash.new, {row_sep: InputRecordSeparator.value}
       options.each do |key, value|
         case key.to_s
         when /\Ain(?:put)?_(.+)\Z/
@@ -1292,8 +1292,8 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L1292
     # Argument +ary+ must be an \Array.
     #
     # Special options:
-    # * Option <tt>:row_sep</tt> defaults to <tt>$INPUT_RECORD_SEPARATOR</tt>
-    #   (<tt>$/</tt>).:
+    # * Option <tt>:row_sep</tt> defaults to <tt>"\n"> on Ruby 3.0 or later
+    #   and <tt>$INPUT_RECORD_SEPARATOR</tt> (<tt>$/</tt>) otherwise.:
     #     $INPUT_RECORD_SEPARATOR # => "\n"
     # * This method accepts an additional option, <tt>:encoding</tt>, which sets the base
     #   Encoding for the output. This method will try to guess your Encoding from
@@ -1315,7 +1315,7 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L1315
     #   CSV.generate_line(:foo)
     #
     def generate_line(row, **options)
-      options = {row_sep: $INPUT_RECORD_SEPARATOR}.merge(options)
+      options = {row_sep: InputRecordSeparator.value}.merge(options)
       str = +""
       if options[:encoding]
         str.force_encoding(options[:encoding])
diff --git a/lib/csv/input_record_separator.rb b/lib/csv/input_record_separator.rb
new file mode 100644
index 0000000000..bbf13479f7
--- /dev/null
+++ b/lib/csv/input_record_separator.rb
@@ -0,0 +1,31 @@ https://github.com/ruby/ruby/blob/trunk/lib/csv/input_record_separator.rb#L1
+require "English"
+require "stringio"
+
+class CSV
+  module InputRecordSeparator
+    class << self
+      is_input_record_separator_deprecated = false
+      verbose, $VERBOSE = $VERBOSE, true
+      stderr, $stderr = $stderr, StringIO.new
+      input_record_separator = $INPUT_RECORD_SEPARATOR
+      begin
+        $INPUT_RECORD_SEPARATOR = "\r\n"
+        is_input_record_separator_deprecated = (not $stderr.string.empty?)
+      ensure
+        $INPUT_RECORD_SEPARATOR = input_record_separator
+        $stderr = stderr
+        $VERBOSE = verbose
+      end
+
+      if is_input_record_separator_deprecated
+        def value
+          "\n"
+        end
+      else
+        def value
+          $INPUT_RECORD_SEPARATOR
+        end
+      end
+    end
+  end
+end
diff --git a/lib/csv/parser.rb b/lib/csv/parser.rb
index d0b02a6423..0d8a157fd7 100644
--- a/lib/csv/parser.rb
+++ b/lib/csv/parser.rb
@@ -3,6 +3,7 @@ https://github.com/ruby/ruby/blob/trunk/lib/csv/parser.rb#L3
 require "strscan"
 
 require_relative "delete_suffix"
+require_relative "input_record_separator"
 require_relative "match_p"
 require_relative "row"
 require_relative "table"
@@ -605,7 +606,7 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv/parser.rb#L606
             # do nothing:  ensure will set default
           end
         end
-        separator = $INPUT_RECORD_SEPARATOR if separator == :auto
+        separator = InputRecordSeparator.value if separator == :auto
       end
       separator.to_s.encode(@encoding)
     end
diff --git a/lib/csv/writer.rb b/lib/csv/writer.rb
index d49115fccf..4a9a35c5af 100644
--- a/lib/csv/writer.rb
+++ b/lib/csv/writer.rb
@@ -1,5 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/lib/csv/writer.rb#L1
 # frozen_string_literal: true
 
+require_relative "input_record_separator"
 require_relative "match_p"
 require_relative "row"
 
@@ -133,7 +134,7 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv/writer.rb#L134
       @column_separator = @options[:column_separator].to_s.encode(@encoding)
       row_separator = @options[:row_separator]
       if row_separator == :auto
-        @row_separator = $INPUT_RECORD_SEPARATOR.encode(@encoding)
+        @row_separator = InputRecordSeparator.value.encode(@encoding)
       else
         @row_separator = row_separator.to_s.encode(@encoding)
       end
-- 
cgit v1.2.1


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

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