From 822fa51412ee2a8551a3f0b23abc843a9ebc9362 Mon Sep 17 00:00:00 2001
From: Aditya <152765789+adityawrk@users.noreply.github.com>
Date: Sun, 29 Nov 2026 08:29:10 +0530
Subject: [PATCH 1/2] Test round-tripping a field that contains a carriage
return
CSV.generate writes a quoted "\r" and a "\n" row separator.
CSV.parse with the default :auto separator treated the quoted
"\r" as the separator and merged the following rows.
---
test/csv/parse/test_row_separator.rb | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/test/csv/parse/test_row_separator.rb b/test/csv/parse/test_row_separator.rb
index 5fd8e751..4c30b711 100644
--- a/test/csv/parse/test_row_separator.rb
+++ b/test/csv/parse/test_row_separator.rb
@@ -13,4 +13,14 @@ def test_multiple_characters
CSV.parse("a\r\nb\r\n", row_sep: "\r\n"))
end
end
+
+ def test_auto_row_separator_ignores_quoted_cr
+ assert_equal(["a\rb"], CSV.parse_line(CSV.generate_line(["a\rb"])))
+ csv = CSV.generate { |out| out << ["a\rb", "c"] << ["d", "e"] }
+ assert_equal([["a\rb", "c"], ["d", "e"]], CSV.parse(csv))
+ assert_equal([["a\rb", "c"], ["d", "e"]],
+ CSV.parse("\"a\rb\",c\nd,e\n"))
+ assert_equal([["a\nb"], ["c"]],
+ CSV.parse("\"a\nb\"\rc\r"))
+ end
end
From 1d3d1b95feefc973567ef12c599d7364b4908a61 Mon Sep 17 00:00:00 2001
From: Aditya <152765789+adityawrk@users.noreply.github.com>
Date: Sun, 29 Nov 2026 08:29:10 +0530
Subject: [PATCH 2/2] Ignore a quoted carriage return when detecting the row
separator
Auto-detection used the first CR or LF in the sample, including
one inside a quoted field. A field written by CSV itself then
parsed as a single merged row. Detection now uses the first
line ending outside quotes.
Fixes #389
---
doc/csv/options/common/row_sep.rdoc | 8 ++++---
lib/csv/parser.rb | 36 +++++++++++++++++++++++++++++
2 files changed, 41 insertions(+), 3 deletions(-)
diff --git a/doc/csv/options/common/row_sep.rdoc b/doc/csv/options/common/row_sep.rdoc
index eae15b4a..24de617d 100644
--- a/doc/csv/options/common/row_sep.rdoc
+++ b/doc/csv/options/common/row_sep.rdoc
@@ -68,9 +68,11 @@ generating uses "\n" as the row separator:
Parsing, on the other hand, invokes auto-discovery of the row separator.
-Auto-discovery reads ahead in the data looking for the next \r\n, +\n+, or +\r+ sequence.
-The sequence will be selected even if it occurs in a quoted field,
-assuming that you would have the same line endings there.
+Auto-discovery reads ahead in the data looking for the next \r\n, +\n+, or +\r+ sequence
+that is not inside a quoted field.
+A carriage return that belongs to a quoted field does not decide the separator,
+so a file written with the default "\n" separator still parses when a field contains "\r".
+If the sample has no line ending outside quotes, the first \r\n, +\n+, or +\r+ anywhere in it is used.
Example:
str = CSV.generate do |csv|
diff --git a/lib/csv/parser.rb b/lib/csv/parser.rb
index f14be3af..6027d7a6 100644
--- a/lib/csv/parser.rb
+++ b/lib/csv/parser.rb
@@ -731,6 +731,9 @@ def resolve_row_separator(separator)
end
def detect_row_separator(sample, cr, lf)
+ detected = first_unquoted_row_separator(sample, cr, lf)
+ return detected if detected
+
lf_index = sample.index(lf)
if lf_index
cr_index = sample[0, lf_index].index(cr)
@@ -754,6 +757,39 @@ def detect_row_separator(sample, cr, lf)
end
end
+ # The first row separator that is not inside a quoted field. A quoted
+ # "\r" must not hide the "\n" that actually ends the row. nil when every
+ # candidate is quoted, so the caller can fall back to the first candidate
+ # anywhere in the sample.
+ def first_unquoted_row_separator(sample, cr, lf)
+ quote = @quote_character
+ return nil if quote.nil? || quote.empty?
+
+ quote_length = quote.length
+ crlf = cr + lf
+ index = 0
+ length = sample.length
+ in_quotes = false
+ while index < length
+ if sample[index, quote_length] == quote
+ if in_quotes && sample[index + quote_length, quote_length] == quote
+ index += quote_length * 2
+ next
+ end
+ in_quotes = !in_quotes
+ index += quote_length
+ next
+ end
+ unless in_quotes
+ return crlf if sample[index, crlf.length] == crlf
+ return cr if sample[index, cr.length] == cr
+ return lf if sample[index, lf.length] == lf
+ end
+ index += 1
+ end
+ nil
+ end
+
def prepare_line
@lineno = 0
@last_line = nil