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 library utils; | 5 library utils; |
6 | 6 |
| 7 import 'dart:async'; |
7 import 'dart:crypto'; | 8 import 'dart:crypto'; |
8 import 'dart:io'; | 9 import 'dart:io'; |
9 import 'dart:isolate'; | |
10 import 'dart:scalarlist'; | 10 import 'dart:scalarlist'; |
11 import 'dart:uri'; | 11 import 'dart:uri'; |
12 import 'dart:utf'; | 12 import 'dart:utf'; |
13 | 13 |
14 /// Converts a URL query string (or `application/x-www-form-urlencoded` body) | 14 /// Converts a URL query string (or `application/x-www-form-urlencoded` body) |
15 /// into a [Map] from parameter names to values. | 15 /// into a [Map] from parameter names to values. |
16 /// | 16 /// |
17 /// queryToMap("foo=bar&baz=bang&qux"); | 17 /// queryToMap("foo=bar&baz=bang&qux"); |
18 /// //=> {"foo": "bar", "baz": "bang", "qux": ""} | 18 /// //=> {"foo": "bar", "baz": "bang", "qux": ""} |
19 Map<String, String> queryToMap(String queryList) { | 19 Map<String, String> queryToMap(String queryList) { |
20 var map = <String>{}; | 20 var map = <String>{}; |
21 for (var pair in queryList.split("&")) { | 21 for (var pair in queryList.split("&")) { |
22 var split = split1(pair, "="); | 22 var split = split1(pair, "="); |
23 if (split.isEmpty) continue; | 23 if (split.isEmpty) continue; |
24 var key = urlDecode(split[0]); | 24 var key = urlDecode(split[0]); |
25 var value = urlDecode(split.length > 1 ? split[1] : ""); | 25 var value = urlDecode(split.length > 1 ? split[1] : ""); |
26 map[key] = value; | 26 map[key] = value; |
27 } | 27 } |
28 return map; | 28 return map; |
29 } | 29 } |
30 | 30 |
31 /// Converts a [Map] from parameter names to values to a URL query string. | 31 /// Converts a [Map] from parameter names to values to a URL query string. |
32 /// | 32 /// |
33 /// mapToQuery({"foo": "bar", "baz": "bang"}); | 33 /// mapToQuery({"foo": "bar", "baz": "bang"}); |
34 /// //=> "foo=bar&baz=bang" | 34 /// //=> "foo=bar&baz=bang" |
35 String mapToQuery(Map<String, String> map) { | 35 String mapToQuery(Map<String, String> map) { |
36 var pairs = <List<String>>[]; | 36 var pairs = <List<String>>[]; |
37 map.forEach((key, value) => | 37 map.forEach((key, value) => |
38 pairs.add([encodeUriComponent(key), encodeUriComponent(value)])); | 38 pairs.add([encodeUriComponent(key), encodeUriComponent(value)])); |
39 return Strings.join(pairs.map((pair) => "${pair[0]}=${pair[1]}"), "&"); | 39 return Strings.join(pairs.mappedBy((pair) => "${pair[0]}=${pair[1]}"), "&"); |
40 } | 40 } |
41 | 41 |
42 /// Adds all key/value pairs from [source] to [destination], overwriting any | 42 /// Adds all key/value pairs from [source] to [destination], overwriting any |
43 /// pre-existing values. | 43 /// pre-existing values. |
44 /// | 44 /// |
45 /// var a = {"foo": "bar", "baz": "bang"}; | 45 /// var a = {"foo": "bar", "baz": "bang"}; |
46 /// mapAddAll(a, {"baz": "zap", "qux": "quux"}); | 46 /// mapAddAll(a, {"baz": "zap", "qux": "quux"}); |
47 /// a; //=> {"foo": "bar", "baz": "zap", "qux": "quux"} | 47 /// a; //=> {"foo": "bar", "baz": "zap", "qux": "quux"} |
48 void mapAddAll(Map destination, Map source) => | 48 void mapAddAll(Map destination, Map source) => |
49 source.forEach((key, value) => destination[key] = value); | 49 source.forEach((key, value) => destination[key] = value); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 133 |
134 /// Buffers all input from an InputStream and returns it as a future. | 134 /// Buffers all input from an InputStream and returns it as a future. |
135 Future<List<int>> consumeInputStream(InputStream stream) { | 135 Future<List<int>> consumeInputStream(InputStream stream) { |
136 if (stream.closed) return new Future<List<int>>.immediate(<int>[]); | 136 if (stream.closed) return new Future<List<int>>.immediate(<int>[]); |
137 | 137 |
138 var completer = new Completer<List<int>>(); | 138 var completer = new Completer<List<int>>(); |
139 /// TODO(nweiz): use BufferList when issue 6409 is fixed | 139 /// TODO(nweiz): use BufferList when issue 6409 is fixed |
140 var buffer = <int>[]; | 140 var buffer = <int>[]; |
141 stream.onClosed = () => completer.complete(buffer); | 141 stream.onClosed = () => completer.complete(buffer); |
142 stream.onData = () => buffer.addAll(stream.read()); | 142 stream.onData = () => buffer.addAll(stream.read()); |
143 stream.onError = completer.completeException; | 143 stream.onError = completer.completeError; |
144 return completer.future; | 144 return completer.future; |
145 } | 145 } |
146 | 146 |
147 /// Takes all input from [source] and writes it to [sink], then closes [sink]. | 147 /// Takes all input from [source] and writes it to [sink], then closes [sink]. |
148 /// Returns a [Future] that completes when [source] is exhausted. | 148 /// Returns a [Future] that completes when [source] is exhausted. |
149 void pipeInputToInput(InputStream source, ListInputStream sink) { | 149 void pipeInputToInput(InputStream source, ListInputStream sink) { |
150 source.onClosed = sink.markEndOfStream; | 150 source.onClosed = sink.markEndOfStream; |
151 source.onData = () => sink.write(source.read()); | 151 source.onData = () => sink.write(source.read()); |
152 // TODO(nweiz): propagate source errors to the sink. See issue 3657. | 152 // TODO(nweiz): propagate source errors to the sink. See issue 3657. |
153 // TODO(nweiz): we need to use async here to avoid issue 4974. | 153 // TODO(nweiz): we need to use async here to avoid issue 4974. |
(...skipping 22 matching lines...) Expand all Loading... |
176 | 176 |
177 // TOOD(nweiz): Get rid of this once https://codereview.chromium.org/11293132/ | 177 // TOOD(nweiz): Get rid of this once https://codereview.chromium.org/11293132/ |
178 // is in. | 178 // is in. |
179 /// Runs [fn] for each element in [input] in order, moving to the next element | 179 /// Runs [fn] for each element in [input] in order, moving to the next element |
180 /// only when the [Future] returned by [fn] completes. Returns a [Future] that | 180 /// only when the [Future] returned by [fn] completes. Returns a [Future] that |
181 /// completes when all elements have been processed. | 181 /// completes when all elements have been processed. |
182 /// | 182 /// |
183 /// The return values of all [Future]s are discarded. Any errors will cause the | 183 /// The return values of all [Future]s are discarded. Any errors will cause the |
184 /// iteration to stop and will be piped through the return value. | 184 /// iteration to stop and will be piped through the return value. |
185 Future forEachFuture(Iterable input, Future fn(element)) { | 185 Future forEachFuture(Iterable input, Future fn(element)) { |
186 var iterator = input.iterator(); | 186 var iterator = input.iterator; |
187 Future nextElement(_) { | 187 Future nextElement(_) { |
188 if (!iterator.hasNext) return new Future.immediate(null); | 188 if (!iterator.moveNext()) return new Future.immediate(null); |
189 return fn(iterator.next()).chain(nextElement); | 189 return fn(iterator.current).then(nextElement); |
190 } | 190 } |
191 return nextElement(null); | 191 return nextElement(null); |
192 } | 192 } |
OLD | NEW |