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