Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Unified Diff: sdk/lib/io/file_system_entity.dart

Issue 26001002: dart:io | Add FileSystemEntity.parent, make File.directory call it. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix Windows tests. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/io/file_impl.dart ('k') | tests/standalone/io/parent_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/io/file_system_entity.dart
diff --git a/sdk/lib/io/file_system_entity.dart b/sdk/lib/io/file_system_entity.dart
index fdeb923c6de41dc31715e1dc13192afcd6546ee2..2594ff5e285068f67d2cd5eb9d3e915108970fee 100644
--- a/sdk/lib/io/file_system_entity.dart
+++ b/sdk/lib/io/file_system_entity.dart
@@ -530,6 +530,48 @@ abstract class FileSystemEntity {
external static _identical(String path1, String path2);
external static _resolveSymbolicLinks(String path);
+ // Finds the next-to-last component when dividing at path separators.
+ static final RegExp _parentRegExp = Platform.isWindows ?
+ new RegExp(r'[^/\\][/\\]+[^/\\]') :
+ new RegExp(r'[^/]/+[^/]');
+
+ /**
+ * Removes the final path component of a path, using the platform's
+ * path separator to split the path. Will not remove the root component
+ * of a Windows path, like "C:\" or "\\server_name\".
+ * Ignores trailing path separators, and leaves no trailing path separators.
+ */
+ 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 '.';
+ }
+ }
+
+ /**
+ * The directory containing [this]. If [this] is a root
+ * directory, returns [this].
+ */
+ Directory get parent => new Directory(parentOf(path));
+
static int _getTypeSync(String path, bool followLinks) {
var result = _getType(path, followLinks);
_throwIfError(result, 'Error getting type of FileSystemEntity');
@@ -537,12 +579,13 @@ abstract class FileSystemEntity {
}
static Future<int> _getTypeAsync(String path, bool followLinks) {
- return _IOService.dispatch(_FILE_TYPE, [path, followLinks]).then((response) {
- if (_isErrorResponse(response)) {
- throw _exceptionFromResponse(response, "Error getting type", path);
- }
- return response;
- });
+ return _IOService.dispatch(_FILE_TYPE, [path, followLinks])
+ .then((response) {
+ if (_isErrorResponse(response)) {
+ throw _exceptionFromResponse(response, "Error getting type", path);
+ }
+ return response;
+ });
}
static _throwIfError(Object result, String msg, [String path]) {
« no previous file with comments | « sdk/lib/io/file_impl.dart ('k') | tests/standalone/io/parent_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698