layerPii function

List<RedactionMatch> layerPii(
  1. String text,
  2. RedactionConfig cfg
)

Finds PII spans in text; non-overlapping, left-to-right.

Implementation

List<RedactionMatch> layerPii(String text, RedactionConfig cfg) {
  if (text.isEmpty) return const [];
  final matches = <RedactionMatch>[];
  void add(int start, int end, String label) {
    final match = RedactionMatch(
      start: start,
      end: end,
      layer: RedactionLayer.pii,
      kindLabel: label,
    );
    if (!matches.any(match.overlaps)) matches.add(match);
  }

  for (final m in _email.allMatches(text)) {
    add(m.start, m.end, 'Email');
  }
  for (final pattern in [_phoneUs, _phoneRu, _phoneTrunk]) {
    for (final m in pattern.allMatches(text)) {
      add(m.start, m.end, 'Phone');
    }
  }
  _addCardMatches(text, add);
  for (final m in _ssn.allMatches(text)) {
    add(m.start, m.end, 'SSN');
  }
  for (final m in _iban.allMatches(text)) {
    add(m.start, m.end, 'IBAN');
  }
  return matches;
}