craft-csv: delimiter detection samples 65,536 characters, not 64 KiB (#2326)

sniff_delimiter read the first 64 KiB of the text. A first field of
three-byte characters longer than about 21,800 characters filled that
sample before its delimiter, so a semicolon or tab file opened as
comma-separated. The sample is now the first 65,536 characters, the
amount detection read before it moved into craft-csv.

The new test fails without the change: a first field of 65,535
three-byte characters followed by a semicolon is detected as comma.
This commit is contained in:
.CooCoo
2026-11-18 23:20:42 -05:00
committed by GitHub
parent 36ca4574e4
commit 4e0c880f58
2 changed files with 13 additions and 7 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ mapping a SYLK sheet onto a document are the caller's work.
| Function | What it does |
|---|---|
| `decode_text(bytes)` | UTF-8 and UTF-16 byte order marks; without one, valid UTF-8 is kept; other bytes are Windows-1256 when they read as Arabic, else Windows-1252 |
| `sniff_delimiter(text, quote)` | comma, semicolon or tab: the one with the most consistent field count over the first 20 records with other text in a 64 KiB sample; when none separates those records, the candidates whose records hold only their delimiters and whitespace (rows of empty cells) are compared the same way, leaving out any candidate that found a record with other text; when no candidate finds coherent complete records with a delimiter in each, a candidate that finds no complete record with other text counts the delimiters of the first such record up to the end of the sample, or up to a quoted field still open there (after its rows of empty cells, a record without its delimiter is left out), and a quote inside a field that does not start with one makes that record incoherent; quote boundaries must be coherent, and ties prefer tab, comma, then semicolon |
| `sniff_delimiter(text, quote)` | comma, semicolon or tab: the one with the most consistent field count over the first 20 records with other text in a sample of the first 65,536 characters; when none separates those records, the candidates whose records hold only their delimiters and whitespace (rows of empty cells) are compared the same way, leaving out any candidate that found a record with other text; when no candidate finds coherent complete records with a delimiter in each, a candidate that finds no complete record with other text counts the delimiters of the first such record up to the end of the sample, or up to a quoted field still open there (after its rows of empty cells, a record without its delimiter is left out), and a quote inside a field that does not start with one makes that record incoherent; quote boundaries must be coherent, and ties prefer tab, comma, then semicolon |
| `parse_records(text, delim, quote, max_fields, on_record)` | records of fields, one callback per record (return `false` to stop); CR, LF and CRLF line ends; quoted fields with doubled quotes and line breaks; a quote in the middle of a field is text |
| `records(text, delim, quote, max_records, max_fields)` | the same, collected |
| `push_field(out, field, delim)`, `field_len(field, delim)` | one field as written, quoted only when it holds the delimiter, a quote or a line break; its exact length, to enforce a size cap before writing |
+12 -6
View File
@@ -37,7 +37,7 @@ pub use decode::{decode_text, decode_utf8};
/// the same rules pick among the candidates whose records hold only their delimiters and
/// whitespace, such as an exported row of empty cells; a candidate that finds a record with
/// other text is left out of that pick. Comma is the fallback when neither pick finds one.
/// Only the first 64 KiB are read. A record cut off at the end of that sample is left out,
/// Only the first 65,536 characters are read. A record cut off at the end of that sample is left out,
/// except when no candidate finds coherent complete records with a delimiter in each. Then a candidate
/// that finds no complete record with other text counts the delimiters read so far in the first
/// such record, unless it found rows of its empty cells and that record has none of its
@@ -45,11 +45,8 @@ pub use decode::{decode_text, decode_utf8};
/// with one. When one of those candidates ends the sample inside a quoted field, each of them
/// counts only up to that field's opening quote.
pub fn sniff_delimiter(text: &str, quote: char) -> u8 {
const SAMPLE_BYTES: usize = 64 * 1024;
let mut sample_end = text.len().min(SAMPLE_BYTES);
while !text.is_char_boundary(sample_end) {
sample_end -= 1;
}
const SAMPLE_CHARS: usize = 64 * 1024;
let sample_end = text.char_indices().nth(SAMPLE_CHARS).map_or(text.len(), |(at, _)| at);
let sample = &text[..sample_end];
let truncated = sample_end < text.len();
let mut scans = b"\t,;".map(|delimiter| scan_delimiter(sample, delimiter, quote));
@@ -441,6 +438,15 @@ mod tests {
assert_eq!(sniff_delimiter(&format!("a,b,c;\"hello,\";d\n{}", "x;y;z\n".repeat(12_000)), '"'), b';');
}
#[test]
fn delimiter_sniffing_samples_characters_not_bytes() {
// 30,000 three-byte characters are 90,000 bytes, more than 64 KiB.
assert_eq!(sniff_delimiter(&format!("{};b;c\n1;2;3\n", "€".repeat(30_000)), '"'), b';');
// The 65,536th character is in the sample and the 65,537th is not.
assert_eq!(sniff_delimiter(&format!("{};x\n1;2\n", "€".repeat(65_535)), '"'), b';');
assert_eq!(sniff_delimiter(&format!("{};x\n1;2\n", "€".repeat(65_536)), '"'), b',');
}
// A sheet whose only value is far down exports rows of empty cells that fill the whole sample.
#[test]
fn delimiter_sniffing_reads_a_sparse_semicolon_export() {