splitPath function
Splits path into (directory, basename, extension).
splitPath('/foo/bar.txt') => ('/foo', 'bar', '.txt')
Implementation
({String dir, String basename, String ext}) splitPath(String path) {
final normalized = normalizePath(path);
final lastSlash = normalized.lastIndexOf('/');
final dir = lastSlash == -1 ? '.' : normalized.substring(0, lastSlash);
final file = lastSlash == -1
? normalized
: normalized.substring(lastSlash + 1);
final dotIdx = file.lastIndexOf('.');
if (dotIdx <= 0) {
return (dir: dir, basename: file, ext: '');
}
return (
dir: dir,
basename: file.substring(0, dotIdx),
ext: file.substring(dotIdx),
);
}