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

Side by Side Diff: pkg/analyzer/lib/src/util/fast_uri.dart

Issue 2003633002: Use FastUri instead of Uri in analyzer. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Added test file. Created 4 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 unified diff | Download patch
OLDNEW
(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 static HashMap<String, Uri> _cache = new HashMap<String, Uri>();
14
15 final String _text;
16 final String _scheme;
17 final String _path;
18 final List<int> _pathSegmentStarts;
19
20 /**
21 * The cached hashcode.
22 */
23 int _hashCode;
24
25 Uri _cachedFallbackUri;
26
27 FastUri(this._text, this._scheme, this._path, this._pathSegmentStarts);
28
29 @override
30 String get authority => '';
31
32 @override
33 UriData get data => null;
34
35 @override
36 String get fragment => '';
37
38 @override
39 bool get hasAbsolutePath => path.startsWith('/');
40
41 @override
42 bool get hasAuthority => false;
43
44 @override
45 bool get hasEmptyPath => _path.isEmpty;
46
47 @override
48 bool get hasFragment => false;
49
50 @override
51 int get hashCode {
52 _hashCode ??= (scheme.hashCode * 31 + path.hashCode) & 0x3FFFFFFF;
53 return _hashCode;
54 }
55
56 @override
57 bool get hasPort => false;
58
59 @override
60 bool get hasQuery => false;
61
62 @override
63 bool get hasScheme => _scheme.isNotEmpty;
64
65 @override
66 String get host => '';
67
68 @override
69 bool get isAbsolute => hasScheme;
70
71 @override
72 String get origin => _fallbackUri.origin;
73
74 @override
75 String get path => _path;
76
77 @override
78 List<String> get pathSegments => _fallbackUri.pathSegments;
79
80 @override
81 int get port => 0;
82
83 @override
84 String get query => '';
85
86 @override
87 Map<String, String> get queryParameters => const <String, String>{};
88
89 @override
90 Map<String, List<String>> get queryParametersAll =>
91 const <String, List<String>>{};
92
93 @override
94 String get scheme => _scheme;
95
96 @override
97 String get userInfo => '';
98
99 Uri get _fallbackUri => _cachedFallbackUri ??= Uri.parse(_text);
100
101 @override
102 bool operator ==(other) {
103 if (other is Uri) {
104 if (other is FastUri) {
105 return _text == other._text;
106 }
107 return _fallbackUri == other;
108 }
109 return false;
110 }
111
112 @override
113 Uri normalizePath() {
114 return this;
115 }
116
117 @override
118 Uri removeFragment() {
119 return this;
120 }
121
122 @override
123 Uri replace(
124 {String scheme,
125 String userInfo,
126 String host,
127 int port,
128 String path,
129 Iterable<String> pathSegments,
130 String query,
131 Map<String, dynamic> queryParameters,
132 String fragment}) {
133 return _fallbackUri.replace(
134 scheme: scheme,
135 userInfo: userInfo,
136 host: host,
137 port: port,
138 path: path,
139 pathSegments: pathSegments,
140 query: query,
141 queryParameters: queryParameters,
142 fragment: fragment);
143 }
144
145 @override
146 Uri resolve(String reference) {
147 // TODO: maybe implement faster
148 return _fallbackUri.resolve(reference);
149 }
150
151 @override
152 Uri resolveUri(Uri reference) {
153 if (reference.hasScheme) {
154 return reference;
155 }
156 String refPath = reference.path;
157 if (refPath.startsWith('./')) {
158 refPath = refPath.substring(2);
159 }
160 if (refPath.startsWith('../') || refPath.contains('/../') || refPath.contain s('/./')) {
161 Uri slowResult = _fallbackUri.resolveUri(reference);
162 return FastUri.parse(slowResult.toString());
163 }
164 String newText = _text.substring(0, _pathSegmentStarts.last + 1) + refPath;
165 return FastUri.parse(newText);
166 }
167
168 @override
169 String toFilePath({bool windows}) {
170 return _fallbackUri.toFilePath(windows: windows);
171 }
172
173 @override
174 String toString() => _text;
175
176 static Uri parse(String text) {
177 Uri uri = _cache[text];
178 if (uri == null) {
179 _UriData data = _parse(text);
180 if (data == null) {
181 uri = Uri.parse(text);
182 } else {
183 uri = new FastUri(text, data.scheme, data.path, data.pathSegmentStarts);
184 }
185 _cache[text] = uri;
186 }
187 return uri;
188 }
189
190 static bool _isAlphabetic(int char) {
191 return char >= 'A'.codeUnitAt(0) && char <= 'Z'.codeUnitAt(0) ||
192 char >= 'a'.codeUnitAt(0) && char <= 'z'.codeUnitAt(0);
193 }
194
195 static bool _isDigit(int char) {
196 return char >= '0'.codeUnitAt(0) && char <= '9'.codeUnitAt(0);
197 }
198
199 static _UriData _parse(String text) {
200 int schemeEnd = null;
201 int pathStart = 0;
202 List<int> pathSegmentStarts = <int>[];
203 for (int i = 0; i < text.length; i++) {
204 int char = text.codeUnitAt(i);
205 if (_isAlphabetic(char) ||
206 _isDigit(char) ||
207 char == '.'.codeUnitAt(0) ||
208 char == '-'.codeUnitAt(0) ||
209 char == '_'.codeUnitAt(0)) {
210 // Valid characters.
211 } else if (char == '/'.codeUnitAt(0)) {
212 pathSegmentStarts.add(i);
213 } else if (char == ':'.codeUnitAt(0)) {
214 if (schemeEnd != null) {
215 return null;
216 }
217 schemeEnd = i;
218 pathStart = i + 1;
219 } else {
220 return null;
221 }
222 }
223 String scheme = schemeEnd != null ? text.substring(0, schemeEnd) : '';
224 String path = text.substring(pathStart);
225 if (path.startsWith('//')) {
226 path = path.substring(2);
227 }
228 return new _UriData(scheme, path, pathSegmentStarts);
229 }
230 }
231
232 class _UriData {
233 final String scheme;
234 final String path;
235 final List<int> pathSegmentStarts;
236
237 _UriData(this.scheme, this.path, this.pathSegmentStarts);
238 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698