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 /// A comprehensive, cross-platform path manipulation library. | |
6 library path; | |
7 | |
8 import 'dart:io' as io; | |
9 | |
10 /// An internal builder for the current OS so we can provide a straight | |
11 /// functional interface and not require users to create one. | |
12 final _builder = new Builder(); | |
13 | |
14 /// Gets the path to the current working directory. | |
15 String get current => new io.Directory.current().path; | |
16 | |
17 /// Gets the path separator for the current platform. On Mac and Linux, this | |
18 /// is `/`. On Windows, it's `\`. | |
19 String get separator => _builder.separator; | |
20 | |
21 /// Converts [path] to an absolute path by resolving it relative to the current | |
22 /// working directory. If [path] is already an absolute path, just returns it. | |
23 /// | |
24 /// path.absolute('foo/bar.txt'); // -> /your/current/dir/foo/bar.txt | |
25 String absolute(String path) => join(current, path); | |
26 | |
27 /// Gets the part of [path] after the last separator. | |
28 /// | |
29 /// path.basename('path/to/foo.dart'); // -> 'foo.dart' | |
30 /// path.basename('path/to'); // -> 'to' | |
31 /// | |
32 /// Trailing separators are ignored. | |
33 /// | |
34 /// builder.basename('path/to/'); // -> 'to' | |
35 String basename(String path) => _builder.basename(path); | |
36 | |
37 /// Gets the part of [path] after the last separator, and without any trailing | |
38 /// file extension. | |
39 /// | |
40 /// path.basenameWithoutExtension('path/to/foo.dart'); // -> 'foo' | |
41 /// | |
42 /// Trailing separators are ignored. | |
43 /// | |
44 /// builder.basenameWithoutExtension('path/to/foo.dart/'); // -> 'foo' | |
45 String basenameWithoutExtension(String path) => | |
46 _builder.basenameWithoutExtension(path); | |
47 | |
48 /// Gets the part of [path] before the last separator. | |
49 /// | |
50 /// path.dirname('path/to/foo.dart'); // -> 'path/to' | |
51 /// path.dirname('path/to'); // -> 'to' | |
52 /// | |
53 /// Trailing separators are ignored. | |
54 /// | |
55 /// builder.dirname('path/to/'); // -> 'path' | |
56 String dirname(String path) => _builder.dirname(path); | |
57 | |
58 /// Gets the file extension of [path]: the portion of [basename] from the last | |
59 /// `.` to the end (including the `.` itself). | |
60 /// | |
61 /// path.extension('path/to/foo.dart'); // -> '.dart' | |
62 /// path.extension('path/to/foo'); // -> '' | |
63 /// path.extension('path.to/foo'); // -> '' | |
64 /// path.extension('path/to/foo.dart.js'); // -> '.js' | |
65 /// | |
66 /// If the file name starts with a `.`, then that is not considered the | |
67 /// extension: | |
68 /// | |
69 /// path.extension('~/.bashrc'); // -> '' | |
70 /// path.extension('~/.notes.txt'); // -> '.txt' | |
71 String extension(String path) => _builder.extension(path); | |
72 | |
73 // TODO(nweiz): add a UNC example for Windows once issue 7323 is fixed. | |
74 /// Returns the root of [path], if it's absolute, or the empty string if it's | |
75 /// relative. | |
76 /// | |
77 /// // Unix | |
78 /// path.rootPrefix('path/to/foo'); // -> '' | |
79 /// path.rootPrefix('/path/to/foo'); // -> '/' | |
80 /// | |
81 /// // Windows | |
82 /// path.rootPrefix(r'path\to\foo'); // -> '' | |
83 /// path.rootPrefix(r'C:\path\to\foo'); // -> r'C:\' | |
84 String rootPrefix(String path) => _builder.rootPrefix(path); | |
85 | |
86 /// Returns `true` if [path] is an absolute path and `false` if it is a | |
87 /// relative path. On POSIX systems, absolute paths start with a `/` (forward | |
88 /// slash). On Windows, an absolute path starts with `\\`, or a drive letter | |
89 /// followed by `:/` or `:\`. | |
90 bool isAbsolute(String path) => _builder.isAbsolute(path); | |
91 | |
92 /// Returns `true` if [path] is a relative path and `false` if it is absolute. | |
93 /// On POSIX systems, absolute paths start with a `/` (forward slash). On | |
94 /// Windows, an absolute path starts with `\\`, or a drive letter followed by | |
95 /// `:/` or `:\`. | |
96 bool isRelative(String path) => _builder.isRelative(path); | |
97 | |
98 /// Joins the given path parts into a single path using the current platform's | |
99 /// [separator]. Example: | |
100 /// | |
101 /// path.join('path', 'to', 'foo'); // -> 'path/to/foo' | |
102 /// | |
103 /// If any part ends in a path separator, then a redundant separator will not | |
104 /// be added: | |
105 /// | |
106 /// path.join('path/', 'to', 'foo'); // -> 'path/to/foo | |
107 /// | |
108 /// If a part is an absolute path, then anything before that will be ignored: | |
109 /// | |
110 /// path.join('path', '/to', 'foo'); // -> '/to/foo' | |
111 String join(String part1, [String part2, String part3, String part4, | |
112 String part5, String part6, String part7, String part8]) => | |
113 _builder.join(part1, part2, part3, part4, part5, part6, part7, part8); | |
114 | |
115 // TODO(nweiz): add a UNC example for Windows once issue 7323 is fixed. | |
116 /// Splits [path] into its components using the current platform's [separator]. | |
117 /// | |
118 /// path.split('path/to/foo'); // -> ['path', 'to', 'foo'] | |
119 /// | |
120 /// The path will *not* be normalized before splitting. | |
121 /// | |
122 /// path.split('path/../foo'); // -> ['path', '..', 'foo'] | |
123 /// | |
124 /// If [path] is absolute, the root directory will be the first element in the | |
125 /// array. Example: | |
126 /// | |
127 /// // Unix | |
128 /// path.split('/path/to/foo'); // -> ['/', 'path', 'to', 'foo'] | |
129 /// | |
130 /// // Windows | |
131 /// path.split(r'C:\path\to\foo'); // -> [r'C:\', 'path', 'to', 'foo'] | |
132 List<String> split(String path) => _builder.split(path); | |
133 | |
134 /// Normalizes [path], simplifying it by handling `..`, and `.`, and | |
135 /// removing redundant path separators whenever possible. | |
136 /// | |
137 /// path.normalize('path/./to/..//file.text'); // -> 'path/file.txt' | |
138 String normalize(String path) => _builder.normalize(path); | |
139 | |
140 /// Attempts to convert [path] to an equivalent relative path from the current | |
141 /// directory. | |
142 /// | |
143 /// // Given current directory is /root/path: | |
144 /// path.relative('/root/path/a/b.dart'); // -> 'a/b.dart' | |
145 /// path.relative('/root/other.dart'); // -> '../other.dart' | |
146 /// | |
147 /// If the [from] argument is passed, [path] is made relative to that instead. | |
148 /// | |
149 /// path.relative('/root/path/a/b.dart', | |
150 /// from: '/root/path'); // -> 'a/b.dart' | |
151 /// path.relative('/root/other.dart', | |
152 /// from: '/root/path'); // -> '../other.dart' | |
153 /// | |
154 /// Since there is no relative path from one drive letter to another on Windows, | |
155 /// this will return an absolute path in that case. | |
156 /// | |
157 /// path.relative(r'D:\other', from: r'C:\home'); // -> 'D:\other' | |
158 String relative(String path, {String from}) => | |
159 _builder.relative(path, from: from); | |
160 | |
161 /// Removes a trailing extension from the last part of [path]. | |
162 /// | |
163 /// withoutExtension('path/to/foo.dart'); // -> 'path/to/foo' | |
164 String withoutExtension(String path) => _builder.withoutExtension(path); | |
165 | |
166 /// Validates that there are no non-null arguments following a null one and | |
167 /// throws an appropriate [ArgumentError] on failure. | |
168 _validateArgList(String method, List<String> args) { | |
169 for (var i = 1; i < args.length; i++) { | |
170 // Ignore nulls hanging off the end. | |
171 if (args[i] == null || args[i - 1] != null) continue; | |
172 | |
173 var numArgs; | |
174 for (numArgs = args.length; numArgs >= 1; numArgs--) { | |
175 if (args[numArgs - 1] != null) break; | |
176 } | |
177 | |
178 // Show the arguments. | |
179 var message = new StringBuffer(); | |
180 message.add("$method("); | |
181 message.add(args.take(numArgs) | |
182 .map((arg) => arg == null ? "null" : '"$arg"') | |
183 .join(", ")); | |
184 message.add("): part ${i - 1} was null, but part $i was not."); | |
185 throw new ArgumentError(message.toString()); | |
186 } | |
187 } | |
188 | |
189 /// An instantiable class for manipulating paths. Unlike the top-level | |
190 /// functions, this lets you explicitly select what platform the paths will use. | |
191 class Builder { | |
192 /// Creates a new path builder for the given style and root directory. | |
193 /// | |
194 /// If [style] is omitted, it uses the host operating system's path style. If | |
195 /// [root] is omitted, it defaults to the current working directory. If [root] | |
196 /// is relative, it is considered relative to the current working directory. | |
197 factory Builder({Style style, String root}) { | |
198 if (style == null) { | |
199 if (io.Platform.operatingSystem == 'windows') { | |
200 style = Style.windows; | |
201 } else { | |
202 style = Style.posix; | |
203 } | |
204 } | |
205 | |
206 if (root == null) root = current; | |
207 | |
208 return new Builder._(style, root); | |
209 } | |
210 | |
211 Builder._(this.style, this.root); | |
212 | |
213 /// The style of path that this builder works with. | |
214 final Style style; | |
215 | |
216 /// The root directory that relative paths will be relative to. | |
217 final String root; | |
218 | |
219 /// Gets the path separator for the builder's [style]. On Mac and Linux, | |
220 /// this is `/`. On Windows, it's `\`. | |
221 String get separator => style.separator; | |
222 | |
223 /// Gets the part of [path] after the last separator on the builder's | |
224 /// platform. | |
225 /// | |
226 /// builder.basename('path/to/foo.dart'); // -> 'foo.dart' | |
227 /// builder.basename('path/to'); // -> 'to' | |
228 /// | |
229 /// Trailing separators are ignored. | |
230 /// | |
231 /// builder.dirname('path/to/'); // -> 'to' | |
232 String basename(String path) => _parse(path).basename; | |
233 | |
234 /// Gets the part of [path] after the last separator on the builder's | |
235 /// platform, and without any trailing file extension. | |
236 /// | |
237 /// builder.basenameWithoutExtension('path/to/foo.dart'); // -> 'foo' | |
238 /// | |
239 /// Trailing separators are ignored. | |
240 /// | |
241 /// builder.dirname('path/to/foo.dart/'); // -> 'foo' | |
242 String basenameWithoutExtension(String path) => | |
243 _parse(path).basenameWithoutExtension; | |
244 | |
245 /// Gets the part of [path] before the last separator. | |
246 /// | |
247 /// builder.dirname('path/to/foo.dart'); // -> 'path/to' | |
248 /// builder.dirname('path/to'); // -> 'path' | |
249 /// | |
250 /// Trailing separators are ignored. | |
251 /// | |
252 /// builder.dirname('path/to/'); // -> 'path' | |
253 String dirname(String path) { | |
254 var parsed = _parse(path); | |
255 parsed.removeTrailingSeparators(); | |
256 if (parsed.parts.isEmpty) return parsed.root == null ? '.' : parsed.root; | |
257 if (parsed.parts.length == 1) { | |
258 return parsed.root == null ? '.' : parsed.root; | |
259 } | |
260 parsed.parts.removeLast(); | |
261 parsed.separators.removeLast(); | |
262 parsed.removeTrailingSeparators(); | |
263 return parsed.toString(); | |
264 } | |
265 | |
266 /// Gets the file extension of [path]: the portion of [basename] from the last | |
267 /// `.` to the end (including the `.` itself). | |
268 /// | |
269 /// builder.extension('path/to/foo.dart'); // -> '.dart' | |
270 /// builder.extension('path/to/foo'); // -> '' | |
271 /// builder.extension('path.to/foo'); // -> '' | |
272 /// builder.extension('path/to/foo.dart.js'); // -> '.js' | |
273 /// | |
274 /// If the file name starts with a `.`, then it is not considered an | |
275 /// extension: | |
276 /// | |
277 /// builder.extension('~/.bashrc'); // -> '' | |
278 /// builder.extension('~/.notes.txt'); // -> '.txt' | |
279 String extension(String path) => _parse(path).extension; | |
280 | |
281 // TODO(nweiz): add a UNC example for Windows once issue 7323 is fixed. | |
282 /// Returns the root of [path], if it's absolute, or an empty string if it's | |
283 /// relative. | |
284 /// | |
285 /// // Unix | |
286 /// builder.rootPrefix('path/to/foo'); // -> '' | |
287 /// builder.rootPrefix('/path/to/foo'); // -> '/' | |
288 /// | |
289 /// // Windows | |
290 /// builder.rootPrefix(r'path\to\foo'); // -> '' | |
291 /// builder.rootPrefix(r'C:\path\to\foo'); // -> r'C:\' | |
292 String rootPrefix(String path) { | |
293 var root = _parse(path).root; | |
294 return root == null ? '' : root; | |
295 } | |
296 | |
297 /// Returns `true` if [path] is an absolute path and `false` if it is a | |
298 /// relative path. On POSIX systems, absolute paths start with a `/` (forward | |
299 /// slash). On Windows, an absolute path starts with `\\`, or a drive letter | |
300 /// followed by `:/` or `:\`. | |
301 bool isAbsolute(String path) => _parse(path).isAbsolute; | |
302 | |
303 /// Returns `true` if [path] is a relative path and `false` if it is absolute. | |
304 /// On POSIX systems, absolute paths start with a `/` (forward slash). On | |
305 /// Windows, an absolute path starts with `\\`, or a drive letter followed by | |
306 /// `:/` or `:\`. | |
307 bool isRelative(String path) => !isAbsolute(path); | |
308 | |
309 /// Joins the given path parts into a single path. Example: | |
310 /// | |
311 /// builder.join('path', 'to', 'foo'); // -> 'path/to/foo' | |
312 /// | |
313 /// If any part ends in a path separator, then a redundant separator will not | |
314 /// be added: | |
315 /// | |
316 /// builder.join('path/', 'to', 'foo'); // -> 'path/to/foo | |
317 /// | |
318 /// If a part is an absolute path, then anything before that will be ignored: | |
319 /// | |
320 /// builder.join('path', '/to', 'foo'); // -> '/to/foo' | |
321 /// | |
322 String join(String part1, [String part2, String part3, String part4, | |
323 String part5, String part6, String part7, String part8]) { | |
324 var buffer = new StringBuffer(); | |
325 var needsSeparator = false; | |
326 | |
327 var parts = [part1, part2, part3, part4, part5, part6, part7, part8]; | |
328 _validateArgList("join", parts); | |
329 | |
330 for (var part in parts) { | |
331 if (part == null) continue; | |
332 | |
333 if (this.isAbsolute(part)) { | |
334 // An absolute path discards everything before it. | |
335 buffer.clear(); | |
336 buffer.add(part); | |
337 } else { | |
338 if (part.length > 0 && part[0].contains(style.separatorPattern)) { | |
339 // The part starts with a separator, so we don't need to add one. | |
340 } else if (needsSeparator) { | |
341 buffer.add(separator); | |
342 } | |
343 | |
344 buffer.add(part); | |
345 } | |
346 | |
347 // Unless this part ends with a separator, we'll need to add one before | |
348 // the next part. | |
349 needsSeparator = part.length > 0 && | |
350 !part[part.length - 1].contains(style.separatorPattern); | |
351 } | |
352 | |
353 return buffer.toString(); | |
354 } | |
355 | |
356 // TODO(nweiz): add a UNC example for Windows once issue 7323 is fixed. | |
357 /// Splits [path] into its components using the current platform's | |
358 /// [separator]. Example: | |
359 /// | |
360 /// builder.split('path/to/foo'); // -> ['path', 'to', 'foo'] | |
361 /// | |
362 /// The path will *not* be normalized before splitting. | |
363 /// | |
364 /// builder.split('path/../foo'); // -> ['path', '..', 'foo'] | |
365 /// | |
366 /// If [path] is absolute, the root directory will be the first element in the | |
367 /// array. Example: | |
368 /// | |
369 /// // Unix | |
370 /// builder.split('/path/to/foo'); // -> ['/', 'path', 'to', 'foo'] | |
371 /// | |
372 /// // Windows | |
373 /// builder.split(r'C:\path\to\foo'); // -> [r'C:\', 'path', 'to', 'foo'] | |
374 List<String> split(String path) { | |
375 var parsed = _parse(path); | |
376 // Filter out empty parts that exist due to multiple separators in a row. | |
377 parsed.parts = parsed.parts.where((part) => !part.isEmpty).toList(); | |
378 if (parsed.root != null) parsed.parts.insertRange(0, 1, parsed.root); | |
379 return parsed.parts; | |
380 } | |
381 | |
382 /// Normalizes [path], simplifying it by handling `..`, and `.`, and | |
383 /// removing redundant path separators whenever possible. | |
384 /// | |
385 /// builder.normalize('path/./to/..//file.text'); // -> 'path/file.txt' | |
386 String normalize(String path) { | |
387 if (path == '') return path; | |
388 | |
389 var parsed = _parse(path); | |
390 parsed.normalize(); | |
391 return parsed.toString(); | |
392 } | |
393 | |
394 /// Creates a new path by appending the given path parts to the [root]. | |
395 /// Equivalent to [join()] with [root] as the first argument. Example: | |
396 /// | |
397 /// var builder = new Builder(root: 'root'); | |
398 /// builder.resolve('path', 'to', 'foo'); // -> 'root/path/to/foo' | |
399 String resolve(String part1, [String part2, String part3, String part4, | |
400 String part5, String part6, String part7]) { | |
401 return join(root, part1, part2, part3, part4, part5, part6, part7); | |
402 } | |
403 | |
404 /// Attempts to convert [path] to an equivalent relative path relative to | |
405 /// [root]. | |
406 /// | |
407 /// var builder = new Builder(root: '/root/path'); | |
408 /// builder.relative('/root/path/a/b.dart'); // -> 'a/b.dart' | |
409 /// builder.relative('/root/other.dart'); // -> '../other.dart' | |
410 /// | |
411 /// If the [from] argument is passed, [path] is made relative to that instead. | |
412 /// | |
413 /// builder.relative('/root/path/a/b.dart', | |
414 /// from: '/root/path'); // -> 'a/b.dart' | |
415 /// builder.relative('/root/other.dart', | |
416 /// from: '/root/path'); // -> '../other.dart' | |
417 /// | |
418 /// Since there is no relative path from one drive letter to another on | |
419 /// Windows, this will return an absolute path in that case. | |
420 /// | |
421 /// builder.relative(r'D:\other', from: r'C:\other'); // -> 'D:\other' | |
422 /// | |
423 /// This will also return an absolute path if an absolute [path] is passed to | |
424 /// a builder with a relative [root]. | |
425 /// | |
426 /// var builder = new Builder(r'some/relative/path'); | |
427 /// builder.relative(r'/absolute/path'); // -> '/absolute/path' | |
428 String relative(String path, {String from}) { | |
429 if (path == '') return '.'; | |
430 | |
431 from = from == null ? root : this.join(root, from); | |
432 | |
433 // We can't determine the path from a relative path to an absolute path. | |
434 if (this.isRelative(from) && this.isAbsolute(path)) { | |
435 return this.normalize(path); | |
436 } | |
437 | |
438 // If the given path is relative, resolve it relative to the root of the | |
439 // builder. | |
440 if (this.isRelative(path)) path = this.resolve(path); | |
441 | |
442 // If the path is still relative and `from` is absolute, we're unable to | |
443 // find a path from `from` to `path`. | |
444 if (this.isRelative(path) && this.isAbsolute(from)) { | |
445 throw new ArgumentError('Unable to find a path to "$path" from "$from".'); | |
446 } | |
447 | |
448 var fromParsed = _parse(from)..normalize(); | |
449 var pathParsed = _parse(path)..normalize(); | |
450 | |
451 // If the root prefixes don't match (for example, different drive letters | |
452 // on Windows), then there is no relative path, so just return the absolute | |
453 // one. In Windows, drive letters are case-insenstive and we allow | |
454 // calculation of relative paths, even if a path has not been normalized. | |
455 if (fromParsed.root != pathParsed.root && | |
456 ((fromParsed.root == null || pathParsed.root == null) || | |
457 fromParsed.root.toLowerCase().replaceAll('/', '\\') != | |
458 pathParsed.root.toLowerCase().replaceAll('/', '\\'))) { | |
459 return pathParsed.toString(); | |
460 } | |
461 | |
462 // Strip off their common prefix. | |
463 while (fromParsed.parts.length > 0 && pathParsed.parts.length > 0 && | |
464 fromParsed.parts[0] == pathParsed.parts[0]) { | |
465 fromParsed.parts.removeAt(0); | |
466 fromParsed.separators.removeAt(0); | |
467 pathParsed.parts.removeAt(0); | |
468 pathParsed.separators.removeAt(0); | |
469 } | |
470 | |
471 // If there are any directories left in the root path, we need to walk up | |
472 // out of them. | |
473 pathParsed.parts.insertRange(0, fromParsed.parts.length, '..'); | |
474 pathParsed.separators.insertRange(0, fromParsed.parts.length, | |
475 style.separator); | |
476 | |
477 // Corner case: the paths completely collapsed. | |
478 if (pathParsed.parts.length == 0) return '.'; | |
479 | |
480 // Make it relative. | |
481 pathParsed.root = ''; | |
482 pathParsed.removeTrailingSeparators(); | |
483 | |
484 return pathParsed.toString(); | |
485 } | |
486 | |
487 /// Removes a trailing extension from the last part of [path]. | |
488 /// | |
489 /// builder.withoutExtension('path/to/foo.dart'); // -> 'path/to/foo' | |
490 String withoutExtension(String path) { | |
491 var parsed = _parse(path); | |
492 | |
493 for (var i = parsed.parts.length - 1; i >= 0; i--) { | |
494 if (!parsed.parts[i].isEmpty) { | |
495 parsed.parts[i] = parsed.basenameWithoutExtension; | |
496 break; | |
497 } | |
498 } | |
499 | |
500 return parsed.toString(); | |
501 } | |
502 | |
503 _ParsedPath _parse(String path) { | |
504 var before = path; | |
505 | |
506 // Remove the root prefix, if any. | |
507 var root = style.getRoot(path); | |
508 if (root != null) path = path.substring(root.length); | |
509 | |
510 // Split the parts on path separators. | |
511 var parts = []; | |
512 var separators = []; | |
513 var start = 0; | |
514 for (var match in style.separatorPattern.allMatches(path)) { | |
515 parts.add(path.substring(start, match.start)); | |
516 separators.add(match[0]); | |
517 start = match.end; | |
518 } | |
519 | |
520 // Add the final part, if any. | |
521 if (start < path.length) { | |
522 parts.add(path.substring(start)); | |
523 separators.add(''); | |
524 } | |
525 | |
526 return new _ParsedPath(style, root, parts, separators); | |
527 } | |
528 } | |
529 | |
530 /// An enum type describing a "flavor" of path. | |
531 class Style { | |
532 /// POSIX-style paths use "/" (forward slash) as separators. Absolute paths | |
533 /// start with "/". Used by UNIX, Linux, Mac OS X, and others. | |
534 static final posix = new Style._('posix', '/', '/', '/'); | |
535 | |
536 /// Windows paths use "\" (backslash) as separators. Absolute paths start with | |
537 /// a drive letter followed by a colon (example, "C:") or two backslashes | |
538 /// ("\\") for UNC paths. | |
539 // TODO(rnystrom): The UNC root prefix should include the drive name too, not | |
540 // just the "\\". | |
541 static final windows = new Style._('windows', '\\', r'[/\\]', | |
542 r'\\\\|[a-zA-Z]:[/\\]'); | |
543 | |
544 Style._(this.name, this.separator, String separatorPattern, | |
545 String rootPattern) | |
546 : separatorPattern = new RegExp(separatorPattern), | |
547 _rootPattern = new RegExp('^$rootPattern'); | |
548 | |
549 /// The name of this path style. Will be "posix" or "windows". | |
550 final String name; | |
551 | |
552 /// The path separator for this style. On POSIX, this is `/`. On Windows, | |
553 /// it's `\`. | |
554 final String separator; | |
555 | |
556 /// The [Pattern] that can be used to match a separator for a path in this | |
557 /// style. Windows allows both "/" and "\" as path separators even though | |
558 /// "\" is the canonical one. | |
559 final Pattern separatorPattern; | |
560 | |
561 // TODO(nweiz): make this a Pattern when issue 7080 is fixed. | |
562 /// The [RegExp] that can be used to match the root prefix of an absolute | |
563 /// path in this style. | |
564 final RegExp _rootPattern; | |
565 | |
566 /// Gets the root prefix of [path] if path is absolute. If [path] is relative, | |
567 /// returns `null`. | |
568 String getRoot(String path) { | |
569 var match = _rootPattern.firstMatch(path); | |
570 if (match == null) return null; | |
571 return match[0]; | |
572 } | |
573 | |
574 String toString() => name; | |
575 } | |
576 | |
577 // TODO(rnystrom): Make this public? | |
578 class _ParsedPath { | |
579 /// The [Style] that was used to parse this path. | |
580 Style style; | |
581 | |
582 /// The absolute root portion of the path, or `null` if the path is relative. | |
583 /// On POSIX systems, this will be `null` or "/". On Windows, it can be | |
584 /// `null`, "//" for a UNC path, or something like "C:\" for paths with drive | |
585 /// letters. | |
586 String root; | |
587 | |
588 /// The path-separated parts of the path. All but the last will be | |
589 /// directories. | |
590 List<String> parts; | |
591 | |
592 /// The path separators following each part. The last one will be an empty | |
593 /// string unless the path ends with a trailing separator. | |
594 List<String> separators; | |
595 | |
596 /// The file extension of the last part, or "" if it doesn't have one. | |
597 String get extension => _splitExtension()[1]; | |
598 | |
599 /// `true` if this is an absolute path. | |
600 bool get isAbsolute => root != null; | |
601 | |
602 _ParsedPath(this.style, this.root, this.parts, this.separators); | |
603 | |
604 String get basename { | |
605 var copy = this.clone(); | |
606 copy.removeTrailingSeparators(); | |
607 if (copy.parts.isEmpty) return root == null ? '' : root; | |
608 return copy.parts.last; | |
609 } | |
610 | |
611 String get basenameWithoutExtension { | |
612 var copy = this.clone(); | |
613 copy.removeTrailingSeparators(); | |
614 if (copy.parts.isEmpty) return root == null ? '' : root; | |
615 return copy._splitExtension()[0]; | |
616 } | |
617 | |
618 void removeTrailingSeparators() { | |
619 while (!parts.isEmpty && parts.last == '') { | |
620 parts.removeLast(); | |
621 separators.removeLast(); | |
622 } | |
623 if (separators.length > 0) separators[separators.length - 1] = ''; | |
624 } | |
625 | |
626 void normalize() { | |
627 // Handle '.', '..', and empty parts. | |
628 var leadingDoubles = 0; | |
629 var newParts = []; | |
630 for (var part in parts) { | |
631 if (part == '.' || part == '') { | |
632 // Do nothing. Ignore it. | |
633 } else if (part == '..') { | |
634 // Pop the last part off. | |
635 if (newParts.length > 0) { | |
636 newParts.removeLast(); | |
637 } else { | |
638 // Backed out past the beginning, so preserve the "..". | |
639 leadingDoubles++; | |
640 } | |
641 } else { | |
642 newParts.add(part); | |
643 } | |
644 } | |
645 | |
646 // A relative path can back out from the start directory. | |
647 if (!isAbsolute) { | |
648 newParts.insertRange(0, leadingDoubles, '..'); | |
649 } | |
650 | |
651 // If we collapsed down to nothing, do ".". | |
652 if (newParts.length == 0 && !isAbsolute) { | |
653 newParts.add('.'); | |
654 } | |
655 | |
656 // Canonicalize separators. | |
657 var newSeparators = []; | |
658 newSeparators.insertRange(0, newParts.length, style.separator); | |
659 | |
660 parts = newParts; | |
661 separators = newSeparators; | |
662 | |
663 // Normalize the Windows root if needed. | |
664 if (root != null && style == Style.windows) { | |
665 root = root.replaceAll('/', '\\'); | |
666 } | |
667 removeTrailingSeparators(); | |
668 } | |
669 | |
670 String toString() { | |
671 var builder = new StringBuffer(); | |
672 if (root != null) builder.add(root); | |
673 for (var i = 0; i < parts.length; i++) { | |
674 builder.add(parts[i]); | |
675 builder.add(separators[i]); | |
676 } | |
677 | |
678 return builder.toString(); | |
679 } | |
680 | |
681 /// Splits the last part of the path into a two-element list. The first is | |
682 /// the name of the file without any extension. The second is the extension | |
683 /// or "" if it has none. | |
684 List<String> _splitExtension() { | |
685 if (parts.isEmpty) return ['', '']; | |
686 | |
687 var file = parts.last; | |
688 if (file == '..') return ['..', '']; | |
689 | |
690 var lastDot = file.lastIndexOf('.'); | |
691 | |
692 // If there is no dot, or it's the first character, like '.bashrc', it | |
693 // doesn't count. | |
694 if (lastDot <= 0) return [file, '']; | |
695 | |
696 return [file.substring(0, lastDot), file.substring(lastDot)]; | |
697 } | |
698 | |
699 _ParsedPath clone() => new _ParsedPath( | |
700 style, root, new List.from(parts), new List.from(separators)); | |
701 } | |
OLD | NEW |