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

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

Issue 23468027: Add FileSystemEntity.absolutePath and .isAbsolute properties. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix Windows Created 7 years, 3 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 | « no previous file | tests/standalone/io/file_absolute_path_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 0887175e859713a681c37ffc5c57d96c66a0cc3a..ebf00575dd1081d3ce1f00a008336c18f5ca47b2 100644
--- a/sdk/lib/io/file_system_entity.dart
+++ b/sdk/lib/io/file_system_entity.dart
@@ -343,6 +343,40 @@ abstract class FileSystemEntity {
});
}
+ static final RegExp _absoluteWindowsPathPattern =
+ new RegExp(r'^(\\\\|[a-zA-Z]:[/\\])');
+
+ /**
+ * Returns a [bool] indicating whether this object's path is absolute.
+ *
+ * On Windows, a path is absolute if it starts with \\ or a drive letter
+ * between a and z (upper or lower case) followed by :\ or :/.
+ * On non-Windows, a path is absolute if it starts with /.
+ */
+ bool get isAbsolute {
+ if (Platform.isWindows) {
+ return path.startsWith(_absoluteWindowsPathPattern);
+ } else {
+ return path.startsWith('/');
+ }
+ }
+
+ /**
+ * Returns the absolute path to this file system entity, by prefixing
+ * a relative path with the current working directory, and returning
+ * an absolute path unchanged.
+ */
+ String get absolutePath {
Søren Gjesse 2013/09/13 06:56:08 Thinking about this once more. Shouldn't this retu
+ if (isAbsolute) return path;
+ String current = Directory.current.path;
+ if (current.endsWith('/') ||
+ (Platform.isWindows && current.endsWith('\\'))) {
+ return '$current$path';
+ } else {
+ return '$current${Platform.pathSeparator}$path';
+ }
+ }
+
/**
* Synchronously checks whether two paths refer to the same object in the
« no previous file with comments | « no previous file | tests/standalone/io/file_absolute_path_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698