Skip to content

feat: an option to read csv quotes strictly, refusing what the reader would otherwise repair - #199

Merged
vaceslav merged 4 commits into
mainfrom
fix/198-csv-strict-quotes
Nov 27, 2026
Merged

vaceslav merged 4 commits into
mainfrom
fix/198-csv-strict-quotes

Conversation

@vaceslav

Copy link
Copy Markdown
Contributor

What and why

The csv reader repairs malformed quoting, and one repair splits valid csv: a quoted field that spans lines and holds a record's worth of delimiters (tables of five columns or more), or more than MaxQuotedFieldLines line breaks, is read again as records. A spreadsheet's export of a cell with a line break and a few commas has exactly that shape. A caller that turns each of a few user-typed rows into work would rather refuse a file than misread a row, and until now its only signal was Diagnostics.IsClean, which also refuses the valid export.

CsvCursorOptions.QuoteMode (new enum CsvQuoteMode) chooses:

  • Repair, the default, is unchanged.
  • Strict reads quotes as RFC 4180 does. A quoted field may span any number of lines and hold any number of delimiters; only MaxFieldChars bounds it, and MaxQuotedFieldLines does not apply. A quoted field the file ends inside, and text after a closing quote ("C" Road), are refused as TabularFormatException format.corrupt, the message naming the row; the cursor is faulted afterwards. Whitespace (space, and tab unless it is the delimiter) before an opening and after a closing quote is dropped, so 1, "Payroll", "monthly, HR" is three values. A quote inside a field that does not start with one (21"st AVE) stays text, as in every spreadsheet program. Nothing is repaired, so Diagnostics stays clean. Strict mode keeps no raw text for a replay, so a quoted field is held once instead of twice.

The same options reach csv files inside archives and gzip.

Fixes #198

Measured

Default path, 5M-row malformed csv (572 MB), 8 alternating runs on a shared machine: main min 3,026 ms, this branch min 3,043 ms (+0.6 %, within the A/A noise of about ±3 %). Allocations equal to the byte apart from the new code (2,875,288,184 vs 2,875,289,080 B); rows (5,127,969) and repairs (458) identical.

What a reader would not guess

  • Whether a quote after whitespace opens a field is asked at every quote inside a field, so it looks from the end of the field: looking from the start was quadratic on a field of blanks, a letter and a run of quotes (60,000 quotes: 24 s; now milliseconds, pinned by a test).
  • A stray opening quote in a large file in strict mode reads up to MaxFieldChars and ends as TabularLimitException, not format.corrupt; a small file ends at its end and gets format.corrupt with the row.
  • New tests: StrictQuoteTests, a strict round-trip fuzz with whitespace-padded quoted fields, and a strict pass in the mangled-input fuzz.

Public API

Added, nothing changed: CsvQuoteMode (Repair = 0, Strict = 1) and CsvCursorOptions.QuoteMode.

Checklist

  • A test that failed before the change and passes after it (for a fix or a new behaviour).
  • dotnet build and dotnet test pass on net10.0 and net11.0 with zero warnings.
  • If the read path changed: measured, and the numbers are in the description.
  • If an error code was added or changed: ErrorCodes and the guide's error-code table agree.
  • Public API changes are described, and breaking ones are called out.
  • No third-party package in src/.

@vaceslav
vaceslav merged commit 3ba2885 into main Nov 27, 2026
10 checks passed
@vaceslav
vaceslav deleted the fix/198-csv-strict-quotes branch November 27, 2026 09:24
vaceslav added a commit that referenced this pull request Nov 27, 2026
… drops any whitespace around a quoted field (#202)

## What and why

The follow-ups the two reviews of #199 left open for `CsvQuoteMode.Strict`, so that its refusals say where and why, its reads stay cancellable, and its whitespace rule matches the trim.

- **A stray quote that reaches the field limit says so.** In strict mode a quote that is never closed reads on to `MaxFieldChars`. It stays `TabularLimitException` (`limit.exceeded`) — a quoted field that long which would have closed later is indistinguishable, and a host answers a limit as an upload too large — but every csv field-limit message now names the row, and in strict mode inside a quoted field it says the quote was never closed within the limit.
- **A refusal inside an archive names the file.** `ArchiveCursor.ReadRow` prefixes a `TabularFormatException` from the file inside with the entry's path (`data/bad.csv: The csv file is not valid at row 2: …`), keeping the code and the original as the inner exception. That holds for any file in a zip or tar, a damaged workbook part included.
- **Cancellation reaches the whitespace after a closing quote.** The skip had no bound and no check: a closing quote and a few hundred million spaces held a read beyond its token. It now asks for cancellation every 64 K characters, as the row loop does.
- **The whitespace dropped around a quoted field is what a cell is trimmed of anyway**, a no-break space included (common in text pasted from spreadsheets and web pages), except a tab that is the delimiter and line endings. It was only space and tab. Consequently `Quote` may not be whitespace when `QuoteMode` is `Strict` (refused when the cursor opens).
- **Strict mode reads quoted text faster.** Inside quotes it counts nothing, so its run now ends only at a quote instead of at every delimiter and line break.
- `format.corrupt` rather than `format.truncated` for a file that ends inside a quoted field is kept and explained in the code: a quote left open is far more often a forgotten one than a clipped upload. That whitespace before an opening quote counts towards `MaxFieldChars` (it is held until the quote shows it is not content) is documented and pinned by a test.

Fixes #201

## Measured

- Default path, 5M-row malformed csv (572 MB), 8 alternating runs on a shared machine: `main` min 3,082 ms, this branch min 3,070 ms (no change); allocations, rows (5,127,969) and repairs (458) identical.
- Strict mode, synthetic 74 MB csv of 300,000 rows each with a quoted note of twelve commas and three line breaks, min of 7: `main` 185 ms, this branch 76 ms.

## Tests

New in `StrictQuoteTests`: the limit message, the blanks counted towards the limit, no-break and em spaces around a quoted field, cancellation inside a run of spaces after a closing quote, a refusal with the closing quote on either side of the 64 K read-buffer edge, blanks before an opening quote crossing that edge, and strict reads of a zip and a tar whose bad entry is named. `OptionsValidationTests` refuses a whitespace quote in strict mode. The strict round-trip fuzz pads quoted fields with no-break and em spaces too (20,000 cases run locally). The comment of the linear-time test now gives the measured 24 s.

## Public API

No new members. Behaviour: `CsvCursorOptions` with `QuoteMode = Strict` and a whitespace `Quote` is now refused (`ArgumentOutOfRangeException`), and strict mode drops all whitespace, not only space and tab, around a quoted field.

## Checklist

- [x] A test that failed before the change and passes after it (for a fix or a new behaviour).
- [x] `dotnet build` and `dotnet test` pass on net10.0 and net11.0 with zero warnings.
- [x] If the read path changed: measured, and the numbers are in the description.
- [x] If an error code was added or changed: `ErrorCodes` and the guide's error-code table agree.
- [x] Public API changes are described, and breaking ones are called out.
- [x] No third-party package in `src/`.
vaceslav added a commit that referenced this pull request Nov 27, 2026
🤖 I have created a release *beep* *boop*
---


## [0.9.0](v0.8.3...v0.9.0)
(2026-11-27)


### Features

* an option to read csv quotes strictly, refusing what the reader would
otherwise repair
([#199](#199))
([3ba2885](3ba2885))
* group rows into outline levels in the xlsx and ods writers
([#203](#203))
([5b87928](5b87928)),
closes [#197](#197)


### Bug Fixes

* strict csv quoting names where it refuses, stays cancellable and drops
any whitespace around a quoted field
([#202](#202))
([3d4f26b](3d4f26b))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Slava <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

csv: an option to read quotes strictly, without stray-quote recovery

1 participant