detectDelimiter static method

String detectDelimiter(
  1. String content
)

Auto-detects the most likely delimiter from CSV / TSV / DSV content.

Implementation

static String detectDelimiter(String content) {
  if (content.isEmpty) return ',';

  String text = content;
  if (text.startsWith('\uFEFF')) {
    text = text.substring(1);
  }

  // Look across the first up to 10 non-empty lines for robust detection
  final lines = text.split(RegExp(r'\r\n|\r|\n')).where((line) => line.trim().isNotEmpty).take(10).toList();

  if (lines.isEmpty) return ',';

  final candidates = TCsvDelimiter.allCandidates;
  final candidateScores = <String, int>{for (var c in candidates) c: 0};

  for (final delimiter in candidates) {
    int totalCount = 0;
    for (final line in lines) {
      int lineCount = 0;
      bool insideQuote = false;
      for (int i = 0; i < line.length; i++) {
        final char = line[i];
        if (char == '"') {
          insideQuote = !insideQuote;
        } else if (!insideQuote && char == delimiter) {
          lineCount++;
        }
      }
      totalCount += lineCount;
    }
    candidateScores[delimiter] = totalCount;
  }

  int maxScore = -1;
  String bestDelimiter = ',';

  for (final delimiter in candidates) {
    final score = candidateScores[delimiter] ?? 0;
    if (score > maxScore) {
      maxScore = score;
      bestDelimiter = delimiter;
    }
  }

  return maxScore > 0 ? bestDelimiter : ',';
}