[ruby/csv] Use "\n" for the default row separator on Ruby 3.0 or later

https://github.com/ruby/csv/commit/1f9cbc170e
This commit is contained in:
Sutou Kouhei 2021-09-12 07:34:15 +09:00 коммит произвёл Sutou Kouhei
Родитель 7f3dd601c8
Коммит 8ba98f83b0
4 изменённых файлов: 40 добавлений и 7 удалений

Просмотреть файл

@ -90,11 +90,11 @@
# with any questions. # with any questions.
require "forwardable" require "forwardable"
require "English"
require "date" require "date"
require "stringio" require "stringio"
require_relative "csv/fields_converter" require_relative "csv/fields_converter"
require_relative "csv/input_record_separator"
require_relative "csv/match_p" require_relative "csv/match_p"
require_relative "csv/parser" require_relative "csv/parser"
require_relative "csv/row" require_relative "csv/row"
@ -1051,7 +1051,7 @@ class CSV
# out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n" # out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
def filter(input=nil, output=nil, **options) def filter(input=nil, output=nil, **options)
# parse options for input, output, or both # 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| options.each do |key, value|
case key.to_s case key.to_s
when /\Ain(?:put)?_(.+)\Z/ when /\Ain(?:put)?_(.+)\Z/
@ -1292,8 +1292,8 @@ class CSV
# Argument +ary+ must be an \Array. # Argument +ary+ must be an \Array.
# #
# Special options: # Special options:
# * Option <tt>:row_sep</tt> defaults to <tt>$INPUT_RECORD_SEPARATOR</tt> # * Option <tt>:row_sep</tt> defaults to <tt>"\n"> on Ruby 3.0 or later
# (<tt>$/</tt>).: # and <tt>$INPUT_RECORD_SEPARATOR</tt> (<tt>$/</tt>) otherwise.:
# $INPUT_RECORD_SEPARATOR # => "\n" # $INPUT_RECORD_SEPARATOR # => "\n"
# * This method accepts an additional option, <tt>:encoding</tt>, which sets the base # * 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 # Encoding for the output. This method will try to guess your Encoding from
@ -1315,7 +1315,7 @@ class CSV
# CSV.generate_line(:foo) # CSV.generate_line(:foo)
# #
def generate_line(row, **options) def generate_line(row, **options)
options = {row_sep: $INPUT_RECORD_SEPARATOR}.merge(options) options = {row_sep: InputRecordSeparator.value}.merge(options)
str = +"" str = +""
if options[:encoding] if options[:encoding]
str.force_encoding(options[:encoding]) str.force_encoding(options[:encoding])

Просмотреть файл

@ -0,0 +1,31 @@
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

Просмотреть файл

@ -3,6 +3,7 @@
require "strscan" require "strscan"
require_relative "delete_suffix" require_relative "delete_suffix"
require_relative "input_record_separator"
require_relative "match_p" require_relative "match_p"
require_relative "row" require_relative "row"
require_relative "table" require_relative "table"
@ -605,7 +606,7 @@ class CSV
# do nothing: ensure will set default # do nothing: ensure will set default
end end
end end
separator = $INPUT_RECORD_SEPARATOR if separator == :auto separator = InputRecordSeparator.value if separator == :auto
end end
separator.to_s.encode(@encoding) separator.to_s.encode(@encoding)
end end

Просмотреть файл

@ -1,5 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
require_relative "input_record_separator"
require_relative "match_p" require_relative "match_p"
require_relative "row" require_relative "row"
@ -133,7 +134,7 @@ class CSV
@column_separator = @options[:column_separator].to_s.encode(@encoding) @column_separator = @options[:column_separator].to_s.encode(@encoding)
row_separator = @options[:row_separator] row_separator = @options[:row_separator]
if row_separator == :auto if row_separator == :auto
@row_separator = $INPUT_RECORD_SEPARATOR.encode(@encoding) @row_separator = InputRecordSeparator.value.encode(@encoding)
else else
@row_separator = row_separator.to_s.encode(@encoding) @row_separator = row_separator.to_s.encode(@encoding)
end end