| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 class _Path implements Path { | 5 class _Path implements Path { |
| 6 final String _path; | 6 final String _path; |
| 7 | 7 |
| 8 const _Path(String source) : _path = source; | 8 const _Path(String source) : _path = source; |
| 9 _Path.fromNative(String source) : _path = _clean(source); | 9 _Path.fromNative(String source) : _path = _clean(source); |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 static String _cleanWindows(source) { | 22 static String _cleanWindows(source) { |
| 23 // Change \ to /. | 23 // Change \ to /. |
| 24 var clean = source.replaceAll('\\', '/'); | 24 var clean = source.replaceAll('\\', '/'); |
| 25 // Add / before intial [Drive letter]: | 25 // Add / before intial [Drive letter]: |
| 26 if (clean.length >= 2 && clean[1] == ':') { | 26 if (clean.length >= 2 && clean[1] == ':') { |
| 27 clean = '/$clean'; | 27 clean = '/$clean'; |
| 28 } | 28 } |
| 29 return clean; | 29 return clean; |
| 30 } | 30 } |
| 31 | 31 |
| 32 bool get isEmpty() => _path.isEmpty(); | 32 bool get isEmpty => _path.isEmpty(); |
| 33 bool get isAbsolute() => _path.startsWith('/'); | 33 bool get isAbsolute => _path.startsWith('/'); |
| 34 bool get hasTrailingSeparator() => _path.endsWith('/'); | 34 bool get hasTrailingSeparator => _path.endsWith('/'); |
| 35 | 35 |
| 36 String toString() => _path; | 36 String toString() => _path; |
| 37 | 37 |
| 38 Path relativeTo(Path base) { | 38 Path relativeTo(Path base) { |
| 39 // Throws exception if an unimplemented or impossible case is reached. | 39 // Throws exception if an unimplemented or impossible case is reached. |
| 40 // Returns a path "relative" such that | 40 // Returns a path "relative" such that |
| 41 // base.join(relative) == this.canonicalize. | 41 // base.join(relative) == this.canonicalize. |
| 42 // Throws an exception if no such path exists, or the case is not | 42 // Throws an exception if no such path exists, or the case is not |
| 43 // implemented yet. | 43 // implemented yet. |
| 44 if (base.isAbsolute && _path.startsWith(base._path)) { | 44 if (base.isAbsolute && _path.startsWith(base._path)) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 70 return new Path('$_path/${further._path}').canonicalize(); | 70 return new Path('$_path/${further._path}').canonicalize(); |
| 71 } | 71 } |
| 72 | 72 |
| 73 // Note: The URI RFC names for these operations are normalize, resolve, and | 73 // Note: The URI RFC names for these operations are normalize, resolve, and |
| 74 // relativize. | 74 // relativize. |
| 75 Path canonicalize() { | 75 Path canonicalize() { |
| 76 if (isCanonical) return this; | 76 if (isCanonical) return this; |
| 77 return makeCanonical(); | 77 return makeCanonical(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 bool get isCanonical() { | 80 bool get isCanonical { |
| 81 // Contains no consecutive path separators. | 81 // Contains no consecutive path separators. |
| 82 // Contains no segments that are '.'. | 82 // Contains no segments that are '.'. |
| 83 // Absolute paths have no segments that are '..'. | 83 // Absolute paths have no segments that are '..'. |
| 84 // All '..' segments of a relative path are at the beginning. | 84 // All '..' segments of a relative path are at the beginning. |
| 85 if (isEmpty) return false; // The canonical form of '' is '.'. | 85 if (isEmpty) return false; // The canonical form of '' is '.'. |
| 86 if (_path == '.') return true; | 86 if (_path == '.') return true; |
| 87 List segs = _path.split('/'); // Don't mask the getter 'segments'. | 87 List segs = _path.split('/'); // Don't mask the getter 'segments'. |
| 88 if (segs[0] == '') { // Absolute path | 88 if (segs[0] == '') { // Absolute path |
| 89 segs[0] = null; // Faster than removeRange(). | 89 segs[0] = null; // Faster than removeRange(). |
| 90 } else { // A canonical relative path may start with .. segments. | 90 } else { // A canonical relative path may start with .. segments. |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 Path append(String finalSegment) { | 183 Path append(String finalSegment) { |
| 184 if (isEmpty) { | 184 if (isEmpty) { |
| 185 return new Path(finalSegment); | 185 return new Path(finalSegment); |
| 186 } else if (hasTrailingSeparator) { | 186 } else if (hasTrailingSeparator) { |
| 187 return new Path('$_path$finalSegment'); | 187 return new Path('$_path$finalSegment'); |
| 188 } else { | 188 } else { |
| 189 return new Path('$_path/$finalSegment'); | 189 return new Path('$_path/$finalSegment'); |
| 190 } | 190 } |
| 191 } | 191 } |
| 192 | 192 |
| 193 String get filenameWithoutExtension() { | 193 String get filenameWithoutExtension { |
| 194 var name = filename; | 194 var name = filename; |
| 195 if (name == '.' || name == '..') return name; | 195 if (name == '.' || name == '..') return name; |
| 196 int pos = name.lastIndexOf('.'); | 196 int pos = name.lastIndexOf('.'); |
| 197 return (pos < 0) ? name : name.substring(0, pos); | 197 return (pos < 0) ? name : name.substring(0, pos); |
| 198 } | 198 } |
| 199 | 199 |
| 200 String get extension() { | 200 String get extension { |
| 201 var name = filename; | 201 var name = filename; |
| 202 int pos = name.lastIndexOf('.'); | 202 int pos = name.lastIndexOf('.'); |
| 203 return (pos < 0) ? '' : name.substring(pos + 1); | 203 return (pos < 0) ? '' : name.substring(pos + 1); |
| 204 } | 204 } |
| 205 | 205 |
| 206 Path get directoryPath() { | 206 Path get directoryPath { |
| 207 int pos = _path.lastIndexOf('/'); | 207 int pos = _path.lastIndexOf('/'); |
| 208 if (pos < 0) return new Path(''); | 208 if (pos < 0) return new Path(''); |
| 209 while (pos > 0 && _path[pos - 1] == '/') --pos; | 209 while (pos > 0 && _path[pos - 1] == '/') --pos; |
| 210 return new Path((pos > 0) ? _path.substring(0, pos) : '/'); | 210 return new Path((pos > 0) ? _path.substring(0, pos) : '/'); |
| 211 } | 211 } |
| 212 | 212 |
| 213 String get filename() { | 213 String get filename { |
| 214 int pos = _path.lastIndexOf('/'); | 214 int pos = _path.lastIndexOf('/'); |
| 215 return _path.substring(pos + 1); | 215 return _path.substring(pos + 1); |
| 216 } | 216 } |
| 217 } | 217 } |
| OLD | NEW |