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 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 class _Path implements Path { | 7 class _Path implements Path { |
8 final String _path; | 8 final String _path; |
9 final bool isWindowsShare; | 9 final bool isWindowsShare; |
10 | 10 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 var basePath = base.toString(); | 53 var basePath = base.toString(); |
54 if (base.isAbsolute && _path.startsWith(basePath) && | 54 if (base.isAbsolute && _path.startsWith(basePath) && |
55 base.isWindowsShare == isWindowsShare) { | 55 base.isWindowsShare == isWindowsShare) { |
56 if (_path == basePath) return new Path('.'); | 56 if (_path == basePath) return new Path('.'); |
57 if (base.hasTrailingSeparator) { | 57 if (base.hasTrailingSeparator) { |
58 return new Path(_path.substring(basePath.length)); | 58 return new Path(_path.substring(basePath.length)); |
59 } | 59 } |
60 if (_path[basePath.length] == '/') { | 60 if (_path[basePath.length] == '/') { |
61 return new Path(_path.substring(basePath.length + 1)); | 61 return new Path(_path.substring(basePath.length + 1)); |
62 } | 62 } |
63 } else if (base.isAbsolute && isAbsolute && | 63 } else if (base.isAbsolute == isAbsolute && |
64 base.isWindowsShare == isWindowsShare) { | 64 base.isWindowsShare == isWindowsShare) { |
65 List<String> baseSegments = base.canonicalize().segments(); | 65 List<String> baseSegments = base.canonicalize().segments(); |
66 List<String> pathSegments = canonicalize().segments(); | 66 List<String> pathSegments = canonicalize().segments(); |
67 if (baseSegments.length == 1 && baseSegments[0] == '.') { | |
68 baseSegments = []; | |
69 } | |
70 if (pathSegments.length == 1 && pathSegments[0] == '.') { | |
71 pathSegments = []; | |
72 } | |
67 int common = 0; | 73 int common = 0; |
68 int length = min(pathSegments.length, baseSegments.length); | 74 int length = min(pathSegments.length, baseSegments.length); |
69 while (common < length && pathSegments[common] == baseSegments[common]) { | 75 while (common < length && pathSegments[common] == baseSegments[common]) { |
Bob Nystrom
2013/01/16 18:42:38
I believe this will do the wrong thing if you have
| |
70 common++; | 76 common++; |
71 } | 77 } |
72 final sb = new StringBuffer(); | 78 final segments = new List<String>(); |
73 | 79 |
74 for (int i = common + 1; i < baseSegments.length; i++) { | 80 if (common < baseSegments.length && baseSegments[common] == '..') { |
75 sb.add('../'); | 81 throw new ArgumentError( |
82 "Invalid case of Path.relativeTo(base):\n" | |
83 " Base path has more '..'s than path does." | |
84 " Arguments: $_path.relativeTo($base)"); | |
Bob Nystrom
2013/01/16 18:42:38
Throwing an ArgumentError here is a trip-wire for
| |
76 } | 85 } |
77 if (base.hasTrailingSeparator) { | 86 for (int i = common; i < baseSegments.length; i++) { |
78 sb.add('../'); | 87 segments.add('..'); |
79 } | 88 } |
80 for (int i = common; i < pathSegments.length - 1; i++) { | 89 for (int i = common; i < pathSegments.length; i++) { |
81 sb.add('${pathSegments[i]}/'); | 90 segments.add('${pathSegments[i]}'); |
82 } | 91 } |
83 sb.add('${pathSegments.last}'); | 92 if (segments.isEmpty) { |
93 segments.add('.'); | |
94 } | |
84 if (hasTrailingSeparator) { | 95 if (hasTrailingSeparator) { |
85 sb.add('/'); | 96 segments.add(''); |
86 } | 97 } |
87 return new Path(sb.toString()); | 98 return new Path(Strings.join(segments, '/')); |
88 } | 99 } |
89 throw new UnimplementedError( | 100 throw new ArgumentError( |
90 "Unimplemented case of Path.relativeTo(base):\n" | 101 "Invalid case of Path.relativeTo(base):\n" |
91 " Only absolute paths are handled at present.\n" | 102 " Path and base must both be relative, or both absolute.\n" |
92 " Arguments: $_path.relativeTo($base)"); | 103 " Arguments: $_path.relativeTo($base)"); |
Bob Nystrom
2013/01/16 18:42:38
Ditto here. This will give users a bad day.
pkg/p
| |
93 } | 104 } |
94 | 105 |
95 Path join(Path further) { | 106 Path join(Path further) { |
96 if (further.isAbsolute) { | 107 if (further.isAbsolute) { |
97 throw new ArgumentError( | 108 throw new ArgumentError( |
98 "Path.join called with absolute Path as argument."); | 109 "Path.join called with absolute Path as argument."); |
99 } | 110 } |
100 if (isEmpty) { | 111 if (isEmpty) { |
101 return further.canonicalize(); | 112 return further.canonicalize(); |
102 } | 113 } |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
253 while (pos > 0 && _path[pos - 1] == '/') --pos; | 264 while (pos > 0 && _path[pos - 1] == '/') --pos; |
254 var dirPath = (pos > 0) ? _path.substring(0, pos) : '/'; | 265 var dirPath = (pos > 0) ? _path.substring(0, pos) : '/'; |
255 return new _Path._internal(dirPath, isWindowsShare); | 266 return new _Path._internal(dirPath, isWindowsShare); |
256 } | 267 } |
257 | 268 |
258 String get filename { | 269 String get filename { |
259 int pos = _path.lastIndexOf('/'); | 270 int pos = _path.lastIndexOf('/'); |
260 return _path.substring(pos + 1); | 271 return _path.substring(pos + 1); |
261 } | 272 } |
262 } | 273 } |
OLD | NEW |