removeDotSegments function

String removeDotSegments(
  1. String path
)

RFC 3986 Section 5.2.4: Remove Dot Segments

This algorithm removes "." and ".." segments from a path.

Implementation

String removeDotSegments(String path) {
  if (path.isEmpty) return path;

  final input = path.split('/');
  final output = <String>[];

  for (final segment in input) {
    if (segment == '.') {
      // Skip "." segments
      continue;
    } else if (segment == '..') {
      // Go up one level for ".." segments
      if (output.isNotEmpty && output.last != '') {
        output.removeLast();
      }
    } else {
      output.add(segment);
    }
  }

  // Reconstruct path
  if (path.startsWith('/') && (output.isEmpty || output.first != '')) {
    return '/${output.join('/')}';
  }
  return output.join('/');
}