Chromium Code Reviews| 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..1cd75c01f6e34f1c2f1798ddd749d54bdfc6f08a |
| --- /dev/null |
| +++ b/pkg/analyzer/lib/src/util/absolute_path.dart |
| @@ -0,0 +1,70 @@ |
| +// 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 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.
|
| + |
| +/// The class for manipulating absolute paths. |
| +class AbsolutePathContext { |
| + final String separator; |
| + |
| + AbsolutePathContext(this.separator); |
| + |
| + /// 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.
|
| + /// |
| + /// 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.
|
| + /// |
| + /// The given [suffix] cannot be an absolute path or use `..`. |
| + String append(String parent, String suffix) { |
| + return '$parent$separator$suffix'; |
| + } |
| + |
| + /// 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.
|
| + /// platform. |
| + /// |
| + /// context.basename('/path/to/foo.dart'); // -> 'foo.dart' |
| + /// context.basename('/path/to'); // -> 'to' |
| + String basename(String path) { |
| + int index = path.lastIndexOf(separator); |
| + return path.substring(index + 1); |
| + } |
| + |
| + /// 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.
|
| + /// |
| + /// context.dirname('/path/to/foo.dart'); // -> '/path/to' |
| + /// context.dirname('/path/to'); // -> '/path' |
| + String dirname(String path) { |
| + int index = path.lastIndexOf(separator); |
| + return index != -1 ? path.substring(0, index) : index; |
| + } |
| + |
| + /// Returns `true` if [child] is a path beneath `parent`, and `false` |
| + /// otherwise. |
|
Brian Wilkerson
2015/11/14 17:30:25
"`parent`" --> "[parent]"
"." --> ". Both the [chi
scheglov
2015/11/14 22:40:12
Done.
|
| + /// |
| + /// 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); |
| + } |
| + |
| + /// If the given [child] is within the given [parent], then returns the |
| + /// 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.
|
| + /// |
| + /// context.relative('/root/path/a/b.dart', '/root/path'); // -> 'a/b.dart' |
| + /// context.relative('/root/other.dart', '/root/path'); // -> null |
| + String relative(String child, String parent) { |
| + String parentPrefix = parent + separator; |
| + if (child.startsWith(parentPrefix)) { |
| + return child.substring(parentPrefix.length); |
| + } |
| + return null; |
| + } |
| + |
| + /// Splits [path] into its components using [separator]. |
| + /// |
| + /// context.split('/path/to/foo'); // -> ['', 'path', 'to', 'foo'] |
| + List<String> split(String path) { |
| + return path.split(separator); |
| + } |
| +} |