layerPem function

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

Finds PEM/X.509/PGP blocks in text.

Complete blocks report their header label ('PEM CERTIFICATE', ...); a begin marker without an end marker reports one span reaching the end of the text.

Implementation

List<RedactionMatch> layerPem(String text, RedactionConfig cfg) {
  if (text.isEmpty || !text.contains('-----BEGIN')) return const [];
  final matches = <RedactionMatch>[];
  void add(int start, int end, String label) {
    final match = RedactionMatch(
      start: start,
      end: end,
      layer: RedactionLayer.pem,
      kindLabel: 'PEM $label',
    );
    if (!matches.any(match.overlaps)) matches.add(match);
  }

  for (final m in _pemBlock.allMatches(text)) {
    add(m.start, m.end, m.group(1)!);
  }
  // Truncated blocks: a begin marker not covered by any complete block
  // masks everything up to the end of the text.
  for (final b in _pemBegin.allMatches(text)) {
    final covered = matches.any((m) => m.start <= b.start && b.start < m.end);
    if (!covered) add(b.start, text.length, b.group(1)!);
  }
  return matches;
}