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

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

Issue 309803003: Avoid using RegExps in path to work around issue 19090. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 7 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 1e84917d6fb5cca1cc83bab1fd1384efb9d8ddf1..b985cc10d59a4bf9eef093fc170ca3e0edf4dc7d 100644
--- a/pkg/path/lib/src/style/url.dart
+++ b/pkg/path/lib/src/style/url.dart
@@ -5,6 +5,7 @@
library path.style.url;
import '../internal_style.dart';
+import '../utils.dart';
/// The style for URL paths.
class UrlStyle extends InternalStyle {
@@ -12,14 +13,69 @@ class UrlStyle extends InternalStyle {
final name = 'url';
final separator = '/';
+ final separators = const ['/'];
+
+ // Deprecated properties.
+
final separatorPattern = new RegExp(r'/');
final needsSeparatorPattern = new RegExp(
r"(^[a-zA-Z][-+.a-zA-Z\d]*://|[^/])$");
final rootPattern = new RegExp(r"[a-zA-Z][-+.a-zA-Z\d]*://[^/]*");
final relativeRootPattern = new RegExp(r"^/");
+ bool needsSeparator(String path) {
+ if (path.isEmpty) return false;
+
+ // A URL that doesn't end in "/" always needs a separator.
+ if (path.codeUnitAt(path.length - 1) != 0x2f) return true;
+
+ // A URI that's just "scheme://" needs an extra separator, despite ending
+ // with "/".
+ var root = _getRoot(path);
+ return root != null && root.endsWith('://');
+ }
+
+ String getRoot(String path) {
+ var root = _getRoot(path);
+ return root == null ? getRelativeRoot(path) : root;
+ }
+
+ String getRelativeRoot(String path) {
+ if (path.isEmpty) return null;
+ return path.codeUnitAt(0) == 0x2f ? "/" : 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;
+ // Schemes can contain "-", "+", or ".".
+ if (char == 0x2d || char == 0x2b || char == 0x2e) 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 && path.codeUnitAt(start) != 0x2f) {
+ start++;
+ }
+ return path.substring(0, start);
+ }
}

Powered by Google App Engine
This is Rietveld 408576698