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 92 matching lines...) Loading... | |
103 /// path.relative('/root/path/a/b.dart'); // -> 'a/b.dart' | 103 /// path.relative('/root/path/a/b.dart'); // -> 'a/b.dart' |
104 /// path.relative('/root/other.dart'); // -> '../other.dart' | 104 /// path.relative('/root/other.dart'); // -> '../other.dart' |
105 /// | 105 /// |
106 /// Since there is no relative path from one drive letter to another on Windows, | 106 /// Since there is no relative path from one drive letter to another on Windows, |
107 /// this will return an absolute path in that case. | 107 /// this will return an absolute path in that case. |
108 /// | 108 /// |
109 /// // Given current directory is C:\home: | 109 /// // Given current directory is C:\home: |
110 /// path.relative(r'D:\other'); // -> 'D:\other' | 110 /// path.relative(r'D:\other'); // -> 'D:\other' |
111 String relative(String path) => _builder.relative(path); | 111 String relative(String path) => _builder.relative(path); |
112 | 112 |
113 /// Splits [path] along separators and returns a [List] of the parts. The | |
114 /// path will *not* be normalized before splitting. | |
115 /// | |
116 /// path.split('a/b/c.txt'); // -> ['a', 'b', 'c.txt'] | |
117 /// path.split('a/../c'); // -> ['a', '..', 'c'] | |
118 /// | |
119 /// If [path] is absolute, the root prefix will be included in the first | |
120 /// part. | |
121 /// | |
122 /// path.split('/a/b/c.txt'); // -> ['/a', 'b', 'c.txt'] | |
123 /// path.split(r'C:\a\b\c.txt'); // -> [r'C:\a', 'b', 'c.txt'] | |
124 List<String> split(String path) => _builder.split(path); | |
nweiz
2012/12/12 21:21:21
I assume this'll be replaced by my implementation.
Bob Nystrom
2012/12/12 21:45:36
Yup.
| |
125 | |
113 /// Removes a trailing extension from the last part of [path]. | 126 /// Removes a trailing extension from the last part of [path]. |
114 /// | 127 /// |
115 /// withoutExtension('path/to/foo.dart'); // -> 'path/to/foo' | 128 /// withoutExtension('path/to/foo.dart'); // -> 'path/to/foo' |
116 String withoutExtension(String path) => _builder.withoutExtension(path); | 129 String withoutExtension(String path) => _builder.withoutExtension(path); |
117 | 130 |
118 /// An instantiable class for manipulating paths. Unlike the top-level | 131 /// An instantiable class for manipulating paths. Unlike the top-level |
119 /// functions, this lets you explicitly select what platform the paths will use. | 132 /// functions, this lets you explicitly select what platform the paths will use. |
120 class Builder { | 133 class Builder { |
121 /// Creates a new path builder for the given style and root directory. | 134 /// Creates a new path builder for the given style and root directory. |
122 /// | 135 /// |
(...skipping 199 matching lines...) Loading... | |
322 // Corner case: the paths completely collapsed. | 335 // Corner case: the paths completely collapsed. |
323 if (pathParsed.parts.length == 0) return '.'; | 336 if (pathParsed.parts.length == 0) return '.'; |
324 | 337 |
325 // Make it relative. | 338 // Make it relative. |
326 pathParsed.root = ''; | 339 pathParsed.root = ''; |
327 pathParsed.removeTrailingSeparator(); | 340 pathParsed.removeTrailingSeparator(); |
328 | 341 |
329 return pathParsed.toString(); | 342 return pathParsed.toString(); |
330 } | 343 } |
331 | 344 |
345 /// Splits [path] along separators and returns a [List] of the parts. The | |
346 /// path will *not* be normalized before splitting. | |
347 /// | |
348 /// builder.split('a/b/c.txt'); // -> ['a', 'b', 'c.txt'] | |
349 /// builder.split('a/../c'); // -> ['a', '..', 'c'] | |
350 /// | |
351 /// If [path] is absolute, the root prefix will be included in the first | |
352 /// part. | |
353 /// | |
354 /// builder.split('/a/b/c.txt'); // -> ['/a', 'b', 'c.txt'] | |
355 /// builder.split(r'C:\a\b\c.txt'); // -> [r'C:\a', 'b', 'c.txt'] | |
356 List<String> split(String path) { | |
357 var parsed = _parse(path); | |
358 | |
359 // Include the root prefix in the first part. | |
360 if (parsed.root != null) { | |
361 if (parsed.parts.isEmpty) return [parsed.root]; | |
362 parsed.parts[0] = '${parsed.root}${parsed.parts[0]}'; | |
363 } | |
364 | |
365 return parsed.parts.filter(((part) => part != '')); | |
366 } | |
367 | |
332 /// Removes a trailing extension from the last part of [path]. | 368 /// Removes a trailing extension from the last part of [path]. |
333 /// | 369 /// |
334 /// builder.withoutExtension('path/to/foo.dart'); // -> 'path/to/foo' | 370 /// builder.withoutExtension('path/to/foo.dart'); // -> 'path/to/foo' |
335 String withoutExtension(String path) { | 371 String withoutExtension(String path) { |
336 var parsed = _parse(path); | 372 var parsed = _parse(path); |
337 if (parsed.hasTrailingSeparator) return parsed.toString(); | 373 if (parsed.hasTrailingSeparator) return parsed.toString(); |
338 | 374 |
339 if (!parsed.parts.isEmpty) { | 375 if (!parsed.parts.isEmpty) { |
340 parsed.parts[parsed.parts.length - 1] = parsed.basenameWithoutExtension; | 376 parsed.parts[parsed.parts.length - 1] = parsed.basenameWithoutExtension; |
341 } | 377 } |
(...skipping 184 matching lines...) Loading... | |
526 | 562 |
527 var lastDot = file.lastIndexOf('.'); | 563 var lastDot = file.lastIndexOf('.'); |
528 | 564 |
529 // If there is no dot, or it's the first character, like '.bashrc', it | 565 // If there is no dot, or it's the first character, like '.bashrc', it |
530 // doesn't count. | 566 // doesn't count. |
531 if (lastDot <= 0) return [file, '']; | 567 if (lastDot <= 0) return [file, '']; |
532 | 568 |
533 return [file.substring(0, lastDot), file.substring(lastDot)]; | 569 return [file.substring(0, lastDot), file.substring(lastDot)]; |
534 } | 570 } |
535 } | 571 } |
OLD | NEW |