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 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