| 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 /// A comprehensive, cross-platform path manipulation library. | 5 /// A comprehensive, cross-platform path manipulation library. |
| 6 library path; | 6 library path; |
| 7 | 7 |
| 8 import 'dart:io' as io; | 8 import 'dart:io' as io; |
| 9 | 9 |
| 10 /// An internal builder for the current OS so we can provide a straight | 10 /// An internal builder for the current OS so we can provide a straight |
| 11 /// functional interface and not require users to create one. | 11 /// functional interface and not require users to create one. |
| 12 final _builder = new Builder(); | 12 final _builder = new Builder(); |
| 13 | 13 |
| 14 /** |
| 15 * Inserts [length] elements in front of the [list] and fills them with the |
| 16 * [fillValue]. |
| 17 */ |
| 18 void _growListFront(List list, int length, fillValue) { |
| 19 list.length += length; |
| 20 list.setRange(length, list.length, list); |
| 21 for (var i = 0; i < length; i++) { |
| 22 list[i] = fillValue; |
| 23 } |
| 24 } |
| 25 |
| 14 /// Gets the path to the current working directory. | 26 /// Gets the path to the current working directory. |
| 15 String get current => new io.Directory.current().path; | 27 String get current => new io.Directory.current().path; |
| 16 | 28 |
| 17 /// Gets the path separator for the current platform. On Mac and Linux, this | 29 /// Gets the path separator for the current platform. On Mac and Linux, this |
| 18 /// is `/`. On Windows, it's `\`. | 30 /// is `/`. On Windows, it's `\`. |
| 19 String get separator => _builder.separator; | 31 String get separator => _builder.separator; |
| 20 | 32 |
| 21 /// Converts [path] to an absolute path by resolving it relative to the current | 33 /// 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. | 34 /// working directory. If [path] is already an absolute path, just returns it. |
| 23 /// | 35 /// |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 /// // Unix | 413 /// // Unix |
| 402 /// builder.split('/path/to/foo'); // -> ['/', 'path', 'to', 'foo'] | 414 /// builder.split('/path/to/foo'); // -> ['/', 'path', 'to', 'foo'] |
| 403 /// | 415 /// |
| 404 /// // Windows | 416 /// // Windows |
| 405 /// builder.split(r'C:\path\to\foo'); // -> [r'C:\', 'path', 'to', 'foo'] | 417 /// builder.split(r'C:\path\to\foo'); // -> [r'C:\', 'path', 'to', 'foo'] |
| 406 List<String> split(String path) { | 418 List<String> split(String path) { |
| 407 var parsed = _parse(path); | 419 var parsed = _parse(path); |
| 408 // Filter out empty parts that exist due to multiple separators in a row. | 420 // Filter out empty parts that exist due to multiple separators in a row. |
| 409 parsed.parts = parsed.parts.where((part) => !part.isEmpty) | 421 parsed.parts = parsed.parts.where((part) => !part.isEmpty) |
| 410 .toList(); | 422 .toList(); |
| 411 if (parsed.root != null) parsed.parts.insertRange(0, 1, parsed.root); | 423 if (parsed.root != null) parsed.parts.insert(0, parsed.root); |
| 412 return parsed.parts; | 424 return parsed.parts; |
| 413 } | 425 } |
| 414 | 426 |
| 415 /// Normalizes [path], simplifying it by handling `..`, and `.`, and | 427 /// Normalizes [path], simplifying it by handling `..`, and `.`, and |
| 416 /// removing redundant path separators whenever possible. | 428 /// removing redundant path separators whenever possible. |
| 417 /// | 429 /// |
| 418 /// builder.normalize('path/./to/..//file.text'); // -> 'path/file.txt' | 430 /// builder.normalize('path/./to/..//file.text'); // -> 'path/file.txt' |
| 419 String normalize(String path) { | 431 String normalize(String path) { |
| 420 if (path == '') return path; | 432 if (path == '') return path; |
| 421 | 433 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 while (fromParsed.parts.length > 0 && pathParsed.parts.length > 0 && | 508 while (fromParsed.parts.length > 0 && pathParsed.parts.length > 0 && |
| 497 fromParsed.parts[0] == pathParsed.parts[0]) { | 509 fromParsed.parts[0] == pathParsed.parts[0]) { |
| 498 fromParsed.parts.removeAt(0); | 510 fromParsed.parts.removeAt(0); |
| 499 fromParsed.separators.removeAt(0); | 511 fromParsed.separators.removeAt(0); |
| 500 pathParsed.parts.removeAt(0); | 512 pathParsed.parts.removeAt(0); |
| 501 pathParsed.separators.removeAt(0); | 513 pathParsed.separators.removeAt(0); |
| 502 } | 514 } |
| 503 | 515 |
| 504 // If there are any directories left in the root path, we need to walk up | 516 // If there are any directories left in the root path, we need to walk up |
| 505 // out of them. | 517 // out of them. |
| 506 pathParsed.parts.insertRange(0, fromParsed.parts.length, '..'); | 518 _growListFront(pathParsed.parts, fromParsed.parts.length, '..'); |
| 507 pathParsed.separators.insertRange(0, fromParsed.parts.length, | 519 _growListFront( |
| 508 style.separator); | 520 pathParsed.separators, fromParsed.parts.length, style.separator); |
| 509 | 521 |
| 510 // Corner case: the paths completely collapsed. | 522 // Corner case: the paths completely collapsed. |
| 511 if (pathParsed.parts.length == 0) return '.'; | 523 if (pathParsed.parts.length == 0) return '.'; |
| 512 | 524 |
| 513 // Make it relative. | 525 // Make it relative. |
| 514 pathParsed.root = ''; | 526 pathParsed.root = ''; |
| 515 pathParsed.removeTrailingSeparators(); | 527 pathParsed.removeTrailingSeparators(); |
| 516 | 528 |
| 517 return pathParsed.toString(); | 529 return pathParsed.toString(); |
| 518 } | 530 } |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 // Backed out past the beginning, so preserve the "..". | 683 // Backed out past the beginning, so preserve the "..". |
| 672 leadingDoubles++; | 684 leadingDoubles++; |
| 673 } | 685 } |
| 674 } else { | 686 } else { |
| 675 newParts.add(part); | 687 newParts.add(part); |
| 676 } | 688 } |
| 677 } | 689 } |
| 678 | 690 |
| 679 // A relative path can back out from the start directory. | 691 // A relative path can back out from the start directory. |
| 680 if (!isAbsolute) { | 692 if (!isAbsolute) { |
| 681 newParts.insertRange(0, leadingDoubles, '..'); | 693 _growListFront(newParts, leadingDoubles, '..'); |
| 682 } | 694 } |
| 683 | 695 |
| 684 // If we collapsed down to nothing, do ".". | 696 // If we collapsed down to nothing, do ".". |
| 685 if (newParts.length == 0 && !isAbsolute) { | 697 if (newParts.length == 0 && !isAbsolute) { |
| 686 newParts.add('.'); | 698 newParts.add('.'); |
| 687 } | 699 } |
| 688 | 700 |
| 689 // Canonicalize separators. | 701 // Canonicalize separators. |
| 690 var newSeparators = []; | 702 var newSeparators = []; |
| 691 newSeparators.insertRange(0, newParts.length, style.separator); | 703 _growListFront(newSeparators, newParts.length, style.separator); |
| 692 | 704 |
| 693 parts = newParts; | 705 parts = newParts; |
| 694 separators = newSeparators; | 706 separators = newSeparators; |
| 695 | 707 |
| 696 // Normalize the Windows root if needed. | 708 // Normalize the Windows root if needed. |
| 697 if (root != null && style == Style.windows) { | 709 if (root != null && style == Style.windows) { |
| 698 root = root.replaceAll('/', '\\'); | 710 root = root.replaceAll('/', '\\'); |
| 699 } | 711 } |
| 700 removeTrailingSeparators(); | 712 removeTrailingSeparators(); |
| 701 } | 713 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 725 // If there is no dot, or it's the first character, like '.bashrc', it | 737 // If there is no dot, or it's the first character, like '.bashrc', it |
| 726 // doesn't count. | 738 // doesn't count. |
| 727 if (lastDot <= 0) return [file, '']; | 739 if (lastDot <= 0) return [file, '']; |
| 728 | 740 |
| 729 return [file.substring(0, lastDot), file.substring(lastDot)]; | 741 return [file.substring(0, lastDot), file.substring(lastDot)]; |
| 730 } | 742 } |
| 731 | 743 |
| 732 _ParsedPath clone() => new _ParsedPath( | 744 _ParsedPath clone() => new _ParsedPath( |
| 733 style, root, new List.from(parts), new List.from(separators)); | 745 style, root, new List.from(parts), new List.from(separators)); |
| 734 } | 746 } |
| OLD | NEW |