OLD | NEW |
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 /// Generic utility functions. Stuff that should possibly be in core. | 5 /// Generic utility functions. Stuff that should possibly be in core. |
6 library utils; | 6 library utils; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:crypto'; | 9 import 'dart:crypto'; |
10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 /// parameters if a name conflict occurs. | 136 /// parameters if a name conflict occurs. |
137 Uri addQueryParameters(Uri url, Map<String, String> parameters) { | 137 Uri addQueryParameters(Uri url, Map<String, String> parameters) { |
138 var queryMap = queryToMap(url.query); | 138 var queryMap = queryToMap(url.query); |
139 mapAddAll(queryMap, parameters); | 139 mapAddAll(queryMap, parameters); |
140 return url.resolve("?${mapToQuery(queryMap)}"); | 140 return url.resolve("?${mapToQuery(queryMap)}"); |
141 } | 141 } |
142 | 142 |
143 /// Convert a URL query string (or `application/x-www-form-urlencoded` body) | 143 /// Convert a URL query string (or `application/x-www-form-urlencoded` body) |
144 /// into a [Map] from parameter names to values. | 144 /// into a [Map] from parameter names to values. |
145 Map<String, String> queryToMap(String queryList) { | 145 Map<String, String> queryToMap(String queryList) { |
146 var map = <String>{}; | 146 var map = {}; |
147 for (var pair in queryList.split("&")) { | 147 for (var pair in queryList.split("&")) { |
148 var split = split1(pair, "="); | 148 var split = split1(pair, "="); |
149 if (split.isEmpty) continue; | 149 if (split.isEmpty) continue; |
150 var key = urlDecode(split[0]); | 150 var key = urlDecode(split[0]); |
151 var value = split.length > 1 ? urlDecode(split[1]) : ""; | 151 var value = split.length > 1 ? urlDecode(split[1]) : ""; |
152 map[key] = value; | 152 map[key] = value; |
153 } | 153 } |
154 return map; | 154 return map; |
155 } | 155 } |
156 | 156 |
(...skipping 13 matching lines...) Expand all Loading... |
170 | 170 |
171 /// Add all key/value pairs from [source] to [destination], overwriting any | 171 /// Add all key/value pairs from [source] to [destination], overwriting any |
172 /// pre-existing values. | 172 /// pre-existing values. |
173 void mapAddAll(Map destination, Map source) => | 173 void mapAddAll(Map destination, Map source) => |
174 source.forEach((key, value) => destination[key] = value); | 174 source.forEach((key, value) => destination[key] = value); |
175 | 175 |
176 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 176 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes |
177 /// replacing `+` with ` `. | 177 /// replacing `+` with ` `. |
178 String urlDecode(String encoded) => | 178 String urlDecode(String encoded) => |
179 decodeUriComponent(encoded.replaceAll("+", " ")); | 179 decodeUriComponent(encoded.replaceAll("+", " ")); |
OLD | NEW |