pathWithoutExtension function
Returns path with its trailing file extension (and the dot) removed.
A leading dot (dotfile like .gitignore) is not treated as an extension,
so such paths are returned unchanged.
Example:
pathWithoutExtension('archive.tar.gz'); // 'archive.tar'
pathWithoutExtension('.gitignore'); // '.gitignore'
Audited: 2026-06-12 11:26 EDT
Implementation
String pathWithoutExtension(String path) {
// Confine the extension dot to the final segment, like pathExtension: the old
// whole-path search made pathWithoutExtension('/a.b/c') return '/a' by cutting
// at the dot in the directory name 'a.b'.
final int slash = path.lastIndexOf('/');
final int backslash = path.lastIndexOf(r'\');
final int sep = slash > backslash ? slash : backslash;
final int dotIndex = path.lastIndexOf('.');
if (dotIndex <= sep + 1) return path;
return path.substring(0, dotIndex);
}