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

Side by Side Diff: utils/pub/utils.dart

Issue 11348268: Don't require users to copy-paste authorization codes when authorizing pub. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years 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
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 /** 5 /**
6 * Generic utility functions. Stuff that should possibly be in core. 6 * Generic utility functions. Stuff that should possibly be in core.
7 */ 7 */
8 library utils; 8 library utils;
9 9
10 import 'dart:crypto'; 10 import 'dart:crypto';
11 import 'dart:isolate'; 11 import 'dart:isolate';
12 import 'dart:uri';
12 13
13 /** A pair of values. */ 14 /** A pair of values. */
14 class Pair<E, F> { 15 class Pair<E, F> {
15 E first; 16 E first;
16 F last; 17 F last;
17 18
18 Pair(this.first, this.last); 19 Pair(this.first, this.last);
19 20
20 String toString() => '($first, $last)'; 21 String toString() => '($first, $last)';
21 22
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 /// Like [String.split], but only splits on the first occurrence of the pattern. 144 /// Like [String.split], but only splits on the first occurrence of the pattern.
144 /// This will always return an array of two elements or fewer. 145 /// This will always return an array of two elements or fewer.
145 List<String> split1(String toSplit, String pattern) { 146 List<String> split1(String toSplit, String pattern) {
146 if (toSplit.isEmpty) return <String>[]; 147 if (toSplit.isEmpty) return <String>[];
147 148
148 var index = toSplit.indexOf(pattern); 149 var index = toSplit.indexOf(pattern);
149 if (index == -1) return [toSplit]; 150 if (index == -1) return [toSplit];
150 return [toSplit.substring(0, index), 151 return [toSplit.substring(0, index),
151 toSplit.substring(index + pattern.length)]; 152 toSplit.substring(index + pattern.length)];
152 } 153 }
154
Bob Nystrom 2012/11/28 19:35:41 Did you copy these from pkg/http? If so, leave a c
nweiz 2012/11/28 19:42:14 Done.
155 /// Adds additional query parameters to [url], overwriting the original
156 /// parameters if a name conflict occurs.
157 Uri addQueryParameters(Uri url, Map<String, String> parameters) {
Bob Nystrom 2012/11/28 19:35:41 "add" -> "set" since it replaces.
nweiz 2012/11/28 19:42:14 "set" seems like it implies that it mutates the UR
158 var queryMap = queryToMap(url.query);
159 mapAddAll(queryMap, parameters);
160 return url.resolve("?${mapToQuery(queryMap)}");
161 }
162
163 /// Convert a URL query string (or `application/x-www-form-urlencoded` body)
164 /// into a [Map] from parameter names to values.
165 Map<String, String> queryToMap(String queryList) {
166 var map = <String>{};
167 for (var pair in queryList.split("&")) {
168 var split = split1(pair, "=");
169 if (split.isEmpty) continue;
170 var key = urlDecode(split[0]);
171 var value = split.length > 1 ? urlDecode(split[1]) : "";
172 map[key] = value;
173 }
174 return map;
175 }
176
177 /// Convert a [Map] from parameter names to values to a URL query string.
178 String mapToQuery(Map<String, String> map) {
179 var pairs = <List<String>>[];
180 map.forEach((key, value) {
181 key = encodeUriComponent(key);
182 value = (value == null || value.isEmpty) ? null : encodeUriComponent(value);
183 pairs.add([key, value]);
184 });
185 return Strings.join(pairs.map((pair) {
186 if (pair[1] == null) return pair[0];
187 return "${pair[0]}=${pair[1]}";
188 }), "&");
189 }
190
191 /// Add all key/value pairs from [source] to [destination], overwriting any
192 /// pre-existing values.
193 void mapAddAll(Map destination, Map source) =>
194 source.forEach((key, value) => destination[key] = value);
195
196 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes
197 /// replacing `+` with ` `.
198 String urlDecode(String encoded) =>
199 decodeUriComponent(encoded.replaceAll("+", " "));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698