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

Unified Diff: sdk/lib/io/http_utils.dart

Issue 14897006: Improve parsing of query string (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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 side-by-side diff with in-line comments
Download patch
Index: sdk/lib/io/http_utils.dart
diff --git a/sdk/lib/io/http_utils.dart b/sdk/lib/io/http_utils.dart
index 8e957bf7f2a0bc580c91d18b660efc82f0c5f3a8..36250425ac744f40de495b430c49cd251eaa0bb4 100644
--- a/sdk/lib/io/http_utils.dart
+++ b/sdk/lib/io/http_utils.dart
@@ -53,22 +53,52 @@ class _HttpUtils {
static Map<String, String> splitQueryString(String queryString) {
Map<String, String> result = new Map<String, String>();
int currentPosition = 0;
- while (currentPosition < queryString.length) {
- int position = queryString.indexOf("=", currentPosition);
- if (position == -1) {
- break;
+ int length = queryString.length;
+
+ while (currentPosition < length) {
+
+ // Find the first equals character between current position and
+ // the provided end.
+ int indexOfEquals(int end) {
+ int index = currentPosition;
+ while (index < end) {
+ if (queryString.codeUnitAt(index) == _CharCode.EQUAL) return index;
+ index++;
+ }
+ return -1;
+ }
+
+ // Find the next separator (either & or ;), see
+ // http://www.w3.org/TR/REC-html40/appendix/notes.html#ampersands-in-uris
+ // relating the ; separator. If no separator is found returns
+ // the length of the quesy string.
Mads Ager (google) 2013/05/15 09:17:35 query
Søren Gjesse 2013/05/15 09:22:18 Done.
+ int indexOfSeparator() {
+ int end = length;
+ int index = currentPosition;
+ while (index < end) {
+ int codeUnit = queryString.codeUnitAt(index);
+ if (codeUnit == _CharCode.AMPERSAND ||
+ codeUnit == _CharCode.SEMI_COLON) {
+ return index;
+ }
+ index++;
+ }
+ return end;
}
- String name = queryString.substring(currentPosition, position);
- currentPosition = position + 1;
- position = queryString.indexOf("&", currentPosition);
+
+ int seppos = indexOfSeparator();
+ int equalspos = indexOfEquals(seppos);
+ String name;
String value;
- if (position == -1) {
- value = queryString.substring(currentPosition);
- currentPosition = queryString.length;
+ if (equalspos == -1) {
+ name = queryString.substring(currentPosition, seppos);
+ value = '';
} else {
- value = queryString.substring(currentPosition, position);
- currentPosition = position + 1;
+ name = queryString.substring(currentPosition, equalspos);
+ value = queryString.substring(equalspos + 1, seppos);
}
+ currentPosition = seppos + 1; // This also works when seppos == length.
+ if (name == '') continue;
result[_HttpUtils.decodeUrlEncodedString(name)] =
_HttpUtils.decodeUrlEncodedString(value);
}

Powered by Google App Engine
This is Rietveld 408576698