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 |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 // find a path from `from` to `path`. | 431 // find a path from `from` to `path`. |
432 if (this.isRelative(path) && this.isAbsolute(from)) { | 432 if (this.isRelative(path) && this.isAbsolute(from)) { |
433 throw new ArgumentError('Unable to find a path to "$path" from "$from".'); | 433 throw new ArgumentError('Unable to find a path to "$path" from "$from".'); |
434 } | 434 } |
435 | 435 |
436 var fromParsed = _parse(from)..normalize(); | 436 var fromParsed = _parse(from)..normalize(); |
437 var pathParsed = _parse(path)..normalize(); | 437 var pathParsed = _parse(path)..normalize(); |
438 | 438 |
439 // If the root prefixes don't match (for example, different drive letters | 439 // If the root prefixes don't match (for example, different drive letters |
440 // on Windows), then there is no relative path, so just return the absolute | 440 // on Windows), then there is no relative path, so just return the absolute |
441 // one. | 441 // one. In Windows, drive letters are case-insenstive and we allow |
442 // TODO(rnystrom): Drive letters are case-insentive on Windows. Should | 442 // calculation of relative paths, even if a path has not been normalized. |
443 // handle "C:\" and "c:\" being the same root. | 443 if (fromParsed.root != pathParsed.root && |
444 if (fromParsed.root != pathParsed.root) return pathParsed.toString(); | 444 ((fromParsed.root == null || pathParsed.root == null) || |
| 445 fromParsed.root.toLowerCase().replaceAll('/', '\\') != |
| 446 pathParsed.root.toLowerCase().replaceAll('/', '\\'))) { |
| 447 return pathParsed.toString(); |
| 448 } |
445 | 449 |
446 // Strip off their common prefix. | 450 // Strip off their common prefix. |
447 while (fromParsed.parts.length > 0 && pathParsed.parts.length > 0 && | 451 while (fromParsed.parts.length > 0 && pathParsed.parts.length > 0 && |
448 fromParsed.parts[0] == pathParsed.parts[0]) { | 452 fromParsed.parts[0] == pathParsed.parts[0]) { |
449 fromParsed.parts.removeAt(0); | 453 fromParsed.parts.removeAt(0); |
450 fromParsed.separators.removeAt(0); | 454 fromParsed.separators.removeAt(0); |
451 pathParsed.parts.removeAt(0); | 455 pathParsed.parts.removeAt(0); |
452 pathParsed.separators.removeAt(0); | 456 pathParsed.separators.removeAt(0); |
453 } | 457 } |
454 | 458 |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 newParts.add('.'); | 641 newParts.add('.'); |
638 } | 642 } |
639 | 643 |
640 // Canonicalize separators. | 644 // Canonicalize separators. |
641 var newSeparators = []; | 645 var newSeparators = []; |
642 newSeparators.insertRange(0, newParts.length, style.separator); | 646 newSeparators.insertRange(0, newParts.length, style.separator); |
643 | 647 |
644 parts = newParts; | 648 parts = newParts; |
645 separators = newSeparators; | 649 separators = newSeparators; |
646 | 650 |
| 651 // Normalize the Windows root if needed. |
| 652 if (root != null && style == Style.windows) { |
| 653 root = root.replaceAll('/', '\\'); |
| 654 } |
647 removeTrailingSeparators(); | 655 removeTrailingSeparators(); |
648 } | 656 } |
649 | 657 |
650 String toString() { | 658 String toString() { |
651 var builder = new StringBuffer(); | 659 var builder = new StringBuffer(); |
652 if (root != null) builder.add(root); | 660 if (root != null) builder.add(root); |
653 for (var i = 0; i < parts.length; i++) { | 661 for (var i = 0; i < parts.length; i++) { |
654 builder.add(parts[i]); | 662 builder.add(parts[i]); |
655 builder.add(separators[i]); | 663 builder.add(separators[i]); |
656 } | 664 } |
(...skipping 15 matching lines...) Expand all Loading... |
672 // If there is no dot, or it's the first character, like '.bashrc', it | 680 // If there is no dot, or it's the first character, like '.bashrc', it |
673 // doesn't count. | 681 // doesn't count. |
674 if (lastDot <= 0) return [file, '']; | 682 if (lastDot <= 0) return [file, '']; |
675 | 683 |
676 return [file.substring(0, lastDot), file.substring(lastDot)]; | 684 return [file.substring(0, lastDot), file.substring(lastDot)]; |
677 } | 685 } |
678 | 686 |
679 _ParsedPath clone() => new _ParsedPath( | 687 _ParsedPath clone() => new _ParsedPath( |
680 style, root, new List.from(parts), new List.from(separators)); | 688 style, root, new List.from(parts), new List.from(separators)); |
681 } | 689 } |
OLD | NEW |