CSV: a save that would reopen with another delimiter names its own (#1446)

Saving drops quotes a field no longer needs, so `"a,b";x` read with `;` was written `a,b;x`, a tie
that detection gives to comma, and `a;b;c|d;e;f` saved with commas read back as `;`. The `sep=`
line the first-row guard already writes now also goes first whenever detection on the written
text would pick another delimiter. Test: csv_save_names_its_delimiter_when_reopening_would_pick_another
(fails without the fix). decisions.md no longer promises more than this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Vedran DobosandClaude Opus 5.5 committed 2026-11-15 14:16:13 +01:00
1 parent 313fdef652
commit 3316bac277
3 files changed
+32 -7

No files matched your search

+19
View File
@@ -417,6 +417,25 @@ fn csv_open_honours_a_sep_line_and_saves_a_file_that_reopens_in_columns() {
/// A first cell that reads like a `sep=` line needs one of its own only in a CSV. A `.tsv` is read
/// with tabs given, so a `sep=` line there would come back as an extra row above the data.
#[test]
fn csv_save_names_its_delimiter_when_reopening_would_pick_another() {
let cells = |wb: &Workbook| {
let sh = wb.sheet(0).unwrap();
(0..2).map(|c| sh.value(CellRef::new(0, c)).as_text().map(str::to_string)).collect::<Vec<_>>()
};
// Read with `;`; saved, `a,b` loses its quotes and the line has one comma and one semicolon,
// which detection alone would read as comma-separated.
let (wb, _) = open_bytes("tie.csv", b"sep=;\r\n\"a,b\";x\r\n").unwrap();
let saved = save_bytes(&wb, "tie.csv").unwrap();
assert_eq!(saved, b"sep=;\r\na,b;x\r\n");
assert_eq!(cells(&open_bytes("tie.csv", &saved).unwrap().0), [Some("a,b".into()), Some("x".into())]);
// `|` is saved as commas; semicolons in the values would make detection pick `;`.
let (wb, _) = open_bytes("pipe.csv", b"sep=|\r\na;b;c|d;e;f\r\n").unwrap();
let saved = save_bytes(&wb, "pipe.csv").unwrap();
assert_eq!(saved, b"sep=,\r\na;b;c,d;e;f\r\n");
assert_eq!(cells(&open_bytes("pipe.csv", &saved).unwrap().0), [Some("a;b;c".into()), Some("d;e;f".into())]);
}
#[test]
fn tsv_save_writes_no_sep_line_and_reopens_in_place() {
let mut wb = Workbook::new();
+6 -3
View File
@@ -657,9 +657,12 @@ fn write_delimited(
return Err(too_large(format!("more than {} MiB (stopped at row {})", limit >> 20, row + 1)));
}
}
// A first row that would read as Excel's `sep=` line (a lone `sep=;` cell) stays data behind a
// `sep=` line of its own naming the delimiter, which GridCraft and Excel both drop on opening.
if sep_guard && out.split(['\r', '\n']).next().is_some_and(|first| sep_line(first, '"').is_some()) {
// A `sep=` line naming the delimiter, which GridCraft and Excel both drop on opening, goes first
// when the file wouldn't reopen in the same columns without it: a first row that would read as
// Excel's `sep=` line (a lone `sep=;` cell) stays data, and output that detection would read
// with another delimiter (`a,b;x` once `"a,b"` lost its quotes) keeps its own.
let first_is_sep = out.split(['\r', '\n']).next().is_some_and(|first| sep_line(first, '"').is_some());
if sep_guard && (first_is_sep || sniff_delimiter(&out, '"') != delim as u8) {
out.insert_str(0, &format!("sep={delim}\r\n"));
}
Ok(out.into_bytes())
+7 -4
View File
@@ -142,10 +142,13 @@ Excel for Microsoft 365, de-DE; this build drops it.)
A delimiter given by the caller wins and the line stays data, so a `.tsv`/`.txt` file (read with
tabs) keeps it. Saving: Excel writes its list separator and drops the line. GridCraft saves a CSV
with the separator it was read with, so it keeps a named `,` `;` or tab only when detection picks
it again on reopening; otherwise (`|`, or `sep=;` over text with as many commas) it saves commas,
which reopen in the same columns. A first row that would itself read as a `sep=` line (a lone
`sep=;` cell) is written behind a `sep=` line naming the delimiter, so it stays data; a `.tsv`
or `.txt` file is read with tabs given, so it gets no such line. Excel writes
it again on reopening; otherwise (`|`, or `sep=;` over text with as many commas) it saves commas.
Saving drops quotes a field no longer needs, so the written text can still read differently
(`"a,b";x` saved with `;` is `a,b;x`, a tie that detection gives to comma; `a;b;c|d;e;f` saved with
commas is `a;b;c,d;e;f`, which detection reads as `;`). Whenever detection on the written text would
pick another delimiter, or a first row would itself read as a `sep=` line (a lone `sep=;` cell), the
file starts with a `sep=` line naming its delimiter, so it reopens in the same columns. A `.tsv` or
`.txt` file is read with tabs given, so it gets no such line. Excel writes
such a row bare (`"sep=;";`) and reopens the file garbled (observed: A1 `sep=;;`, row 2 in one cell).
From Text/CSV still guesses its own delimiter; making it detect like File › Open (and so honour