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

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: code review 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
« no previous file with comments | « pkg/path/lib/src/style/posix.dart ('k') | pkg/path/lib/src/style/windows.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..f383923d271100d44b6e65fd299a45df3a85c539 100644
--- a/pkg/path/lib/src/style/url.dart
+++ b/pkg/path/lib/src/style/url.dart
@@ -4,7 +4,9 @@
library path.style.url;
+import '../characters.dart' as chars;
import '../internal_style.dart';
+import '../utils.dart';
/// The style for URL paths.
class UrlStyle extends InternalStyle {
@@ -12,14 +14,75 @@ 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 containsSeparator(String path) => path.contains('/');
+
+ bool isSeparator(int codeUnit) => codeUnit == chars.SLASH;
+
+ bool needsSeparator(String path) {
+ if (path.isEmpty) return false;
+
+ // A URL that doesn't end in "/" always needs a separator.
+ if (!isSeparator(path.codeUnitAt(path.length - 1))) 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 isSeparator(path.codeUnitAt(0)) ? "/" : 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);
+ }
}
« no previous file with comments | « pkg/path/lib/src/style/posix.dart ('k') | pkg/path/lib/src/style/windows.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698