splitPathAndSel function

SplitReadPath splitPathAndSel(
  1. String rawPath
)

Splits a trailing :sel off rawPath when the tail matches the selector grammar (a range list or raw, optionally one of each in either order — path:1-50:raw / path:raw:1-50). Anything else stays part of the path so archive and SQLite targets keep their colon syntax.

Implementation

SplitReadPath splitPathAndSel(String rawPath) {
  final colon = rawPath.lastIndexOf(':');
  if (colon <= 0) return SplitReadPath(rawPath);

  final candidate = rawPath.substring(colon + 1);
  if (!_fileLineRangeRe.hasMatch(candidate)) return SplitReadPath(rawPath);

  var basePath = rawPath.substring(0, colon);
  var sel = candidate;

  // Allow a compound trailing selector: `path:1-50:raw` or `path:raw:1-50`.
  // The two chunks must be one line-range plus one `raw`, in either order.
  final innerColon = basePath.lastIndexOf(':');
  if (innerColon > 0) {
    final innerCandidate = basePath.substring(innerColon + 1);
    final innerIsRaw = _fileRawOnlyRe.hasMatch(innerCandidate);
    final outerIsRaw = _fileRawOnlyRe.hasMatch(candidate);
    final innerIsRange = _fileLineRangeOnlyRe.hasMatch(innerCandidate);
    final outerIsRange = _fileLineRangeOnlyRe.hasMatch(candidate);
    if ((innerIsRaw && outerIsRange) || (innerIsRange && outerIsRaw)) {
      sel = '$innerCandidate:$candidate';
      basePath = basePath.substring(0, innerColon);
    }
  }

  return SplitReadPath(basePath, sel);
}