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

Unified Diff: pkg/analyzer/lib/src/util/absolute_path.dart

Issue 1443173004: Optimize work with absolute paths. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Updates for review comments. Created 5 years, 1 month 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
Index: pkg/analyzer/lib/src/util/absolute_path.dart
diff --git a/pkg/analyzer/lib/src/util/absolute_path.dart b/pkg/analyzer/lib/src/util/absolute_path.dart
new file mode 100644
index 0000000000000000000000000000000000000000..5052538767a19cd7d4f42c3c590e5b0f79749531
--- /dev/null
+++ b/pkg/analyzer/lib/src/util/absolute_path.dart
@@ -0,0 +1,80 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library analyzer.src.util.absolute_path;
+
+/// The class for manipulating absolute paths.
+class AbsolutePathContext {
+ final String separator;
+
+ AbsolutePathContext(this.separator);
+
+ /// Append the given relative [suffix] to the given absolute [parent].
+ ///
+ /// context.append('/path/to', 'foo'); // -> '/path/to/foo'
+ ///
+ /// The given [suffix] cannot be an absolute path or use `..`.
+ String append(String parent, String suffix) {
+ return '$parent$separator$suffix';
+ }
+
+ /// Return the part of the absolute [path] after the last separator on the
+ /// context's platform.
+ ///
+ /// context.basename('/path/to/foo.dart'); // -> 'foo.dart'
+ /// context.basename('/path/to'); // -> 'to'
+ /// context.basename('/path'); // -> 'path'
+ /// context.basename('/'); // -> ''
+ String basename(String path) {
+ int index = path.lastIndexOf(separator);
+ return path.substring(index + 1);
+ }
+
+ /// Return the part of the absolute [path] before the last separator.
+ ///
+ /// context.dirname('/path/to/foo.dart'); // -> '/path/to'
+ /// context.dirname('/path/to'); // -> '/path'
+ /// context.dirname(r'/path'); // -> '/'
+ /// context.dirname(r'/'); // -> '/'
+ /// context.dirname(r'C:\path'); // -> 'C:\'
+ /// context.dirname(r'C:\'); // -> 'C:\'
+ String dirname(String path) {
+ int firstIndex = path.indexOf(separator);
+ int lastIndex = path.lastIndexOf(separator);
+ return lastIndex == firstIndex
+ ? path.substring(0, firstIndex + 1)
+ : path.substring(0, lastIndex);
+ }
+
+ /// Return `true` if [child] is a path beneath [parent], and `false`
+ /// otherwise. Both the [child] and [parent] paths must be absolute paths.
+ ///
+ /// context.isWithin('/root/path', '/root/path/a'); // -> true
+ /// context.isWithin('/root/path', '/root/other'); // -> false
+ /// context.isWithin('/root/path', '/root/path'); // -> false
+ bool isWithin(String parent, String child) {
+ return child.startsWith(parent + separator);
+ }
+
+ /// Split [path] into its components using [separator].
+ ///
+ /// context.split('/path/to/foo'); // -> ['', 'path', 'to', 'foo']
+ List<String> split(String path) {
+ return path.split(separator);
+ }
+
+ /// If the given [child] is within the given [parent], then return the
+ /// relative path from [parent] to [child]. Otherwise return `null`. Both
+ /// the [child] and [parent] paths must be absolute paths.
+ ///
+ /// context.relative('/root/path/a/b.dart', '/root/path'); // -> 'a/b.dart'
+ /// context.relative('/root/other.dart', '/root/path'); // -> null
+ String suffix(String child, String parent) {
+ String parentPrefix = parent + separator;
+ if (child.startsWith(parentPrefix)) {
+ return child.substring(parentPrefix.length);
+ }
+ return null;
+ }
+}
« no previous file with comments | « pkg/analysis_server/lib/src/context_manager.dart ('k') | pkg/analyzer/test/src/util/absolute_path_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698