Skip to content

Ignore a quoted carriage return when detecting the row separator - #402

Open
adityawrk wants to merge 2 commits into
ruby:mainfrom
adityawrk:fix/quoted-cr-row-sep
Open

adityawrk wants to merge 2 commits into
ruby:mainfrom
adityawrk:fix/quoted-cr-row-sep

Conversation

@adityawrk

@adityawrk adityawrk commented Nov 29, 2026 •

Copy link
Copy Markdown

CSV.generate quotes a field that contains \r and ends rows with \n. Parsing that text with the default row_sep: :auto treated the quoted \r as the row separator:

csv = CSV.generate { |out| out << ["a\rb", "c"] << ["d", "e"] }
# => "\"a\rb\",c\nd,e\n"

CSV.parse(csv)
# was [["a\rb", "c\nd", "e\n"]]
# now [["a\rb", "c"], ["d", "e"]]

CSV.parse_line(CSV.generate_line(["a\rb"]))
# was CSV::MalformedCSVError: Any value after quoted field isn't allowed in line 1.
# now ["a\rb"]

Detection uses the first \r\n, \n, or \r that is not inside a quoted field. If the sample has no line ending outside quotes, it still uses the first one anywhere, which is how foo,"\r",baz keeps parsing as one row. A file whose real separator is \r and whose quoted fields contain \n is detected as \r.

ruby run-test.rb is 536 tests, 4070 assertions, including the chunk-size-1 scanner run.

Fixes #389

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.
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 ruby#389

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant