| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import 'dart:collection'; | |
| 6 | |
| 7 /** | |
| 8 * Implementation of [Uri] that understands only a limited set of valid | |
| 9 * URI formats, but works fast. In practice Dart code almost always uses such | |
| 10 * limited URI format, so almost always can be processed fast. | |
| 11 */ | |
| 12 class FastUri implements Uri { | |
| 13 /*** | |
| 14 * The maximum [_cache] length before we flush it and start a new generation. | |
| 15 */ | |
| 16 static const int _MAX_CACHE_LENGTH_BEFORE_FLUSH = 50000; | |
| 17 | |
| 18 static HashMap<String, Uri> _cache = new HashMap<String, Uri>(); | |
| 19 static int _currentCacheLength = 0; | |
| 20 static int _currentCacheGeneration = 0; | |
| 21 | |
| 22 static bool _hashUsingText = _shouldComputeHashCodeUsingText(); | |
| 23 | |
| 24 final int _cacheGeneration; | |
| 25 final String _text; | |
| 26 final String _scheme; | |
| 27 final bool _hasEmptyAuthority; | |
| 28 final String _path; | |
| 29 | |
| 30 /** | |
| 31 * The offset of the last `/` in [_text], or `null` if there isn't any. | |
| 32 */ | |
| 33 final int _lastSlashIndex; | |
| 34 | |
| 35 /** | |
| 36 * The cached hash code. | |
| 37 */ | |
| 38 int _hashCode; | |
| 39 | |
| 40 Uri _cachedFallbackUri; | |
| 41 | |
| 42 FastUri._(this._cacheGeneration, this._text, this._scheme, | |
| 43 this._hasEmptyAuthority, this._path, this._lastSlashIndex); | |
| 44 | |
| 45 @override | |
| 46 String get authority => ''; | |
| 47 | |
| 48 @override | |
| 49 UriData get data => null; | |
| 50 | |
| 51 @override | |
| 52 String get fragment => ''; | |
| 53 | |
| 54 @override | |
| 55 bool get hasAbsolutePath => path.startsWith('/'); | |
| 56 | |
| 57 @override | |
| 58 bool get hasAuthority => _hasEmptyAuthority; | |
| 59 | |
| 60 @override | |
| 61 bool get hasEmptyPath => _path.isEmpty; | |
| 62 | |
| 63 @override | |
| 64 bool get hasFragment => false; | |
| 65 | |
| 66 @override | |
| 67 int get hashCode { | |
| 68 return _hashCode ??= _hashUsingText | |
| 69 ? _computeHashUsingText(this) | |
| 70 : _computeHashUsingCombine(this); | |
| 71 } | |
| 72 | |
| 73 @override | |
| 74 bool get hasPort => false; | |
| 75 | |
| 76 @override | |
| 77 bool get hasQuery => false; | |
| 78 | |
| 79 @override | |
| 80 bool get hasScheme => _scheme.isNotEmpty; | |
| 81 | |
| 82 @override | |
| 83 String get host => ''; | |
| 84 | |
| 85 @override | |
| 86 bool get isAbsolute => hasScheme; | |
| 87 | |
| 88 @override | |
| 89 String get origin => _fallbackUri.origin; | |
| 90 | |
| 91 @override | |
| 92 String get path => _path; | |
| 93 | |
| 94 @override | |
| 95 List<String> get pathSegments => _fallbackUri.pathSegments; | |
| 96 | |
| 97 @override | |
| 98 int get port => 0; | |
| 99 | |
| 100 @override | |
| 101 String get query => ''; | |
| 102 | |
| 103 @override | |
| 104 Map<String, String> get queryParameters => const <String, String>{}; | |
| 105 | |
| 106 @override | |
| 107 Map<String, List<String>> get queryParametersAll => | |
| 108 const <String, List<String>>{}; | |
| 109 | |
| 110 @override | |
| 111 String get scheme => _scheme; | |
| 112 | |
| 113 @override | |
| 114 String get userInfo => ''; | |
| 115 | |
| 116 /** | |
| 117 * Full [Uri] object computed on demand; we fall back to this for some of the | |
| 118 * more complex methods of [Uri] that are less in need of a fast | |
| 119 * implementation. | |
| 120 */ | |
| 121 Uri get _fallbackUri => _cachedFallbackUri ??= Uri.parse(_text); | |
| 122 | |
| 123 @override | |
| 124 bool operator ==(other) { | |
| 125 if (other is FastUri) { | |
| 126 if (other._cacheGeneration == _cacheGeneration) { | |
| 127 return identical(other, this); | |
| 128 } | |
| 129 return _text == other._text; | |
| 130 } else if (other is Uri) { | |
| 131 return _fallbackUri == other; | |
| 132 } | |
| 133 return false; | |
| 134 } | |
| 135 | |
| 136 @override | |
| 137 bool isScheme(String scheme) { | |
| 138 String thisScheme = this.scheme; | |
| 139 if (scheme == null) return thisScheme.isEmpty; | |
| 140 if (scheme.length != thisScheme.length) return false; | |
| 141 return _compareScheme(scheme, thisScheme); | |
| 142 } | |
| 143 | |
| 144 @override | |
| 145 Uri normalizePath() { | |
| 146 return this; | |
| 147 } | |
| 148 | |
| 149 @override | |
| 150 Uri removeFragment() { | |
| 151 return this; | |
| 152 } | |
| 153 | |
| 154 @override | |
| 155 Uri replace( | |
| 156 {String scheme, | |
| 157 String userInfo, | |
| 158 String host, | |
| 159 int port, | |
| 160 String path, | |
| 161 Iterable<String> pathSegments, | |
| 162 String query, | |
| 163 Map<String, dynamic> queryParameters, | |
| 164 String fragment}) { | |
| 165 return _fallbackUri.replace( | |
| 166 scheme: scheme, | |
| 167 userInfo: userInfo, | |
| 168 host: host, | |
| 169 port: port, | |
| 170 path: path, | |
| 171 pathSegments: pathSegments, | |
| 172 query: query, | |
| 173 queryParameters: queryParameters, | |
| 174 fragment: fragment); | |
| 175 } | |
| 176 | |
| 177 @override | |
| 178 Uri resolve(String reference) { | |
| 179 // TODO: maybe implement faster | |
| 180 return _fallbackUri.resolve(reference); | |
| 181 } | |
| 182 | |
| 183 @override | |
| 184 Uri resolveUri(Uri reference) { | |
| 185 if (reference.hasScheme) { | |
| 186 return reference; | |
| 187 } | |
| 188 String refPath = reference.path; | |
| 189 if (refPath.startsWith('./')) { | |
| 190 refPath = refPath.substring(2); | |
| 191 } | |
| 192 if (refPath.startsWith('../') || | |
| 193 refPath.contains('/../') || | |
| 194 refPath.contains('/./') || | |
| 195 refPath.startsWith('/')) { | |
| 196 Uri slowResult = _fallbackUri.resolveUri(reference); | |
| 197 return FastUri.parse(slowResult.toString()); | |
| 198 } | |
| 199 String newText; | |
| 200 if (_lastSlashIndex != null) { | |
| 201 newText = _text.substring(0, _lastSlashIndex + 1) + refPath; | |
| 202 } else { | |
| 203 newText = _text + '/' + refPath; | |
| 204 } | |
| 205 return FastUri.parse(newText); | |
| 206 } | |
| 207 | |
| 208 @override | |
| 209 String toFilePath({bool windows}) { | |
| 210 return _fallbackUri.toFilePath(windows: windows); | |
| 211 } | |
| 212 | |
| 213 @override | |
| 214 String toString() => _text; | |
| 215 | |
| 216 /** | |
| 217 * Parse the given URI [text] and return the corresponding [Uri] instance. If | |
| 218 * the [text] can be represented as a [FastUri], then it is returned. If the | |
| 219 * [text] is more complex, then `dart:core` [Uri] is created and returned. | |
| 220 * This method also performs memoization, so that usually the same instance | |
| 221 * of [FastUri] or [Uri] is returned for the same [text]. | |
| 222 */ | |
| 223 static Uri parse(String text) { | |
| 224 Uri uri = _cache[text]; | |
| 225 if (uri == null) { | |
| 226 uri = _parse(text); | |
| 227 uri ??= Uri.parse(text); | |
| 228 _cache[text] = uri; | |
| 229 _currentCacheLength++; | |
| 230 // If the cache is too big, start a new generation. | |
| 231 if (_currentCacheLength > _MAX_CACHE_LENGTH_BEFORE_FLUSH) { | |
| 232 _cache.clear(); | |
| 233 _currentCacheLength = 0; | |
| 234 _currentCacheGeneration++; | |
| 235 } | |
| 236 } | |
| 237 return uri; | |
| 238 } | |
| 239 | |
| 240 /** | |
| 241 * Compares scheme characters in [scheme] and at the start of [uri]. | |
| 242 * | |
| 243 * Returns `true` if [scheme] represents the same scheme as the start of | |
| 244 * [uri]. That means having the same characters, but possibly different case | |
| 245 * for letters. | |
| 246 * | |
| 247 * This function doesn't check that the characters are valid URI scheme | |
| 248 * characters. The [uri] is assumed to be valid, so if [scheme] matches | |
| 249 * it, it has to be valid too. | |
| 250 * | |
| 251 * The length should be tested before calling this function, | |
| 252 * so the scheme part of [uri] is known to have the same length as [scheme]. | |
| 253 */ | |
| 254 static bool _compareScheme(String scheme, String uri) { | |
| 255 for (int i = 0; i < scheme.length; i++) { | |
| 256 int schemeChar = scheme.codeUnitAt(i); | |
| 257 int uriChar = uri.codeUnitAt(i); | |
| 258 int delta = schemeChar ^ uriChar; | |
| 259 if (delta != 0) { | |
| 260 if (delta == 0x20) { | |
| 261 // Might be a case difference. | |
| 262 int lowerChar = uriChar | delta; | |
| 263 if (0x61 /*a*/ <= lowerChar && lowerChar <= 0x7a /*z*/) { | |
| 264 continue; | |
| 265 } | |
| 266 } | |
| 267 return false; | |
| 268 } | |
| 269 } | |
| 270 return true; | |
| 271 } | |
| 272 | |
| 273 /** | |
| 274 * This implementation was used before 'fast-URI' in Dart VM. | |
| 275 */ | |
| 276 static int _computeHashUsingCombine(FastUri uri) { | |
| 277 // This code is copied from the standard Uri implementation. | |
| 278 // It is important that Uri and FastUri generate compatible hashCodes | |
| 279 // because Uri and FastUri may be used as keys in the same map. | |
| 280 int combine(part, current) { | |
| 281 // The sum is truncated to 30 bits to make sure it fits into a Smi. | |
| 282 return (current * 31 + part.hashCode) & 0x3FFFFFFF; | |
| 283 } | |
| 284 | |
| 285 return combine( | |
| 286 uri.scheme, | |
| 287 combine( | |
| 288 uri.userInfo, | |
| 289 combine( | |
| 290 uri.host, | |
| 291 combine( | |
| 292 uri.port, | |
| 293 combine(uri.path, | |
| 294 combine(uri.query, combine(uri.fragment, 1))))))); | |
| 295 } | |
| 296 | |
| 297 /** | |
| 298 * This implementation should be used with 'fast-URI' in Dart VM. | |
| 299 * https://github.com/dart-lang/sdk/commit/afbbbb97cfcd86a64d0ba5dcfe1ab758954
adaf4 | |
| 300 */ | |
| 301 static int _computeHashUsingText(FastUri uri) { | |
| 302 return uri._text.hashCode; | |
| 303 } | |
| 304 | |
| 305 static bool _isAlphabetic(int char) { | |
| 306 return char >= 'A'.codeUnitAt(0) && char <= 'Z'.codeUnitAt(0) || | |
| 307 char >= 'a'.codeUnitAt(0) && char <= 'z'.codeUnitAt(0); | |
| 308 } | |
| 309 | |
| 310 static bool _isDigit(int char) { | |
| 311 return char >= '0'.codeUnitAt(0) && char <= '9'.codeUnitAt(0); | |
| 312 } | |
| 313 | |
| 314 /** | |
| 315 * Parse the given [text] into a new [FastUri]. If the [text] uses URI | |
| 316 * features that are not supported by [FastUri], return `null`. | |
| 317 */ | |
| 318 static FastUri _parse(String text) { | |
| 319 int schemeEnd = null; | |
| 320 int pathStart = 0; | |
| 321 int lastSlashIndex = null; | |
| 322 for (int i = 0; i < text.length; i++) { | |
| 323 int char = text.codeUnitAt(i); | |
| 324 if (_isAlphabetic(char) || | |
| 325 _isDigit(char) || | |
| 326 char == '.'.codeUnitAt(0) || | |
| 327 char == '-'.codeUnitAt(0) || | |
| 328 char == '_'.codeUnitAt(0)) { | |
| 329 // Valid characters. | |
| 330 } else if (char == '/'.codeUnitAt(0)) { | |
| 331 lastSlashIndex = i; | |
| 332 } else if (char == ':'.codeUnitAt(0)) { | |
| 333 if (schemeEnd != null) { | |
| 334 return null; | |
| 335 } | |
| 336 schemeEnd = i; | |
| 337 pathStart = i + 1; | |
| 338 } else { | |
| 339 return null; | |
| 340 } | |
| 341 } | |
| 342 String scheme = schemeEnd != null ? text.substring(0, schemeEnd) : ''; | |
| 343 bool hasEmptyAuthority = false; | |
| 344 String path = text.substring(pathStart); | |
| 345 if (path.startsWith('//')) { | |
| 346 hasEmptyAuthority = true; | |
| 347 path = path.substring(2); | |
| 348 if (!path.startsWith('/')) { | |
| 349 return null; | |
| 350 } | |
| 351 } | |
| 352 return new FastUri._(_currentCacheGeneration, text, scheme, | |
| 353 hasEmptyAuthority, path, lastSlashIndex); | |
| 354 } | |
| 355 | |
| 356 /** | |
| 357 * Determine whether VM has the text based hash code computation in [Uri], | |
| 358 * or the old combine style. | |
| 359 * | |
| 360 * See https://github.com/dart-lang/sdk/issues/27159 for details. | |
| 361 */ | |
| 362 static bool _shouldComputeHashCodeUsingText() { | |
| 363 String text = 'package:foo/foo.dart'; | |
| 364 return Uri.parse(text).hashCode == text.hashCode; | |
| 365 } | |
| 366 } | |
| OLD | NEW |