Repository navigation
feat: an option to read csv quotes strictly, refusing what the reader would otherwise repair - #199
Merged
Merged
Conversation
This was referenced Nov 27, 2026
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]>
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.
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
MaxQuotedFieldLinesline 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 wasDiagnostics.IsClean, which also refuses the valid export.CsvCursorOptions.QuoteMode(new enumCsvQuoteMode) chooses:Repair, the default, is unchanged.Strictreads quotes as RFC 4180 does. A quoted field may span any number of lines and hold any number of delimiters; onlyMaxFieldCharsbounds it, andMaxQuotedFieldLinesdoes not apply. A quoted field the file ends inside, and text after a closing quote ("C" Road), are refused asTabularFormatExceptionformat.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, so1, "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, soDiagnosticsstays 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:
mainmin 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
MaxFieldCharsand ends asTabularLimitException, notformat.corrupt; a small file ends at its end and getsformat.corruptwith the row.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) andCsvCursorOptions.QuoteMode.Checklist
dotnet buildanddotnet testpass on net10.0 and net11.0 with zero warnings.ErrorCodesand the guide's error-code table agree.src/.