Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(411)

Unified Diff: sdk/lib/io/path_impl.dart

Issue 11968012: Fix Path.relativeTo and add support for relative paths as input. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: #Just say '/' instead of 'path separator' in the doc comments. Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/io/path.dart ('k') | tests/standalone/io/path_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/io/path_impl.dart
diff --git a/sdk/lib/io/path_impl.dart b/sdk/lib/io/path_impl.dart
index a7623b05d321c12774825924bc1fe7dd2e1a9418..b3c98572d8cc58ddccddd2aa5ce935319614d27d 100644
--- a/sdk/lib/io/path_impl.dart
+++ b/sdk/lib/io/path_impl.dart
@@ -60,35 +60,46 @@ class _Path implements Path {
if (_path[basePath.length] == '/') {
return new Path(_path.substring(basePath.length + 1));
}
- } else if (base.isAbsolute && isAbsolute &&
+ } else if (base.isAbsolute == isAbsolute &&
base.isWindowsShare == isWindowsShare) {
List<String> baseSegments = base.canonicalize().segments();
List<String> pathSegments = canonicalize().segments();
+ if (baseSegments.length == 1 && baseSegments[0] == '.') {
+ baseSegments = [];
+ }
+ if (pathSegments.length == 1 && pathSegments[0] == '.') {
+ pathSegments = [];
+ }
int common = 0;
int length = min(pathSegments.length, baseSegments.length);
while (common < length && pathSegments[common] == baseSegments[common]) {
Bob Nystrom 2013/01/16 18:42:38 I believe this will do the wrong thing if you have
common++;
}
- final sb = new StringBuffer();
+ final segments = new List<String>();
- for (int i = common + 1; i < baseSegments.length; i++) {
- sb.add('../');
+ if (common < baseSegments.length && baseSegments[common] == '..') {
+ throw new ArgumentError(
+ "Invalid case of Path.relativeTo(base):\n"
+ " Base path has more '..'s than path does."
+ " Arguments: $_path.relativeTo($base)");
Bob Nystrom 2013/01/16 18:42:38 Throwing an ArgumentError here is a trip-wire for
}
- if (base.hasTrailingSeparator) {
- sb.add('../');
+ for (int i = common; i < baseSegments.length; i++) {
+ segments.add('..');
+ }
+ for (int i = common; i < pathSegments.length; i++) {
+ segments.add('${pathSegments[i]}');
}
- for (int i = common; i < pathSegments.length - 1; i++) {
- sb.add('${pathSegments[i]}/');
+ if (segments.isEmpty) {
+ segments.add('.');
}
- sb.add('${pathSegments.last}');
if (hasTrailingSeparator) {
- sb.add('/');
+ segments.add('');
}
- return new Path(sb.toString());
+ return new Path(Strings.join(segments, '/'));
}
- throw new UnimplementedError(
- "Unimplemented case of Path.relativeTo(base):\n"
- " Only absolute paths are handled at present.\n"
+ throw new ArgumentError(
+ "Invalid case of Path.relativeTo(base):\n"
+ " Path and base must both be relative, or both absolute.\n"
" Arguments: $_path.relativeTo($base)");
Bob Nystrom 2013/01/16 18:42:38 Ditto here. This will give users a bad day. pkg/p
}
« no previous file with comments | « sdk/lib/io/path.dart ('k') | tests/standalone/io/path_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698