Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:collection'; | 5 import 'dart:collection'; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Implementation of [Uri] that understands only a limited set of valid | 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 | 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. | 10 * limited URI format, so almost always can be processed fast. |
| 11 */ | 11 */ |
| 12 class FastUri implements Uri { | 12 class FastUri implements Uri { |
| 13 /*** | 13 /*** |
| 14 * The maximum [_cache] length before we flush it and start a new generation. | 14 * The maximum [_cache] length before we flush it and start a new generation. |
| 15 */ | 15 */ |
| 16 static const int _MAX_CACHE_LENGTH_BEFORE_FLUSH = 50000; | 16 static const int _MAX_CACHE_LENGTH_BEFORE_FLUSH = 50000; |
| 17 | 17 |
| 18 static HashMap<String, Uri> _cache = new HashMap<String, Uri>(); | 18 static HashMap<String, Uri> _cache = new HashMap<String, Uri>(); |
| 19 static int _currentCacheLength = 0; | 19 static int _currentCacheLength = 0; |
| 20 static int _currentCacheGeneration = 0; | 20 static int _currentCacheGeneration = 0; |
| 21 | 21 |
| 22 final int _cacheGeneration; | 22 final int _cacheGeneration; |
| 23 final String _text; | 23 final String _text; |
| 24 final String _scheme; | 24 final String _scheme; |
| 25 final bool _hasEmptyAuthority; | |
| 25 final String _path; | 26 final String _path; |
| 26 | 27 |
| 27 /** | 28 /** |
| 28 * The offset of the last `/` in [_text], or `null` if there isn't any. | 29 * The offset of the last `/` in [_text], or `null` if there isn't any. |
| 29 */ | 30 */ |
| 30 final int _lastSlashIndex; | 31 final int _lastSlashIndex; |
| 31 | 32 |
| 32 /** | 33 /** |
| 33 * The cached hashcode. | 34 * The cached hashcode. |
| 34 */ | 35 */ |
| 35 int _hashCode; | 36 int _hashCode; |
| 36 | 37 |
| 37 Uri _cachedFallbackUri; | 38 Uri _cachedFallbackUri; |
| 38 | 39 |
| 39 FastUri._(this._cacheGeneration, this._text, this._scheme, this._path, | 40 FastUri._(this._cacheGeneration, this._text, this._scheme, |
| 40 this._lastSlashIndex); | 41 this._hasEmptyAuthority, this._path, this._lastSlashIndex); |
| 41 | 42 |
| 42 @override | 43 @override |
| 43 String get authority => ''; | 44 String get authority => ''; |
| 44 | 45 |
| 45 @override | 46 @override |
| 46 UriData get data => null; | 47 UriData get data => null; |
| 47 | 48 |
| 48 @override | 49 @override |
| 49 String get fragment => ''; | 50 String get fragment => ''; |
| 50 | 51 |
| 51 @override | 52 @override |
| 52 bool get hasAbsolutePath => path.startsWith('/'); | 53 bool get hasAbsolutePath => path.startsWith('/'); |
| 53 | 54 |
| 54 @override | 55 @override |
| 55 bool get hasAuthority => false; | 56 bool get hasAuthority => _hasEmptyAuthority; |
| 56 | 57 |
| 57 @override | 58 @override |
| 58 bool get hasEmptyPath => _path.isEmpty; | 59 bool get hasEmptyPath => _path.isEmpty; |
| 59 | 60 |
| 60 @override | 61 @override |
| 61 bool get hasFragment => false; | 62 bool get hasFragment => false; |
| 62 | 63 |
| 63 @override | 64 @override |
| 64 int get hashCode { | 65 int get hashCode { |
| 65 _hashCode ??= (scheme.hashCode * 31 + path.hashCode) & 0x3FFFFFFF; | 66 // This code is copied from the standard Uri implementation. |
| 66 return _hashCode; | 67 int combine(part, current) { |
| 68 // The sum is truncated to 30 bits to make sure it fits into a Smi. | |
| 69 return (current * 31 + part.hashCode) & 0x3FFFFFFF; | |
| 70 } | |
| 71 return _hashCode ??= combine( | |
| 72 scheme, | |
| 73 combine( | |
| 74 userInfo, | |
| 75 combine( | |
| 76 host, | |
| 77 combine(port, | |
| 78 combine(path, combine(query, combine(fragment, 1))))))); | |
| 67 } | 79 } |
| 68 | 80 |
| 69 @override | 81 @override |
| 70 bool get hasPort => false; | 82 bool get hasPort => false; |
| 71 | 83 |
| 72 @override | 84 @override |
| 73 bool get hasQuery => false; | 85 bool get hasQuery => false; |
| 74 | 86 |
| 75 @override | 87 @override |
| 76 bool get hasScheme => _scheme.isNotEmpty; | 88 bool get hasScheme => _scheme.isNotEmpty; |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 255 if (schemeEnd != null) { | 267 if (schemeEnd != null) { |
| 256 return null; | 268 return null; |
| 257 } | 269 } |
| 258 schemeEnd = i; | 270 schemeEnd = i; |
| 259 pathStart = i + 1; | 271 pathStart = i + 1; |
| 260 } else { | 272 } else { |
| 261 return null; | 273 return null; |
| 262 } | 274 } |
| 263 } | 275 } |
| 264 String scheme = schemeEnd != null ? text.substring(0, schemeEnd) : ''; | 276 String scheme = schemeEnd != null ? text.substring(0, schemeEnd) : ''; |
| 277 bool hasEmptyAuthority = false; | |
| 265 String path = text.substring(pathStart); | 278 String path = text.substring(pathStart); |
| 266 if (path.startsWith('//')) { | 279 if (path.startsWith('//')) { |
| 280 if (!path.startsWith('/')) { | |
|
Brian Wilkerson
2016/05/23 13:53:53
If the path starts with two slashes, how can it no
scheglov
2016/05/23 13:58:10
Ouch...
This code should be after updating the pat
| |
| 281 return null; | |
| 282 } | |
| 283 hasEmptyAuthority = true; | |
| 267 path = path.substring(2); | 284 path = path.substring(2); |
| 268 } | 285 } |
| 269 return new FastUri._( | 286 return new FastUri._(_currentCacheGeneration, text, scheme, |
| 270 _currentCacheGeneration, text, scheme, path, lastSlashIndex); | 287 hasEmptyAuthority, path, lastSlashIndex); |
| 271 } | 288 } |
| 272 } | 289 } |
| OLD | NEW |