Skip to content

CSV row splitting: a lone CR ends a row only in a file with quotes, and an LFCR header loses the first row #1891

Done

Description

@singaraiona

Summary

Whether a lone CR ends a CSV row depends on whether the file contains a quote anywhere, and an LFCR line end after the header loses the first data row.

Repro (dev 2ed4637)

printf 'a,b\r1,2\r3,4\r'     > cr_noq.csv    # CR line ends, no quotes
printf 'a,b\r1,"2"\r3,4\r'   > cr_q.csv      # the same with one quoted cell
printf 'a,b\n\r1,2\n\r3,4\n\r' > lfcr.csv    # LFCR line ends
(.csv.read [I64 I64] "cr_noq.csv")   ; 1 row:  a [1]      b [2]       -- "3,4" is lost
(.csv.read [I64 I64] "cr_q.csv")     ; 2 rows: a [1 3]    b [2 4]
(.csv.read [I64 I64] "lfcr.csv")     ; 2 rows: a [0Nl 3]  b [0Nl 4]   -- "1,2" is lost

The row splitter has two paths: without quotes it splits at LF only (absorbing a CR right after it), with quotes it ends a row at LF, CRLF or a lone CR. The cell scanner ends a cell at CR in both, so in a quote-free CR file everything after a row's first CR is never read. The header reader consumes CRLF but not LFCR, so after an LFCR header the first data row starts at the CR and reads as empty.

What the reader should do

A row ends at LF, CRLF, a lone CR, or LFCR (one line end, as the data rows already treat it), the same with or without quotes, and the header ends the same way. The writer (.csv.write) emits LF line ends and quotes every cell holding a CR or LF, so its output reads the same under this rule (checked: a table with "x\ry" and "p\nq" cells round-trips through either path today); the change only affects files from elsewhere, where it recovers rows that are lost now.

Found while working on #1885 (strict mode refuses the quote-free CR case rather than losing the text).

Activity

  1. singaraiona commented on Nov 28, 2026

    @singaraiona
    CollaboratorAuthor

    Fixed in #1895: a row, and the header, ends at LF, CRLF, a lone CR or LFCR, with quotes in the file or without; inside a quoted cell they stay text. The quote-free scan now searches for LF and CR in one pass, so LF and CRLF files read as fast as before; .csv.write's output (LF line ends, CR/LF cells quoted) reads back cell for cell.

  2. added 5 commits that reference this issue on Nov 28, 2026
    0fa8911
    225e49c
    ec4544a
    bd1c42c
    320e57b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions