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

Unified Diff: pkg/path/lib/src/style/url.dart

Issue 439223002: Add InternalStyle:rootLength to implement isAbsolute and rootPrefix. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Review update. Created 6 years, 4 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
Index: pkg/path/lib/src/style/url.dart
diff --git a/pkg/path/lib/src/style/url.dart b/pkg/path/lib/src/style/url.dart
index f383923d271100d44b6e65fd299a45df3a85c539..98eba8ff993e2097797c8ebf4ba107010be3cd59 100644
--- a/pkg/path/lib/src/style/url.dart
+++ b/pkg/path/lib/src/style/url.dart
@@ -36,53 +36,30 @@ class UrlStyle extends InternalStyle {
// A URI that's just "scheme://" needs an extra separator, despite ending
// with "/".
- var root = _getRoot(path);
- return root != null && root.endsWith('://');
+ return rootLength(path) > 0 && path.endsWith("://");
nweiz 2014/08/05 20:22:27 I believe this still breaks on "http://foo.com/bar
Anders Johnsen 2014/08/06 07:05:15 Better. Fixed. Also, swapped them as endsWith is f
}
- String getRoot(String path) {
- var root = _getRoot(path);
- return root == null ? getRelativeRoot(path) : root;
+ int rootLength(String path) {
+ if (path.isEmpty) return 0;
+ if (isSeparator(path.codeUnitAt(0))) return 1;
+ var index = path.indexOf("/");
+ if (index > 0 && path.startsWith('://', index - 1)) {
+ // The root part is up until the next '/', or the full path. Skip
+ // '://' and search for '/' after that.
+ index = path.indexOf('/', index + 2);
+ if (index > 0) return index;
+ return path.length;
+ }
+ return 0;
}
- String getRelativeRoot(String path) {
- if (path.isEmpty) return null;
- return isSeparator(path.codeUnitAt(0)) ? "/" : null;
- }
+ bool isRootRelative(String path) =>
+ path.isNotEmpty && isSeparator(path.codeUnitAt(0));
+
+ String getRelativeRoot(String path) => isRootRelative(path) ? '/' : null;
String pathFromUri(Uri uri) => uri.toString();
Uri relativePathToUri(String path) => Uri.parse(path);
Uri absolutePathToUri(String path) => Uri.parse(path);
-
- // A helper method for [getRoot] that doesn't handle relative roots.
- String _getRoot(String path) {
- if (path.isEmpty) return null;
-
- // We aren't using a RegExp for this because they're slow (issue 19090). If
- // we could, we'd match against r"[a-zA-Z][-+.a-zA-Z\d]*://[^/]*".
-
- if (!isAlphabetic(path.codeUnitAt(0))) return null;
- var start = 1;
- for (; start < path.length; start++) {
- var char = path.codeUnitAt(start);
- if (isAlphabetic(char)) continue;
- if (isNumeric(char)) continue;
- if (char == chars.MINUS || char == chars.PLUS || char == chars.PERIOD) {
- continue;
- }
-
- break;
- }
-
- if (start + 3 > path.length) return null;
- if (path.substring(start, start + 3) != '://') return null;
- start += 3;
-
- // A URL root can end with a non-"/" prefix.
- while (start < path.length && !isSeparator(path.codeUnitAt(start))) {
- start++;
- }
- return path.substring(0, start);
- }
}

Powered by Google App Engine
This is Rietveld 408576698