summaryrefslogtreecommitdiff
path: root/lib/csv
diff options
context:
space:
mode:
authorSutou Kouhei <[email protected]>2021-09-12 07:34:15 +0900
committerSutou Kouhei <[email protected]>2021-10-24 05:57:33 +0900
commit8ba98f83b0fa8634c68e2d86e71718cc8097bfcf (patch)
tree8093406ef146891d9254f179aeac3378b997f279 /lib/csv
parent7f3dd601c895354c041988251a0be05a8a423664 (diff)
[ruby/csv] Use "\n" for the default row separator on Ruby 3.0 or later
https://github.com/ruby/csv/commit/1f9cbc170e
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/5010
Diffstat (limited to 'lib/csv')
-rw-r--r--lib/csv/input_record_separator.rb31
-rw-r--r--lib/csv/parser.rb3
-rw-r--r--lib/csv/writer.rb3
3 files changed, 35 insertions, 2 deletions
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 @@
+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 @@
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
# 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 @@
# frozen_string_literal: true
+require_relative "input_record_separator"
require_relative "match_p"
require_relative "row"
@@ -133,7 +134,7 @@ class CSV
@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