| 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 _Path(String source) : _path = source; | 8 _Path(String source) : _path = source; |
| 9 _Path.fromNative(String source) : _path = _clean(source); | 9 _Path.fromNative(String source) : _path = _clean(source); |
| 10 | 10 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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)) { |
| 45 if (_path == base._path) return new Path('.'); | 45 if (_path == base._path) return new Path('.'); |
| 46 if (base.hasTrailingSeparator) { | 46 if (base.hasTrailingSeparator) { |
| 47 return new Path(_path.substring(base._path.length)); | 47 return new Path(_path.substring(base._path.length)); |
| 48 } | 48 } |
| 49 if (_path[base._path.length] == '/') { | 49 if (_path[base._path.length] == '/') { |
| 50 return new Path(_path.substring(base._path.length + 1)); | 50 return new Path(_path.substring(base._path.length + 1)); |
| 51 } | 51 } |
| 52 } else if (base.isAbsolute && isAbsolute) { |
| 53 List<String> baseSegments = base.canonicalize().segments(); |
| 54 List<String> pathSegments = canonicalize().segments(); |
| 55 int common = 0; |
| 56 int length = min(pathSegments.length, baseSegments.length); |
| 57 while (common < length && pathSegments[common] == baseSegments[common]) { |
| 58 common++; |
| 59 } |
| 60 final sb = new StringBuffer(); |
| 61 |
| 62 for (int i = common + 1; i < baseSegments.length; i++) { |
| 63 sb.add('../'); |
| 64 } |
| 65 if (base.hasTrailingSeparator) { |
| 66 sb.add('../'); |
| 67 } |
| 68 for (int i = common; i < pathSegments.length - 1; i++) { |
| 69 sb.add('${pathSegments[i]}/'); |
| 70 } |
| 71 sb.add('${pathSegments.last()}'); |
| 72 if (_path.hasTrailingSeparator) { |
| 73 sb.add('/'); |
| 74 } |
| 75 return new Path(sb.toString()); |
| 52 } | 76 } |
| 53 throw new NotImplementedException( | 77 throw new NotImplementedException( |
| 54 "Unimplemented case of Path.relativeTo(base):\n" | 78 "Unimplemented case of Path.relativeTo(base):\n" |
| 55 " Only absolute paths with strict containment are handled at present.\n" | 79 " Only absolute paths are handled at present.\n" |
| 56 " Arguments: $_path.relativeTo($base)"); | 80 " Arguments: $_path.relativeTo($base)"); |
| 57 } | 81 } |
| 58 | 82 |
| 59 Path join(Path further) { | 83 Path join(Path further) { |
| 60 if (further.isAbsolute) { | 84 if (further.isAbsolute) { |
| 61 throw new ArgumentError( | 85 throw new ArgumentError( |
| 62 "Path.join called with absolute Path as argument."); | 86 "Path.join called with absolute Path as argument."); |
| 63 } | 87 } |
| 64 if (isEmpty) { | 88 if (isEmpty) { |
| 65 return further.canonicalize(); | 89 return further.canonicalize(); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 if (pos < 0) return new Path(''); | 234 if (pos < 0) return new Path(''); |
| 211 while (pos > 0 && _path[pos - 1] == '/') --pos; | 235 while (pos > 0 && _path[pos - 1] == '/') --pos; |
| 212 return new Path((pos > 0) ? _path.substring(0, pos) : '/'); | 236 return new Path((pos > 0) ? _path.substring(0, pos) : '/'); |
| 213 } | 237 } |
| 214 | 238 |
| 215 String get filename { | 239 String get filename { |
| 216 int pos = _path.lastIndexOf('/'); | 240 int pos = _path.lastIndexOf('/'); |
| 217 return _path.substring(pos + 1); | 241 return _path.substring(pos + 1); |
| 218 } | 242 } |
| 219 } | 243 } |
| OLD | NEW |