Repository navigation
to csv/tsv: add --quote and --escape, matching from csv/tsv - #19328
Conversation
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
With --escape, escape characters that are already in the data aren't escaped, so values with a backslash don't survive to csv --escape '\' | from csv --escape '\'. The ASCII check also lets Latin-1 characters through and produces invalid UTF-8.
Findings: 1
Open (10)
--escapedrops backslashes that are already in the data The ASCII check lets U+0080 to U+00FF through No tests beyond the examples, and nothing covers the streaming writer The builder setup is copied into both closures and rebuilt for every row Quote and escape errors point at the command instead of the flag value--quote 'ab'reports a missing "single character separator" A quote or escape that matches the separator writes unreadable outputto_csvtakes(quote, escape)as a tuple,to_tsvtakes two arguments The--quotehelp says it only wraps fields containing the separatorto tsvhas no--quoteexample
Notes from my clanker, checked on 943bea5. Built the branch and ran 18 probes against it (escape round trips, Latin-1 quote/escape, error spans, quote == separator, streaming input), plus the formats::to unit tests and the format_conversions::csv/tsv integration tests.
|
when complete, please reply to each review comment so we can be sure that everything has been fixed |
|
Done! |
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
Two small cleanups in delimited.rs. The --escape '"' round trip in the escape thread is still the one that matters.
Findings: 0
Notes from my clanker, checked on cfcafc2. Built the branch with both suggestions applied: cargo fmt is clean and the format_conversions::csv tests pass.
|
where are we at on this? |
from csv and from tsv already expose --quote/--escape, wired straight into csv::ReaderBuilder::quote()/escape(). The write side only ever exposed --separator/--noheaders/--columns, and the shared writer helper (ToDelimitedDataArgs, to_delimited_data) never touched csv::WriterBuilder::quote()/escape() at all, even though the same csv crate's WriterBuilder supports both symmetrically to ReaderBuilder. Net effect: you could parse CSV/TSV written with a custom quote or escape character, but never produce it, so round-tripping non-default-quoted data was impossible. Add the same --quote/--escape flags to to csv and to tsv, threaded through to_delimited_data's two WriterBuilder construction sites (materialized and streaming paths). Note WriterBuilder::escape (unlike ReaderBuilder's) only has an effect once double_quote is turned off, so --escape also disables the default doubled-quote escaping when set, matching the csv crate's own documented behavior for idiosyncratic (non-doubled) quote escaping.
- Double an escape byte that's already in the data before writing, not just the quote byte, so --escape round-trips through from's --escape instead of silently dropping the character. Applies to both the materialized and streaming writers, and to headers too. - Check is_ascii() before converting quote/escape/separator to a byte, instead of relying on u8::try_from(char), which also accepts the Latin-1 range and corrupted the output stream for those. - Reject quote or escape equal to the separator, since that produces output that can't be split back into columns. - Parse quote/escape as Spanned<char> so a bad value's own span is reported instead of the command's head, and so the "single character ..." error names the flag instead of always saying "separator". - Build the WriterBuilder once instead of inside both per-row closures. - Fold the now 8-argument to_csv into ToCsv::run and drop the (quote, escape) tuple on to_csv so it matches to_tsv's separate parameters. - Add write-side tests for --quote/--escape, including the streaming (ListStream) branch, round trips, and the new validation errors. - Fix the --quote help text and give to tsv a --quote example.
Drop the unneeded raw string literals in two of the new round-trip and validation tests.
…equals --quote Per review: use a let chain instead of an expect() on escape_span (it can't fire today, but AGENTS.md asks for no unwrap/expect outside tests), and stop write_csv_header's doc comment from pointing at a review-comment thread instead of explaining itself. Also caught while re-testing: csv-core's own writer already doubles every quote byte it writes inside a quoted field once escape is set, so when the escape character is the same as the quote character, escape_delimited_field's pre-doubling pass and the writer's own quote-escaping compounded into 'a""""b' instead of 'a""b'. Skip the pre-doubling pass in that one case, since the writer's own quote-escaping already covers it.
1871adb to
01242b9
Compare
|
Thanks |



Description
from csv/from tsvalready expose--quote/-qand--escape/-eflags, wired straight into
csv::ReaderBuilder::quote()/.escape(). Thewrite-side siblings
to csv/to tsvonly ever exposed--separator/--noheaders/--columns-- the shared writer helper nevertouched
csv::WriterBuilder::quote()/.escape()at all, even though thesame
csvcrate'sWriterBuildersupports both symmetrically toReaderBuilder. Net effect: you could parse CSV/TSV written with a customquote or escape character, but never produce it, so round-tripping
non-default-quoted data was impossible.
This adds the same
--quote/--escapeflags toto csvandto tsv,threaded through the shared
to_delimited_datahelper's twoWriterBuilderconstruction sites (the materialized-input and streaming-input paths both build a writer independently).
One thing worth calling out for reviewers:
WriterBuilder::escape(unlikeReaderBuilder's) only has an effect oncedouble_quoteis turned off --by default
csvescapes embedded quotes by doubling them, and theidiosyncratic single-escape-character style is an alternative to that, not
an addition on top of it. So
--escapealso disables the defaultdoubled-quote escaping when set, matching the
csvcrate's own documentedbehavior.
User-facing changes (Release notes)
--quote/-qand--escape/-eflags toto csvandto tsv,matching the existing flags on
from csv/from tsv, so CSV/TSV outputcan use a custom quote or escape character instead of the default
"and doubled-quote escaping.
Additional notes
n/a