fix(csv-stringify): quote fields containing a bare CR or LF#493
Open
greymoth-jp wants to merge 1 commit into
Open
fix(csv-stringify): quote fields containing a bare CR or LF#493greymoth-jp wants to merge 1 commit into
greymoth-jp wants to merge 1 commit into
Conversation
A field holding a carriage return or line feed is left unquoted unless it contains the configured record_delimiter (default "\n"), so a lone "\r" fails to round-trip: parse auto-detects CR as a record delimiter and reports CSV_RECORD_INCONSISTENT_FIELDS_LENGTH. Quote any field that contains CR or LF.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
stringifyquotes a field only when it contains the quote, the delimiter, or the configuredrecord_delimiter(default"\n"). A field that contains a bare carriage return is therefore emitted unquoted, butparsetreats a lone CR as a record delimiter under its default auto-detection, so the output ofstringifyno longer round-trips throughparse:A
\nround-trips because it matches the defaultrecord_delimiter, and a\r\nround-trips because its\nmatches, but a bare\rslips past every check. The same gap applies to a\ninside a field when a non-\nrecord_delimiteris configured.Fix
Quote a field when it contains a CR or LF.
parserecognizes CR, LF and CRLF as record delimiters in its default auto-detection mode regardless of the delimiter used to write the record, so a field carrying either must be quoted to round-trip. This matches RFC 4180 section 2.6 (fields with line breaks must be enclosed in double quotes) and the behavior of other CSV writers.Quoting is sufficient, since a quoted field with a CR already round-trips:
Test
Added a case under
Option record_delimiterasserting a field with a bare CR is quoted. Reverting the fix makes only that case fail (expected 'x\ry,z' to equal '"x\ry",z'); the rest of thecsv-stringifysuite stays green.