Chromium Code Reviews| Index: pkg/path/lib/path.dart |
| diff --git a/pkg/path/lib/path.dart b/pkg/path/lib/path.dart |
| index b425124cd42e2e86055998bd3fc13004cb66f080..aa7a604a7d701499c24eed94e764a83705db7a08 100644 |
| --- a/pkg/path/lib/path.dart |
| +++ b/pkg/path/lib/path.dart |
| @@ -114,6 +114,16 @@ String basenameWithoutExtension(String path) => |
| /// Trailing separators are ignored. |
| /// |
| /// builder.dirname('path/to/'); // -> 'path' |
| +/// |
| +/// If an absolute path contains no directories, only a root, then the root |
| +/// is returned. |
| +/// |
| +/// path.dirname('/'); // -> '/' (posix) |
| +/// path.dirname('c:\'); // -> 'c:\' (windows) |
| +/// |
| +/// If a relative path has no directories, then '.' is returned. |
|
Bob Nystrom
2013/07/15 17:49:29
Blank line here.
Bill Hesse
2013/07/16 12:42:26
Done.
|
| +/// path.dirname('foo'); // -> '.' |
| +/// path.dirname(''); // -> '.' |
| String dirname(String path) => _builder.dirname(path); |
| /// Gets the file extension of [path]: the portion of [basename] from the last |
| @@ -588,8 +598,6 @@ class Builder { |
| /// |
| /// builder.normalize('path/./to/..//file.text'); // -> 'path/file.txt' |
| String normalize(String path) { |
| - if (path == '') return path; |
| - |
| var parsed = _parse(path); |
| parsed.normalize(); |
| return parsed.toString(); |
| @@ -630,8 +638,6 @@ class Builder { |
| /// var builder = new Builder(r'some/relative/path'); |
| /// builder.relative(r'/absolute/path'); // -> '/absolute/path' |
| String relative(String path, {String from}) { |
| - if (path == '') return '.'; |
| - |
|
Bill Hesse
2013/07/15 16:58:48
This is wrong if [from] is not null. It should wo
Bob Nystrom
2013/07/15 17:49:29
Eek, you're right.
|
| from = from == null ? root : this.join(root, from); |
| // We can't determine the path from a relative path to an absolute path. |
| @@ -678,8 +684,12 @@ class Builder { |
| pathParsed.separators.removeAt(1); |
| } |
| - // If there are any directories left in the root path, we need to walk up |
| - // out of them. |
| + // If there are any directories left in the from path, we need to walk up |
| + // out of them. If a directory left in the from path is '..', it cannot |
| + // be cancelled by adding a '..'. |
| + if (fromParsed.parts.length > 0 && fromParsed.parts[0] == '..') { |
| + throw new ArgumentError('Unable to find a path to "$path" from "$from".'); |
| + } |
|
Bob Nystrom
2013/07/15 17:49:29
Can you give me an example where this will come in
Bill Hesse
2013/07/16 12:42:26
My tests include this case. Any case with ".." in
|
| _growListFront(pathParsed.parts, fromParsed.parts.length, '..'); |
| pathParsed.separators[0] = ''; |
| pathParsed.separators.insertAll(1, |
| @@ -688,6 +698,13 @@ class Builder { |
| // Corner case: the paths completely collapsed. |
| if (pathParsed.parts.length == 0) return '.'; |
| + // Corner case: path was '.' and some '..' directories were added in front. |
| + // Don't add a final '/.' in that case. |
| + if (pathParsed.parts.length > 1 && pathParsed.parts.last == '.') { |
| + pathParsed.parts.removeLast(); |
| + pathParsed.separators..removeLast()..removeLast()..add(''); |
| + } |
|
Bob Nystrom
2013/07/15 17:49:29
Nice catch. Add a test for this?
Bill Hesse
2013/07/16 12:42:26
There is a test for this, in my tests.
|
| + |
| // Make it relative. |
| pathParsed.root = ''; |
| pathParsed.removeTrailingSeparators(); |
| @@ -1046,7 +1063,8 @@ class _ParsedPath { |
| return copy._splitExtension()[0]; |
| } |
| - bool get hasTrailingSeparator => !parts.isEmpty && (parts.last == '' || separators.last != ''); |
| + bool get hasTrailingSeparator => |
| + !parts.isEmpty && (parts.last == '' || separators.last != ''); |
| void removeTrailingSeparators() { |
| while (!parts.isEmpty && parts.last == '') { |
| @@ -1097,7 +1115,7 @@ class _ParsedPath { |
| parts = newParts; |
| separators = newSeparators; |
| - // Normalize the Windows root if needed. |
| + // Normalize the Windows root if needed. |
|
Bill Hesse
2013/07/15 16:58:48
oops.
|
| if (root != null && style == Style.windows) { |
| root = root.replaceAll('/', '\\'); |
| } |