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

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: Change name to absolute, return type to File, Directory, and Link. 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 | « sdk/lib/io/file_impl.dart ('k') | sdk/lib/io/link.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..ab2b7d30c61f630e84bf8419d31195e999924123 100644
--- a/sdk/lib/io/file_system_entity.dart
+++ b/sdk/lib/io/file_system_entity.dart
@@ -343,6 +343,45 @@ 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 a [FileSystemEntity] whose path is the absolute path to [this].
+ * The type of the returned instance is the type of [this].
+ *
+ * The absolute path is computed by prefixing
+ * a relative path with the current working directory, and returning
+ * an absolute path unchanged.
+ */
+ FileSystemEntity get absolute;
+
+ String get _absolutePath {
+ 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 | « sdk/lib/io/file_impl.dart ('k') | sdk/lib/io/link.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698