detectCsvDialect function
Detects the dialect of a CSV/TSV sample by inspecting its first line.
Counts tabs versus commas in the first line; if the line contains tabs and
they are at least as common as commas, the delimiter is tab, otherwise
comma. The first row is always reported as a header. A first line with no
tabs (including an empty sample) yields a comma delimiter.
Example:
detectCsvDialect('a\tb\tc').delimiter; // '\t'
detectCsvDialect('a,b,c').delimiter; // ','
Implementation
CsvDialectUtils detectCsvDialect(String sample) {
final String first = sample.split('\n').first;
final int commas = ','.allMatches(first).length;
final int tabs = '\t'.allMatches(first).length;
// Comma is the default: only pick tab when tabs are actually present (so an
// empty or comma-only line yields ',', not '\t' from the 0 >= 0 tie).
final String delimiter = tabs > 0 && tabs >= commas ? '\t' : ',';
return CsvDialectUtils(delimiter: delimiter, hasHeader: true);
}