pathNormalize function

String pathNormalize(
  1. String path
)

Normalize path (resolve . and ..). Roadmap #162. Audited: 2026-06-12 11:26 EDT

Implementation

String pathNormalize(String path) {
  // Normalize separators (backslash -> '/', collapse runs), then resolve the
  // segments: '.' and empty drop out, '..' pops the previous real segment.
  final String p = path.replaceAll(r'\', '/').replaceAll(RegExp(r'/+'), '/');
  final List<String> parts = p.split('/');
  final List<String> out = <String>[];
  for (final String seg in parts) {
    if (seg.isEmpty || seg == '.') continue;
    if (seg == '..') {
      // Pop the parent; a leading '..' with nothing to pop is dropped (this does
      // not produce '../' prefixes, matching pathJoin's behavior).
      if (out.isNotEmpty) out.removeLast();
      continue;
    }
    out.add(seg);
  }
  return out.join('/');
}