CSV: the sep= line counts against the output limit (#1446)

A save that puts a sep= line first checks the limit with the line included,
so a file at the cap is refused instead of growing past it. The CSV row in
file-format-parity.md now says when a sep= line is written.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Vedran Dobos
2026-11-15 16:14:13 +01:00
co-authored by Claude Opus 5.5
parent 7853f14449
commit a7ae5a8668
3 changed files with 17 additions and 2 deletions
+6 -1
View File
@@ -459,7 +459,12 @@ fn write_delimited(
// 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"));
let line = format!("sep={delim}\r\n");
// The line counts against the cap like the rows.
if out.len().saturating_add(line.len()) > limit {
return Err(too_large(format!("more than {} MiB with its sep= line", limit >> 20)));
}
out.insert_str(0, &line);
}
Ok(out.into_bytes())
}
+10
View File
@@ -446,6 +446,16 @@ fn a_named_delimiter_is_kept_only_if_it_would_be_detected_again() {
assert_eq!(read_csv(b"sep=;\na;b\n1;2\n", &opts).unwrap().csv_delimiter, Some(b';'));
}
#[test]
fn sep_line_counts_against_the_output_limit() {
let mut sheet = Sheet::new("Sheet1");
sheet.set_value(CellRef::new(0, 0), Value::text("sep=;"));
let text = |c: CellRef| sheet.value(c).as_text().unwrap_or("").to_string();
// The row is 7 bytes and fits; with the `sep=,` line it needs 14.
assert!(matches!(write_csv_limited(&sheet, b',', 7, text), Err(IoError::TooLarge(_))));
assert_eq!(write_csv_limited(&sheet, b',', 14, text).unwrap(), b"sep=,\r\nsep=;\r\n");
}
/// Review of #2270: a first row that looks like a `sep=` line (a lone `sep=;` in A1) is data. Saved
/// behind a `sep=,` line it opens as it was; Excel writes it bare and reads it back garbled.
#[test]
+1 -1
View File
@@ -104,7 +104,7 @@ past the parser's limit, and a wrapper inside the text. **Saving writes the real
| Format | Excel | GridCraft read | GridCraft write | Notes |
|---|---|---|---|---|
| `.csv` UTF-8 / Windows / Mac / MS-DOS | R/W | yes (UTF-8 and UTF-16 BOMs, UTF-8, likely Windows-1256 Arabic detection, otherwise Windows-1252 fallback; delimiter detection, or the one an Excel `sep=` first line names, #1446; per-column day/month date-order detection for `/` and `-` dates) | yes (UTF-8 without BOM, CRLF; a file opened as CSV is saved with the separator it was read with, so a `;` file stays `;`; otherwise `,`) | Excel writes the **displayed** values, and so do we; `file.exportCsv {values: true}` writes raw values (full precision, ISO 8601 dates) for automation. No encoding choice on save. Re-import of a single-column export splits text (#127) A used range that would write more than 512 MiB (two far-apart cells span the whole rectangle) is refused with an error instead of being expanded (#528). |
| `.csv` UTF-8 / Windows / Mac / MS-DOS | R/W | yes (UTF-8 and UTF-16 BOMs, UTF-8, likely Windows-1256 Arabic detection, otherwise Windows-1252 fallback; delimiter detection, or the one an Excel `sep=` first line names, #1446; per-column day/month date-order detection for `/` and `-` dates) | yes (UTF-8 without BOM, CRLF; a file opened as CSV is saved with the separator it was read with, so a `;` file stays `;`; otherwise `,`, also for a `sep=` delimiter that detection wouldn't pick again; a `sep=` line goes first when detection would read the saved text with another delimiter, #1446) | Excel writes the **displayed** values, and so do we; `file.exportCsv {values: true}` writes raw values (full precision, ISO 8601 dates) for automation. No encoding choice on save. Re-import of a single-column export splits text (#127) A used range that would write more than 512 MiB (two far-apart cells span the whole rectangle) is refused with an error instead of being expanded (#528). |
| `.txt` tab-delimited, Unicode text | R/W | yes (`.txt`, `.tsv`, `.tab`; likely Windows-1256 Arabic detection for invalid UTF-8; per-column day/month date-order detection for `/` and `-` dates) | yes | Text Import Wizard options partly via Text to Columns |
| `.csv` UTF-8 / Windows / Mac / MS-DOS | R/W | yes (UTF-8 and UTF-16 BOMs, UTF-8, likely Windows-1256 Arabic detection, otherwise Windows-1252 fallback; delimiter detection, or the one an Excel `sep=` first line names, #1446; per-column day/month date-order detection for `/` and `-` dates) | yes (UTF-8 without BOM, CRLF) | Writes the **displayed** values like Excel (number formats applied, formula results, never column-width `#####`); `file.exportCsv {values: true}` writes raw values instead. No encoding choice on save. Re-import of a single-column export splits text (#127) A used range that would write more than 512 MiB (two far-apart cells span the whole rectangle) is refused with an error instead of being expanded (#528). |
| `.txt` tab-delimited, Unicode text | R/W | yes (`.txt`, `.tsv`, `.tab`; likely Windows-1256 Arabic detection for invalid UTF-8; per-column day/month date-order detection for `/` and `-` dates) | yes (displayed values) | Text Import Wizard options partly via Text to Columns |