| OLD | NEW |
| (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 analyzer.src.util.absolute_path; |
| 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 relative [suffix] to the given absolute [parent]. |
| 14 /// |
| 15 /// context.append('/path/to', 'foo'); // -> '/path/to/foo' |
| 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 /// Return the part of the absolute [path] after the last separator on the |
| 23 /// context's platform. |
| 24 /// |
| 25 /// context.basename('/path/to/foo.dart'); // -> 'foo.dart' |
| 26 /// context.basename('/path/to'); // -> 'to' |
| 27 /// context.basename('/path'); // -> 'path' |
| 28 /// context.basename('/'); // -> '' |
| 29 String basename(String path) { |
| 30 int index = path.lastIndexOf(separator); |
| 31 return path.substring(index + 1); |
| 32 } |
| 33 |
| 34 /// Return the part of the absolute [path] before the last separator. |
| 35 /// |
| 36 /// context.dirname('/path/to/foo.dart'); // -> '/path/to' |
| 37 /// context.dirname('/path/to'); // -> '/path' |
| 38 /// context.dirname(r'/path'); // -> '/' |
| 39 /// context.dirname(r'/'); // -> '/' |
| 40 /// context.dirname(r'C:\path'); // -> 'C:\' |
| 41 /// context.dirname(r'C:\'); // -> 'C:\' |
| 42 String dirname(String path) { |
| 43 int firstIndex = path.indexOf(separator); |
| 44 int lastIndex = path.lastIndexOf(separator); |
| 45 return lastIndex == firstIndex |
| 46 ? path.substring(0, firstIndex + 1) |
| 47 : path.substring(0, lastIndex); |
| 48 } |
| 49 |
| 50 /// Return `true` if [child] is a path beneath [parent], and `false` |
| 51 /// otherwise. Both the [child] and [parent] paths must be absolute paths. |
| 52 /// |
| 53 /// context.isWithin('/root/path', '/root/path/a'); // -> true |
| 54 /// context.isWithin('/root/path', '/root/other'); // -> false |
| 55 /// context.isWithin('/root/path', '/root/path'); // -> false |
| 56 bool isWithin(String parent, String child) { |
| 57 return child.startsWith(parent + separator); |
| 58 } |
| 59 |
| 60 /// Split [path] into its components using [separator]. |
| 61 /// |
| 62 /// context.split('/path/to/foo'); // -> ['', 'path', 'to', 'foo'] |
| 63 List<String> split(String path) { |
| 64 return path.split(separator); |
| 65 } |
| 66 |
| 67 /// If the given [child] is within the given [parent], then return the |
| 68 /// relative path from [parent] to [child]. Otherwise return `null`. Both |
| 69 /// the [child] and [parent] paths must be absolute paths. |
| 70 /// |
| 71 /// context.relative('/root/path/a/b.dart', '/root/path'); // -> 'a/b.dart
' |
| 72 /// context.relative('/root/other.dart', '/root/path'); // -> null |
| 73 String suffix(String child, String parent) { |
| 74 String parentPrefix = parent + separator; |
| 75 if (child.startsWith(parentPrefix)) { |
| 76 return child.substring(parentPrefix.length); |
| 77 } |
| 78 return null; |
| 79 } |
| 80 } |
| OLD | NEW |