Метод parentOf
- String path
Путь родительского каталога.
Находит конечный компонент пути, используя разделитель путей платформы для разделения пути, и возвращает префикс до этой части.
Не будет удалять корневой компонент пути Windows, например, «C:\» или «\\server_name\». Включает конечный разделитель пути в последней части path, и не оставляет конечного разделителя пути.
Реализация
static String parentOf(String path) {
int rootEnd = -1;
if (Platform.isWindows) {
if (path.startsWith(_absoluteWindowsPathPattern)) {
// Root ends at first / or \ after the first two characters.
rootEnd = path.indexOf(new RegExp(r'[/\\]'), 2);
if (rootEnd == -1) return path;
} else if (path.startsWith('\\') || path.startsWith('/')) {
rootEnd = 0;
}
} else if (path.startsWith('/')) {
rootEnd = 0;
}
// Ignore trailing slashes.
// All non-trivial cases have separators between two non-separators.
int pos = path.lastIndexOf(_parentRegExp);
if (pos > rootEnd) {
return path.substring(0, pos + 1);
} else if (rootEnd > -1) {
return path.substring(0, rootEnd + 1);
} else {
return '.';
}
}
© 2012 the Dart project authors
Licensed under the BSD 3-Clause "New" or "Revised" License.
https://api.dart.dev/stable/2.18.5/dart-io/FileSystemEntity/parentOf.html