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

Side by Side Diff: sdk/lib/core/uri.dart

Issue 16108003: Avoid parsing path and query string more than once (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/corelib/uri_path_test.dart » ('j') | tests/corelib/uri_path_test.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 part of dart.core; 5 part of dart.core;
6 6
7 /** 7 /**
8 * A parsed URI, as specified by RFC-3986, http://tools.ietf.org/html/rfc3986. 8 * A parsed URI, as specified by RFC-3986, http://tools.ietf.org/html/rfc3986.
9 */ 9 */
10 class Uri { 10 class Uri {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 final String query; 74 final String query;
75 75
76 /** 76 /**
77 * Returns the fragment identifier component. 77 * Returns the fragment identifier component.
78 * 78 *
79 * Returns the empty string if there is no fragment identifier 79 * Returns the empty string if there is no fragment identifier
80 * component. 80 * component.
81 */ 81 */
82 final String fragment; 82 final String fragment;
83 83
84 List<String> _pathSegments;
Lasse Reichstein Nielsen 2013/05/30 11:57:50 Dartdoc saying that this caches the computed retur
Søren Gjesse 2013/05/30 13:38:25 Done.
85 Map<String, String> _queryParameters;
Lasse Reichstein Nielsen 2013/05/30 11:57:50 Similar.
Søren Gjesse 2013/05/30 13:38:25 Done.
86
84 /** 87 /**
85 * Creates a new URI object by parsing a URI string. 88 * Creates a new URI object by parsing a URI string.
86 */ 89 */
87 static Uri parse(String uri) => new Uri._fromMatch(_splitRe.firstMatch(uri)); 90 static Uri parse(String uri) => new Uri._fromMatch(_splitRe.firstMatch(uri));
88 91
89 Uri._fromMatch(Match m) : 92 Uri._fromMatch(Match m) :
90 this(scheme: _emptyIfNull(m[_COMPONENT_SCHEME]), 93 this(scheme: _emptyIfNull(m[_COMPONENT_SCHEME]),
91 userInfo: _emptyIfNull(m[_COMPONENT_USER_INFO]), 94 userInfo: _emptyIfNull(m[_COMPONENT_USER_INFO]),
92 host: _eitherOf( 95 host: _eitherOf(
93 m[_COMPONENT_HOST], m[_COMPONENT_HOST_IPV6]), 96 m[_COMPONENT_HOST], m[_COMPONENT_HOST_IPV6]),
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 _port = 0; 163 _port = 0;
161 } else { 164 } else {
162 _port = port; 165 _port = port;
163 } 166 }
164 } 167 }
165 168
166 /* 169 /*
167 * Returns the URI path split into its segments. Each of the 170 * Returns the URI path split into its segments. Each of the
168 * segments in the returned list have been decoded. If the path is 171 * segments in the returned list have been decoded. If the path is
169 * empty the empty list will be returned. 172 * empty the empty list will be returned.
173 *
174 * The returned list should not be modified as the same object will
175 * be returned for each call.
Lasse Reichstein Nielsen 2013/05/30 11:57:50 "should not" isn't very safe. Could you make it un
Søren Gjesse 2013/05/30 13:38:25 Created an immutable list wrapper.
170 */ 176 */
171 List<String> get pathSegments { 177 List<String> get pathSegments {
172 if (path == "") return const<String>[]; 178 if (_pathSegments == null) {
173 return path.split("/").map(Uri.decodeComponent).toList(growable: false); 179 _pathSegments =
180 path == "" ? const<String>[]
181 : path.split("/")
182 .map(Uri.decodeComponent)
183 .toList(growable: false);
184 }
185 return _pathSegments;
174 } 186 }
175 187
176 /* 188 /*
177 * Returns the URI query split into a map according to the rules 189 * Returns the URI query split into a map according to the rules
178 * specified for FORM post in the HTML 4.01 specification. Each key 190 * specified for FORM post in the HTML 4.01 specification. Each key
179 * and value in the returned map have been decoded. If there is no 191 * and value in the returned map have been decoded. If there is no
180 * query the empty map will be returned. 192 * query the empty map will be returned.
193 *
194 * The returned map should not be modified as the same object will
195 * be returned for each call.
181 */ 196 */
182 Map<String, String> get queryParameters { 197 Map<String, String> get queryParameters {
183 return query.split("&").fold({}, (map, element) { 198 if (_queryParameters == null) {
199 _queryParameters = query.split("&").fold({}, (map, element) {
184 int index = element.indexOf("="); 200 int index = element.indexOf("=");
185 if (index == -1) { 201 if (index == -1) {
186 if (!element.isEmpty) map[element] = ""; 202 if (!element.isEmpty) map[element] = "";
187 } else if (index != 0) { 203 } else if (index != 0) {
188 var key = element.substring(0, index); 204 var key = element.substring(0, index);
189 var value = element.substring(index + 1); 205 var value = element.substring(index + 1);
190 map[Uri.decodeQueryComponent(key)] = decodeQueryComponent(value); 206 map[Uri.decodeQueryComponent(key)] = decodeQueryComponent(value);
191 } 207 }
192 return map; 208 return map;
193 }); 209 });
210 }
211 return _queryParameters;
194 } 212 }
195 213
196 static String _makeScheme(String scheme) { 214 static String _makeScheme(String scheme) {
197 bool isSchemeLowerCharacter(int ch) { 215 bool isSchemeLowerCharacter(int ch) {
198 return ch < 128 && 216 return ch < 128 &&
199 ((_schemeLowerTable[ch >> 4] & (1 << (ch & 0x0f))) != 0); 217 ((_schemeLowerTable[ch >> 4] & (1 << (ch & 0x0f))) != 0);
200 } 218 }
201 219
202 bool isSchemeCharacter(int ch) { 220 bool isSchemeCharacter(int ch) {
203 return ch < 128 && ((_schemeTable[ch >> 4] & (1 << (ch & 0x0f))) != 0); 221 return ch < 128 && ((_schemeTable[ch >> 4] & (1 << (ch & 0x0f))) != 0);
(...skipping 733 matching lines...) Expand 10 before | Expand all | Expand 10 after
937 0xafff, // 0x30 - 0x3f 1111111111110101 955 0xafff, // 0x30 - 0x3f 1111111111110101
938 // @ABCDEFGHIJKLMNO 956 // @ABCDEFGHIJKLMNO
939 0xffff, // 0x40 - 0x4f 1111111111111111 957 0xffff, // 0x40 - 0x4f 1111111111111111
940 // PQRSTUVWXYZ _ 958 // PQRSTUVWXYZ _
941 0x87ff, // 0x50 - 0x5f 1111111111100001 959 0x87ff, // 0x50 - 0x5f 1111111111100001
942 // abcdefghijklmno 960 // abcdefghijklmno
943 0xfffe, // 0x60 - 0x6f 0111111111111111 961 0xfffe, // 0x60 - 0x6f 0111111111111111
944 // pqrstuvwxyz ~ 962 // pqrstuvwxyz ~
945 0x47ff]; // 0x70 - 0x7f 1111111111100010 963 0x47ff]; // 0x70 - 0x7f 1111111111100010
946 } 964 }
OLDNEW
« no previous file with comments | « no previous file | tests/corelib/uri_path_test.dart » ('j') | tests/corelib/uri_path_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698