Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions doc/csv/options/common/row_sep.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ generating uses <tt>"\n"</tt> 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 <tt>\r\n</tt>, +\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 <tt>\r\n</tt>, +\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 <tt>"\n"</tt> separator still parses when a field contains <tt>"\r"</tt>.
If the sample has no line ending outside quotes, the first <tt>\r\n</tt>, +\n+, or +\r+ anywhere in it is used.

Example:
str = CSV.generate do |csv|
Expand Down
36 changes: 36 additions & 0 deletions lib/csv/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/csv/parse/test_row_separator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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