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

Side by Side 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: 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library engine.utilities.absolute_path;
Brian Wilkerson 2015/11/14 17:30:25 The library name should be "analyzer.src.util.abso
scheglov 2015/11/14 22:40:12 Done.
6
7 /// The class for manipulating absolute paths.
8 class AbsolutePathContext {
9 final String separator;
10
11 AbsolutePathContext(this.separator);
12
13 /// Append the given [suffix] the given absolute [parent].
Brian Wilkerson 2015/11/14 17:30:25 "[suffix]" --> "relative [suffix] to"
scheglov 2015/11/14 22:40:12 Done.
14 ///
15 /// context.append('/path/to', 'foo'); // -> 'path/to/foo'
Brian Wilkerson 2015/11/14 17:30:24 Missing leading '/' in result.
scheglov 2015/11/14 22:40:12 Done.
16 ///
17 /// The given [suffix] cannot be an absolute path or use `..`.
18 String append(String parent, String suffix) {
19 return '$parent$separator$suffix';
20 }
21
22 /// Gets the part of [path] after the last separator on the context's
Brian Wilkerson 2015/11/14 17:30:25 "Gets" --> "Return" "[path]" --> "the absolute [pa
scheglov 2015/11/14 22:40:12 Done.
23 /// platform.
24 ///
25 /// context.basename('/path/to/foo.dart'); // -> 'foo.dart'
26 /// context.basename('/path/to'); // -> 'to'
27 String basename(String path) {
28 int index = path.lastIndexOf(separator);
29 return path.substring(index + 1);
30 }
31
32 /// Gets the part of [path] before the last separator.
Brian Wilkerson 2015/11/14 17:30:25 "Gets" --> "Return" "[path]" --> "the absolute [pa
scheglov 2015/11/14 22:40:12 Done.
33 ///
34 /// context.dirname('/path/to/foo.dart'); // -> '/path/to'
35 /// context.dirname('/path/to'); // -> '/path'
36 String dirname(String path) {
37 int index = path.lastIndexOf(separator);
38 return index != -1 ? path.substring(0, index) : index;
39 }
40
41 /// Returns `true` if [child] is a path beneath `parent`, and `false`
42 /// otherwise.
Brian Wilkerson 2015/11/14 17:30:25 "`parent`" --> "[parent]" "." --> ". Both the [chi
scheglov 2015/11/14 22:40:12 Done.
43 ///
44 /// context.isWithin('/root/path', '/root/path/a'); // -> true
45 /// context.isWithin('/root/path', '/root/other'); // -> false
46 /// context.isWithin('/root/path', '/root/path'); // -> false
47 bool isWithin(String parent, String child) {
48 return child.startsWith(parent + separator);
49 }
50
51 /// If the given [child] is within the given [parent], then returns the
52 /// relative path from [parent] to [child]. Otherwise returns `null`.
Brian Wilkerson 2015/11/14 17:30:24 "`." --> "`. Both the [child] and [parent] paths m
scheglov 2015/11/14 22:40:12 Done.
53 ///
54 /// context.relative('/root/path/a/b.dart', '/root/path'); // -> 'a/b.dart '
55 /// context.relative('/root/other.dart', '/root/path'); // -> null
56 String relative(String child, String parent) {
57 String parentPrefix = parent + separator;
58 if (child.startsWith(parentPrefix)) {
59 return child.substring(parentPrefix.length);
60 }
61 return null;
62 }
63
64 /// Splits [path] into its components using [separator].
65 ///
66 /// context.split('/path/to/foo'); // -> ['', 'path', 'to', 'foo']
67 List<String> split(String path) {
68 return path.split(separator);
69 }
70 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698