Chromium Code Reviews| Index: pkg/analyzer/lib/src/util/fast_uri.dart |
| diff --git a/pkg/analyzer/lib/src/util/fast_uri.dart b/pkg/analyzer/lib/src/util/fast_uri.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5c1906604ff0343f82cca00cba3f4407d01fa8f9 |
| --- /dev/null |
| +++ b/pkg/analyzer/lib/src/util/fast_uri.dart |
| @@ -0,0 +1,253 @@ |
| +// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +import 'dart:collection'; |
| + |
| +/** |
| + * Implementation of [Uri] that understands only a limited set of valid |
| + * URI formats, but works fast. In practice Dart code almost always uses such |
| + * limited URI format, so almost always can be processed fast. |
| + */ |
| +class FastUri implements Uri { |
| + static HashMap<String, Uri> _cache = new HashMap<String, Uri>(); |
| + |
| + final String _text; |
| + final String _scheme; |
| + final String _path; |
| + |
| + /** |
| + * The offset of the last `/` in [_text], or `null` if there isn't any. |
| + */ |
| + final int _lastSlashIndex; |
| + |
| + /** |
| + * The cached hashcode. |
| + */ |
| + int _hashCode; |
| + |
| + Uri _cachedFallbackUri; |
| + |
| + FastUri(this._text, this._scheme, this._path, this._lastSlashIndex); |
| + |
| + @override |
| + String get authority => ''; |
| + |
| + @override |
| + UriData get data => null; |
| + |
| + @override |
| + String get fragment => ''; |
| + |
| + @override |
| + bool get hasAbsolutePath => path.startsWith('/'); |
| + |
| + @override |
| + bool get hasAuthority => false; |
| + |
| + @override |
| + bool get hasEmptyPath => _path.isEmpty; |
| + |
| + @override |
| + bool get hasFragment => false; |
| + |
| + @override |
| + int get hashCode { |
| + _hashCode ??= (scheme.hashCode * 31 + path.hashCode) & 0x3FFFFFFF; |
| + return _hashCode; |
| + } |
| + |
| + @override |
| + bool get hasPort => false; |
| + |
| + @override |
| + bool get hasQuery => false; |
| + |
| + @override |
| + bool get hasScheme => _scheme.isNotEmpty; |
| + |
| + @override |
| + String get host => ''; |
| + |
| + @override |
| + bool get isAbsolute => hasScheme; |
| + |
| + @override |
| + String get origin => _fallbackUri.origin; |
| + |
| + @override |
| + String get path => _path; |
| + |
| + @override |
| + List<String> get pathSegments => _fallbackUri.pathSegments; |
| + |
| + @override |
| + int get port => 0; |
| + |
| + @override |
| + String get query => ''; |
| + |
| + @override |
| + Map<String, String> get queryParameters => const <String, String>{}; |
| + |
| + @override |
| + Map<String, List<String>> get queryParametersAll => |
| + const <String, List<String>>{}; |
| + |
| + @override |
| + String get scheme => _scheme; |
| + |
| + @override |
| + String get userInfo => ''; |
| + |
| + /** |
| + * Full [Uri] object computed on demand; we fall back to this for some of the |
| + * more complex methods of [Uri] that are less in need of a fast |
| + * implementation. |
| + */ |
| + Uri get _fallbackUri => _cachedFallbackUri ??= Uri.parse(_text); |
| + |
| + @override |
| + bool operator ==(other) { |
| + if (other is Uri) { |
| + if (other is FastUri) { |
|
Brian Wilkerson
2016/05/20 16:56:54
Won't this be the more common case? If so, it migh
|
| + return _text == other._text; |
| + } |
| + return _fallbackUri == other; |
| + } |
| + return false; |
| + } |
| + |
| + @override |
| + Uri normalizePath() { |
| + return this; |
| + } |
| + |
| + @override |
| + Uri removeFragment() { |
| + return this; |
| + } |
| + |
| + @override |
| + Uri replace( |
| + {String scheme, |
| + String userInfo, |
| + String host, |
| + int port, |
| + String path, |
| + Iterable<String> pathSegments, |
| + String query, |
| + Map<String, dynamic> queryParameters, |
| + String fragment}) { |
| + return _fallbackUri.replace( |
| + scheme: scheme, |
| + userInfo: userInfo, |
| + host: host, |
| + port: port, |
| + path: path, |
| + pathSegments: pathSegments, |
| + query: query, |
| + queryParameters: queryParameters, |
| + fragment: fragment); |
| + } |
| + |
| + @override |
| + Uri resolve(String reference) { |
| + // TODO: maybe implement faster |
| + return _fallbackUri.resolve(reference); |
| + } |
| + |
| + @override |
| + Uri resolveUri(Uri reference) { |
| + if (reference.hasScheme) { |
| + return reference; |
| + } |
| + String refPath = reference.path; |
| + if (refPath.startsWith('./')) { |
| + refPath = refPath.substring(2); |
| + } |
| + if (refPath.startsWith('../') || |
| + refPath.contains('/../') || |
| + refPath.contains('/./')) { |
| + Uri slowResult = _fallbackUri.resolveUri(reference); |
| + return FastUri.parse(slowResult.toString()); |
| + } |
| + String newText; |
| + if (_lastSlashIndex != null) { |
| + newText = _text.substring(0, _lastSlashIndex + 1) + refPath; |
| + } else { |
| + newText = _text + '/' + refPath; |
| + } |
| + return FastUri.parse(newText); |
| + } |
| + |
| + @override |
| + String toFilePath({bool windows}) { |
| + return _fallbackUri.toFilePath(windows: windows); |
| + } |
| + |
| + @override |
| + String toString() => _text; |
| + |
| + /** |
| + * Parse the given URI [text] and return the corresponding [Uri] instance. If |
| + * the [text] can be represented as a [FastUri], then it is returned. If the |
| + * [text] is more complex, then `dart:core` [Uri] is created and returned. |
| + * This method also performs memoization, so that usually the same instance |
|
Paul Berry
2016/05/20 16:50:39
Why "usually"? From the implementation it looks l
|
| + * of [FastUri] or [Uri] is returned for the same [text]. |
| + */ |
| + static Uri parse(String text) { |
| + Uri uri = _cache[text]; |
| + if (uri == null) { |
| + uri = _parse(text); |
| + uri ??= Uri.parse(text); |
| + _cache[text] = uri; |
| + } |
| + return uri; |
| + } |
| + |
| + static bool _isAlphabetic(int char) { |
| + return char >= 'A'.codeUnitAt(0) && char <= 'Z'.codeUnitAt(0) || |
| + char >= 'a'.codeUnitAt(0) && char <= 'z'.codeUnitAt(0); |
| + } |
| + |
| + static bool _isDigit(int char) { |
| + return char >= '0'.codeUnitAt(0) && char <= '9'.codeUnitAt(0); |
| + } |
| + |
| + /** |
| + * Parse the given [text] into a new [FastUri]. If the [text] uses URI |
| + * features that are not supported by [FastUri], return `null`. |
| + */ |
| + static FastUri _parse(String text) { |
| + int schemeEnd = null; |
| + int pathStart = 0; |
| + int lastSlashIndex = null; |
| + for (int i = 0; i < text.length; i++) { |
| + int char = text.codeUnitAt(i); |
| + if (_isAlphabetic(char) || |
| + _isDigit(char) || |
| + char == '.'.codeUnitAt(0) || |
| + char == '-'.codeUnitAt(0) || |
| + char == '_'.codeUnitAt(0)) { |
| + // Valid characters. |
| + } else if (char == '/'.codeUnitAt(0)) { |
| + lastSlashIndex = i; |
| + } else if (char == ':'.codeUnitAt(0)) { |
| + if (schemeEnd != null) { |
| + return null; |
| + } |
| + schemeEnd = i; |
| + pathStart = i + 1; |
| + } else { |
| + return null; |
| + } |
| + } |
| + String scheme = schemeEnd != null ? text.substring(0, schemeEnd) : ''; |
| + String path = text.substring(pathStart); |
| + if (path.startsWith('//')) { |
| + path = path.substring(2); |
| + } |
| + return new FastUri(text, scheme, path, lastSlashIndex); |
| + } |
| +} |