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 library legacy_path; | 5 library legacy_path; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 import 'dart:math'; | 8 import 'dart:math'; |
9 | 9 |
10 // TODO: Remove this class, and use the URI class for all path manipulation. | 10 // TODO: Remove this class, and use the URI class for all path manipulation. |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 return makeCanonical(); | 167 return makeCanonical(); |
168 } | 168 } |
169 | 169 |
170 bool get isCanonical { | 170 bool get isCanonical { |
171 // Contains no consecutive path separators. | 171 // Contains no consecutive path separators. |
172 // Contains no segments that are '.'. | 172 // Contains no segments that are '.'. |
173 // Absolute paths have no segments that are '..'. | 173 // Absolute paths have no segments that are '..'. |
174 // All '..' segments of a relative path are at the beginning. | 174 // All '..' segments of a relative path are at the beginning. |
175 if (isEmpty) return false; // The canonical form of '' is '.'. | 175 if (isEmpty) return false; // The canonical form of '' is '.'. |
176 if (_path == '.') return true; | 176 if (_path == '.') return true; |
177 List segs = _path.split('/'); // Don't mask the getter 'segments'. | 177 var segs = _path.split('/'); // Don't mask the getter 'segments'. |
178 if (segs[0] == '') { | 178 if (segs[0] == '') { |
179 // Absolute path | 179 // Absolute path |
180 segs[0] = null; // Faster than removeRange(). | 180 segs[0] = null; // Faster than removeRange(). |
181 } else { | 181 } else { |
182 // A canonical relative path may start with .. segments. | 182 // A canonical relative path may start with .. segments. |
183 for (int pos = 0; pos < segs.length && segs[pos] == '..'; ++pos) { | 183 for (int pos = 0; pos < segs.length && segs[pos] == '..'; ++pos) { |
184 segs[pos] = null; | 184 segs[pos] = null; |
185 } | 185 } |
186 } | 186 } |
187 if (segs.last == '') segs.removeLast(); // Path ends with /. | 187 if (segs.last == '') segs.removeLast(); // Path ends with /. |
188 // No remaining segments can be ., .., or empty. | 188 // No remaining segments can be ., .., or empty. |
189 return !segs.any((s) => s == '' || s == '.' || s == '..'); | 189 return !segs.any((s) => s == '' || s == '.' || s == '..'); |
190 } | 190 } |
191 | 191 |
192 Path makeCanonical() { | 192 Path makeCanonical() { |
193 bool isAbs = isAbsolute; | 193 var isAbs = isAbsolute; |
194 List segs = segments(); | 194 var segs = segments(); |
195 String drive; | 195 String drive; |
196 if (isAbs && !segs.isEmpty && segs[0].length == 2 && segs[0][1] == ':') { | 196 if (isAbs && !segs.isEmpty && segs[0].length == 2 && segs[0][1] == ':') { |
197 drive = segs[0]; | 197 drive = segs[0]; |
198 segs.removeRange(0, 1); | 198 segs.removeRange(0, 1); |
199 } | 199 } |
200 List newSegs = []; | 200 var newSegs = <String>[]; |
201 for (String segment in segs) { | 201 for (var segment in segs) { |
202 switch (segment) { | 202 switch (segment) { |
203 case '..': | 203 case '..': |
204 // Absolute paths drop leading .. markers, including after a drive. | 204 // Absolute paths drop leading .. markers, including after a drive. |
205 if (newSegs.isEmpty) { | 205 if (newSegs.isEmpty) { |
206 if (isAbs) { | 206 if (isAbs) { |
207 // Do nothing: drop the segment. | 207 // Do nothing: drop the segment. |
208 } else { | 208 } else { |
209 newSegs.add('..'); | 209 newSegs.add('..'); |
210 } | 210 } |
211 } else if (newSegs.last == '..') { | 211 } else if (newSegs.last == '..') { |
212 newSegs.add('..'); | 212 newSegs.add('..'); |
213 } else { | 213 } else { |
214 newSegs.removeLast(); | 214 newSegs.removeLast(); |
215 } | 215 } |
216 break; | 216 break; |
217 case '.': | 217 case '.': |
218 case '': | 218 case '': |
219 // Do nothing - drop the segment. | 219 // Do nothing - drop the segment. |
220 break; | 220 break; |
221 default: | 221 default: |
222 newSegs.add(segment); | 222 newSegs.add(segment); |
223 break; | 223 break; |
224 } | 224 } |
225 } | 225 } |
226 | 226 |
227 List segmentsToJoin = []; | 227 var segmentsToJoin = <String>[]; |
228 if (isAbs) { | 228 if (isAbs) { |
229 segmentsToJoin.add(''); | 229 segmentsToJoin.add(''); |
230 if (drive != null) { | 230 if (drive != null) { |
231 segmentsToJoin.add(drive); | 231 segmentsToJoin.add(drive); |
232 } | 232 } |
233 } | 233 } |
234 | 234 |
235 if (newSegs.isEmpty) { | 235 if (newSegs.isEmpty) { |
236 if (isAbs) { | 236 if (isAbs) { |
237 segmentsToJoin.add(''); | 237 segmentsToJoin.add(''); |
(...skipping 22 matching lines...) Expand all Loading... |
260 nativePath = nativePath.replaceAll('/', '\\'); | 260 nativePath = nativePath.replaceAll('/', '\\'); |
261 if (isWindowsShare) { | 261 if (isWindowsShare) { |
262 return '\\$nativePath'; | 262 return '\\$nativePath'; |
263 } | 263 } |
264 return nativePath; | 264 return nativePath; |
265 } | 265 } |
266 return _path; | 266 return _path; |
267 } | 267 } |
268 | 268 |
269 List<String> segments() { | 269 List<String> segments() { |
270 List result = _path.split('/'); | 270 var result = _path.split('/'); |
271 if (isAbsolute) result.removeRange(0, 1); | 271 if (isAbsolute) result.removeRange(0, 1); |
272 if (hasTrailingSeparator) result.removeLast(); | 272 if (hasTrailingSeparator) result.removeLast(); |
273 return result; | 273 return result; |
274 } | 274 } |
275 | 275 |
276 Path append(String finalSegment) { | 276 Path append(String finalSegment) { |
277 if (isEmpty) { | 277 if (isEmpty) { |
278 return new Path._internal(finalSegment, isWindowsShare); | 278 return new Path._internal(finalSegment, isWindowsShare); |
279 } else if (hasTrailingSeparator) { | 279 } else if (hasTrailingSeparator) { |
280 return new Path._internal('$_path$finalSegment', isWindowsShare); | 280 return new Path._internal('$_path$finalSegment', isWindowsShare); |
(...skipping 21 matching lines...) Expand all Loading... |
302 while (pos > 0 && _path[pos - 1] == '/') --pos; | 302 while (pos > 0 && _path[pos - 1] == '/') --pos; |
303 var dirPath = (pos > 0) ? _path.substring(0, pos) : '/'; | 303 var dirPath = (pos > 0) ? _path.substring(0, pos) : '/'; |
304 return new Path._internal(dirPath, isWindowsShare); | 304 return new Path._internal(dirPath, isWindowsShare); |
305 } | 305 } |
306 | 306 |
307 String get filename { | 307 String get filename { |
308 int pos = _path.lastIndexOf('/'); | 308 int pos = _path.lastIndexOf('/'); |
309 return _path.substring(pos + 1); | 309 return _path.substring(pos + 1); |
310 } | 310 } |
311 } | 311 } |
OLD | NEW |