| 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 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 // Ignore nulls hanging off the end. | 187 // Ignore nulls hanging off the end. |
| 188 if (args[i] == null || args[i - 1] != null) continue; | 188 if (args[i] == null || args[i - 1] != null) continue; |
| 189 | 189 |
| 190 var numArgs; | 190 var numArgs; |
| 191 for (numArgs = args.length; numArgs >= 1; numArgs--) { | 191 for (numArgs = args.length; numArgs >= 1; numArgs--) { |
| 192 if (args[numArgs - 1] != null) break; | 192 if (args[numArgs - 1] != null) break; |
| 193 } | 193 } |
| 194 | 194 |
| 195 // Show the arguments. | 195 // Show the arguments. |
| 196 var message = new StringBuffer(); | 196 var message = new StringBuffer(); |
| 197 message.add("$method("); | 197 message.write("$method("); |
| 198 message.add(args.take(numArgs) | 198 message.write(args.take(numArgs) |
| 199 .map((arg) => arg == null ? "null" : '"$arg"') | 199 .map((arg) => arg == null ? "null" : '"$arg"') |
| 200 .join(", ")); | 200 .join(", ")); |
| 201 message.add("): part ${i - 1} was null, but part $i was not."); | 201 message.write("): part ${i - 1} was null, but part $i was not."); |
| 202 throw new ArgumentError(message.toString()); | 202 throw new ArgumentError(message.toString()); |
| 203 } | 203 } |
| 204 } | 204 } |
| 205 | 205 |
| 206 /// An instantiable class for manipulating paths. Unlike the top-level | 206 /// An instantiable class for manipulating paths. Unlike the top-level |
| 207 /// functions, this lets you explicitly select what platform the paths will use. | 207 /// functions, this lets you explicitly select what platform the paths will use. |
| 208 class Builder { | 208 class Builder { |
| 209 /// Creates a new path builder for the given style and root directory. | 209 /// Creates a new path builder for the given style and root directory. |
| 210 /// | 210 /// |
| 211 /// If [style] is omitted, it uses the host operating system's path style. If | 211 /// If [style] is omitted, it uses the host operating system's path style. If |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 /// builder.joinAll(['path', '/to', 'foo']); // -> '/to/foo' | 357 /// builder.joinAll(['path', '/to', 'foo']); // -> '/to/foo' |
| 358 /// | 358 /// |
| 359 /// For a fixed number of parts, [join] is usually terser. | 359 /// For a fixed number of parts, [join] is usually terser. |
| 360 String joinAll(Iterable<String> parts) { | 360 String joinAll(Iterable<String> parts) { |
| 361 var buffer = new StringBuffer(); | 361 var buffer = new StringBuffer(); |
| 362 var needsSeparator = false; | 362 var needsSeparator = false; |
| 363 | 363 |
| 364 for (var part in parts) { | 364 for (var part in parts) { |
| 365 if (this.isAbsolute(part)) { | 365 if (this.isAbsolute(part)) { |
| 366 // An absolute path discards everything before it. | 366 // An absolute path discards everything before it. |
| 367 buffer.clear(); | 367 buffer = new StringBuffer(); |
| 368 buffer.add(part); | 368 buffer.write(part); |
| 369 } else { | 369 } else { |
| 370 if (part.length > 0 && part[0].contains(style.separatorPattern)) { | 370 if (part.length > 0 && part[0].contains(style.separatorPattern)) { |
| 371 // The part starts with a separator, so we don't need to add one. | 371 // The part starts with a separator, so we don't need to add one. |
| 372 } else if (needsSeparator) { | 372 } else if (needsSeparator) { |
| 373 buffer.add(separator); | 373 buffer.write(separator); |
| 374 } | 374 } |
| 375 | 375 |
| 376 buffer.add(part); | 376 buffer.write(part); |
| 377 } | 377 } |
| 378 | 378 |
| 379 // Unless this part ends with a separator, we'll need to add one before | 379 // Unless this part ends with a separator, we'll need to add one before |
| 380 // the next part. | 380 // the next part. |
| 381 needsSeparator = part.length > 0 && | 381 needsSeparator = part.length > 0 && |
| 382 !part[part.length - 1].contains(style.separatorPattern); | 382 !part[part.length - 1].contains(style.separatorPattern); |
| 383 } | 383 } |
| 384 | 384 |
| 385 return buffer.toString(); | 385 return buffer.toString(); |
| 386 } | 386 } |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 | 694 |
| 695 // Normalize the Windows root if needed. | 695 // Normalize the Windows root if needed. |
| 696 if (root != null && style == Style.windows) { | 696 if (root != null && style == Style.windows) { |
| 697 root = root.replaceAll('/', '\\'); | 697 root = root.replaceAll('/', '\\'); |
| 698 } | 698 } |
| 699 removeTrailingSeparators(); | 699 removeTrailingSeparators(); |
| 700 } | 700 } |
| 701 | 701 |
| 702 String toString() { | 702 String toString() { |
| 703 var builder = new StringBuffer(); | 703 var builder = new StringBuffer(); |
| 704 if (root != null) builder.add(root); | 704 if (root != null) builder.write(root); |
| 705 for (var i = 0; i < parts.length; i++) { | 705 for (var i = 0; i < parts.length; i++) { |
| 706 builder.add(parts[i]); | 706 builder.write(parts[i]); |
| 707 builder.add(separators[i]); | 707 builder.write(separators[i]); |
| 708 } | 708 } |
| 709 | 709 |
| 710 return builder.toString(); | 710 return builder.toString(); |
| 711 } | 711 } |
| 712 | 712 |
| 713 /// Splits the last part of the path into a two-element list. The first is | 713 /// Splits the last part of the path into a two-element list. The first is |
| 714 /// the name of the file without any extension. The second is the extension | 714 /// the name of the file without any extension. The second is the extension |
| 715 /// or "" if it has none. | 715 /// or "" if it has none. |
| 716 List<String> _splitExtension() { | 716 List<String> _splitExtension() { |
| 717 if (parts.isEmpty) return ['', '']; | 717 if (parts.isEmpty) return ['', '']; |
| 718 | 718 |
| 719 var file = parts.last; | 719 var file = parts.last; |
| 720 if (file == '..') return ['..', '']; | 720 if (file == '..') return ['..', '']; |
| 721 | 721 |
| 722 var lastDot = file.lastIndexOf('.'); | 722 var lastDot = file.lastIndexOf('.'); |
| 723 | 723 |
| 724 // If there is no dot, or it's the first character, like '.bashrc', it | 724 // If there is no dot, or it's the first character, like '.bashrc', it |
| 725 // doesn't count. | 725 // doesn't count. |
| 726 if (lastDot <= 0) return [file, '']; | 726 if (lastDot <= 0) return [file, '']; |
| 727 | 727 |
| 728 return [file.substring(0, lastDot), file.substring(lastDot)]; | 728 return [file.substring(0, lastDot), file.substring(lastDot)]; |
| 729 } | 729 } |
| 730 | 730 |
| 731 _ParsedPath clone() => new _ParsedPath( | 731 _ParsedPath clone() => new _ParsedPath( |
| 732 style, root, new List.from(parts), new List.from(separators)); | 732 style, root, new List.from(parts), new List.from(separators)); |
| 733 } | 733 } |
| OLD | NEW |