layerPath function

List<RedactionMatch> layerPath(
  1. String text,
  2. RedactionConfig cfg, {
  3. String? pathHint,
})

Scans text for credential path content.

When pathHint is a credential file path the whole text becomes a single match; otherwise inline credential path tokens are matched individually.

Implementation

List<RedactionMatch> layerPath(
  String text,
  RedactionConfig cfg, {
  String? pathHint,
}) {
  if (text.isEmpty) return const [];
  if (pathHint != null && isCredentialPath(pathHint)) {
    return [
      RedactionMatch(
        start: 0,
        end: text.length,
        layer: RedactionLayer.path,
        kindLabel: credentialFileLabel,
      ),
    ];
  }
  final lower = text.toLowerCase();
  if (!_mightContainCredentialPath(lower)) return const [];
  final matches = <RedactionMatch>[];
  for (final m in _inlineCredentialPath.allMatches(text)) {
    final match = RedactionMatch(
      start: m.start,
      end: m.end,
      layer: RedactionLayer.path,
      kindLabel: credentialFileLabel,
    );
    if (!matches.any(match.overlaps)) matches.add(match);
  }
  return matches;
}