normalize method
path normalized to an absolute spelling: every separator becomes
separator and every . and .. segment is resolved against the root
driveRoot gives. Excess .. at the root are dropped; the spelling of
the root itself is kept.
Implementation
String normalize(String path) {
final String? root = driveRoot(path);
final String head = root == null ? path : path.substring(root.length);
final List<String> result = <String>[];
for (final String segment in head.split(RegExp(r'[/\\]'))) {
if (segment.isEmpty || segment == '.') {
continue;
}
if (segment == '..') {
if (result.isNotEmpty) {
result.removeLast();
}
continue;
}
result.add(segment);
}
if (root == null) {
return result.join(separator);
}
final String tail = result.join(separator);
final bool rooted = root.endsWith(r'\') || root.endsWith('/');
return rooted ? '$root$tail' : '$root$separator$tail';
}