extract method

List<String> extract(
  1. String content
)

Retourne les candidats uniques, dans l'ordre de première apparition.

Implementation

List<String> extract(String content) {
  final seen = <String>{};
  var depth = 0;
  var start = -1; // Début du candidat en cours, ou -1.

  void flush(int end) {
    if (start < 0) return;
    // Un candidat trop long n'est jamais une classe : inutile de le copier.
    if (end - start <= maxCandidateLength) {
      final candidate = _trim(content.substring(start, end));
      if (candidate.isNotEmpty) seen.add(candidate);
    }
    start = -1;
  }

  for (var i = 0; i < content.length; i++) {
    final code = content.codeUnitAt(i);
    if (code == 0x5B) {
      depth++;
    } else if (code == 0x5D && depth > 0) {
      depth--;
    }

    // Dans des crochets, seuls les blancs et guillemets ferment le candidat
    // (les virgules, `=` ou `;` restent permis : `[rgb(0,0,0)]`).
    final isBreak = _isHardBreak(code) || (depth == 0 && _isSoftBreak(code));

    if (isBreak) {
      flush(i);
      depth = 0;
    } else if (start < 0) {
      start = i;
    }
  }
  flush(content.length);

  return seen.toList();
}