| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, 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 /** | |
| 6 * A Path, which is a String interpreted as a sequence of path segments, | |
| 7 * which are strings, separated by forward slashes. | |
| 8 * Paths are immutable wrappers of a String, that offer member functions for | |
| 9 * useful path manipulations and queries. Joining of paths and normalization | |
| 10 * interpret '.' and '..' in the usual way. | |
| 11 */ | |
| 12 abstract class Path { | |
| 13 /** | |
| 14 * Creates a Path from the String [source]. [source] is used as-is, so if | |
| 15 * the string does not consist of segments separated by forward slashes, the | |
| 16 * behavior may not be as expected. Paths are immutable. | |
| 17 */ | |
| 18 factory Path(String source) => new _Path(source); | |
| 19 | |
| 20 /** | |
| 21 * Creates a Path from a String that uses the native filesystem's conventions. | |
| 22 * On Windows, this converts '\' to '/', and adds a '/' before a drive letter. | |
| 23 * A path starting with '/c:/' (or any other character instead of 'c') is | |
| 24 * treated specially. Backwards links ('..') cannot cancel the drive letter. | |
| 25 */ | |
| 26 factory Path.fromNative(String source) => new _Path.fromNative(source); | |
| 27 | |
| 28 /** | |
| 29 * Is this path the empty string? | |
| 30 */ | |
| 31 bool get isEmpty; | |
| 32 | |
| 33 /** | |
| 34 * Is this path an absolute path, beginning with a path separator? | |
| 35 */ | |
| 36 bool get isAbsolute; | |
| 37 | |
| 38 /** | |
| 39 * Does this path end with a path separator? | |
| 40 */ | |
| 41 bool get hasTrailingSeparator; | |
| 42 | |
| 43 /** | |
| 44 * Does this path contain no consecutive path separators, no segments that | |
| 45 * are '.' unless the path is exactly '.', and segments that are '..' only | |
| 46 * as the leading segments on a relative path? | |
| 47 */ | |
| 48 bool get isCanonical; | |
| 49 | |
| 50 /** | |
| 51 * Make a path canonical by dropping segments that are '.', cancelling | |
| 52 * segments that are '..' with preceding segments, if possible, | |
| 53 * and combining consecutive path separators. Leading '..' segments | |
| 54 * are kept on relative paths, and dropped from absolute paths. | |
| 55 */ | |
| 56 Path canonicalize(); | |
| 57 | |
| 58 /** | |
| 59 * Joins the relative path [further] to this path. Canonicalizes the | |
| 60 * resulting joined path using [canonicalize], | |
| 61 * interpreting '.' and '..' as directory traversal commands, and removing | |
| 62 * consecutive path separators. | |
| 63 * | |
| 64 * If [further] is an absolute path, an IllegalArgument exception is thrown. | |
| 65 * | |
| 66 * Examples: | |
| 67 * `new Path('/a/b/c').join(new Path('d/e'))` returns the Path object | |
| 68 * containing `'a/b/c/d/e'`. | |
| 69 * | |
| 70 * `new Path('a/b/../c/').join(new Path('d/./e//')` returns the Path | |
| 71 * containing `'a/c/d/e/'`. | |
| 72 * | |
| 73 * `new Path('a/b/c').join(new Path('d/../../e')` returns the Path | |
| 74 * containing `'a/b/e'`. | |
| 75 * | |
| 76 * Note that the join operation does not drop the last segment of the | |
| 77 * base path, the way URL joining does. That would be accomplished with | |
| 78 * basepath.directoryPath.join(further). | |
| 79 * | |
| 80 * If you want to avoid joins that traverse | |
| 81 * parent directories in the base, you can check whether | |
| 82 * `further.canonicalize()` starts with '../' or equals '..'. | |
| 83 */ | |
| 84 Path join(Path further); | |
| 85 | |
| 86 | |
| 87 /** | |
| 88 * Returns a path [:relative:] such that | |
| 89 * [:base.join(relative) == this.canonicalize():]. | |
| 90 * Throws an exception if such a path is impossible. | |
| 91 * For example, if [base] is '../../a/b' and [this] is '.'. | |
| 92 * The computation is independent of the file system and current directory. | |
| 93 */ | |
| 94 Path relativeTo(Path base); | |
| 95 | |
| 96 /** | |
| 97 * Converts a path to a string using the native filesystem's conventions. | |
| 98 * | |
| 99 * On Windows, converts path separators to backwards slashes, and removes | |
| 100 * the leading path separator if the path starts with a drive specification. | |
| 101 * For most valid Windows paths, this should be the inverse of the | |
| 102 * constructor Path.fromNative. | |
| 103 */ | |
| 104 String toNativePath(); | |
| 105 | |
| 106 /** | |
| 107 * Returns the path as a string. If this path is constructed using | |
| 108 * new Path() or new Path.fromNative() on a non-Windows system, the | |
| 109 * returned value is the original string argument to the constructor. | |
| 110 */ | |
| 111 String toString(); | |
| 112 | |
| 113 /** | |
| 114 * Gets the segments of a Path. Paths beginning or ending with the | |
| 115 * path separator do not have leading or terminating empty segments. | |
| 116 * Other than that, the segments are just the result of splitting the | |
| 117 * path on the path separator. | |
| 118 * | |
| 119 * new Path('/a/b/c/d').segments() == ['a', 'b', 'c', d']; | |
| 120 * new Path(' foo bar //../') == [' foo bar ', '', '..']; | |
| 121 */ | |
| 122 List<String> segments(); | |
| 123 | |
| 124 /** | |
| 125 * Appends [finalSegment] to a path as a new segment. Adds a path separator | |
| 126 * between the path and [finalSegment] if the path does not already end in | |
| 127 * a path separator. The path is not canonicalized, and [finalSegment] may | |
| 128 * contain path separators. | |
| 129 */ | |
| 130 Path append(String finalSegment); | |
| 131 | |
| 132 /** | |
| 133 * Drops the final path separator and whatever follows it from this Path, | |
| 134 * and returns the resulting Path object. If the only path separator in | |
| 135 * this Path is the first character, returns '/' instead of the empty string. | |
| 136 * If there is no path separator in the Path, returns the empty string. | |
| 137 * | |
| 138 * new Path('../images/dot.gif').directoryPath == '../images' | |
| 139 * new Path('/usr/geoffrey/www/').directoryPath == '/usr/geoffrey/www' | |
| 140 * new Path('lost_file_old').directoryPath == '' | |
| 141 * new Path('/src').directoryPath == '/' | |
| 142 * Note: new Path('/D:/src').directoryPath == '/D:' | |
| 143 */ | |
| 144 Path get directoryPath; | |
| 145 | |
| 146 /** | |
| 147 * The part of the path after the last path separator, or the entire path if | |
| 148 * it contains no path separator. | |
| 149 * | |
| 150 * new Path('images/DSC_0027.jpg).filename == 'DSC_0027.jpg' | |
| 151 * new Path('users/fred/').filename == '' | |
| 152 */ | |
| 153 String get filename; | |
| 154 | |
| 155 /** | |
| 156 * The part of [filename] before the last '.', or the entire filename if it | |
| 157 * contains no '.'. If [filename] is '.' or '..' it is unchanged. | |
| 158 * | |
| 159 * new Path('/c:/My Documents/Heidi.txt').filenameWithoutExtension | |
| 160 * would return 'Heidi'. | |
| 161 * new Path('not what I would call a path').filenameWithoutExtension | |
| 162 * would return 'not what I would call a path'. | |
| 163 */ | |
| 164 String get filenameWithoutExtension; | |
| 165 | |
| 166 /** | |
| 167 * The part of [filename] after the last '.', or '' if [filename] | |
| 168 * contains no '.'. If [filename] is '.' or '..', returns ''. | |
| 169 * | |
| 170 * new Path('tiger.svg').extension == 'svg' | |
| 171 * new Path('/src/dart/dart_secrets').extension == '' | |
| 172 */ | |
| 173 String get extension; | |
| 174 } | |
| OLD | NEW |