| 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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 return completer.future; | 111 return completer.future; |
| 112 } | 112 } |
| 113 | 113 |
| 114 /// Configures [future] so that its result (success or exception) is passed on | 114 /// Configures [future] so that its result (success or exception) is passed on |
| 115 /// to [completer]. | 115 /// to [completer]. |
| 116 void chainToCompleter(Future future, Completer completer) { | 116 void chainToCompleter(Future future, Completer completer) { |
| 117 future.then((value) => completer.complete(value), | 117 future.then((value) => completer.complete(value), |
| 118 onError: (e) => completer.completeError(e.error, e.stackTrace)); | 118 onError: (e) => completer.completeError(e.error, e.stackTrace)); |
| 119 } | 119 } |
| 120 | 120 |
| 121 // TODO(nweiz): remove this when issue 7964 is fixed. | |
| 122 /// Returns a [Future] that will complete to the first element of [stream]. | |
| 123 /// Unlike [Stream.first], this is safe to use with single-subscription streams. | |
| 124 Future streamFirst(Stream stream) { | |
| 125 var completer = new Completer(); | |
| 126 var subscription; | |
| 127 subscription = stream.listen((value) { | |
| 128 subscription.cancel(); | |
| 129 completer.complete(value); | |
| 130 }, | |
| 131 onError: (e) => completer.completeError(e.error, e.stackTrace), | |
| 132 onDone: () => completer.completeError(new StateError("No elements")), | |
| 133 unsubscribeOnError: true); | |
| 134 return completer.future; | |
| 135 } | |
| 136 | |
| 137 /// Returns a wrapped version of [stream] along with a [StreamSubscription] that | |
| 138 /// can be used to control the wrapped stream. | |
| 139 Pair<Stream, StreamSubscription> streamWithSubscription(Stream stream) { | |
| 140 var controller = stream.isSingleSubscription ? | |
| 141 new StreamController() : | |
| 142 new StreamController.multiSubscription(); | |
| 143 var subscription = stream.listen(controller.add, | |
| 144 onError: controller.signalError, | |
| 145 onDone: controller.close); | |
| 146 return new Pair<Stream, StreamSubscription>(controller.stream, subscription); | |
| 147 } | |
| 148 | |
| 149 // TODO(nweiz): remove this when issue 7787 is fixed. | |
| 150 /// Creates two single-subscription [Stream]s that each emit all values and | |
| 151 /// errors from [stream]. This is useful if [stream] is single-subscription but | |
| 152 /// multiple subscribers are necessary. | |
| 153 Pair<Stream, Stream> tee(Stream stream) { | |
| 154 var controller1 = new StreamController(); | |
| 155 var controller2 = new StreamController(); | |
| 156 stream.listen((value) { | |
| 157 controller1.add(value); | |
| 158 controller2.add(value); | |
| 159 }, onError: (error) { | |
| 160 controller1.signalError(error); | |
| 161 controller2.signalError(error); | |
| 162 }, onDone: () { | |
| 163 controller1.close(); | |
| 164 controller2.close(); | |
| 165 }); | |
| 166 return new Pair<Stream, Stream>(controller1.stream, controller2.stream); | |
| 167 } | |
| 168 | |
| 169 /// A regular expression matching a line termination character or character | |
| 170 /// sequence. | |
| 171 final RegExp _lineRegexp = new RegExp(r"\r\n|\r|\n"); | |
| 172 | |
| 173 /// Converts a stream of arbitrarily chunked strings into a line-by-line stream. | |
| 174 /// The lines don't include line termination characters. A single trailing | |
| 175 /// newline is ignored. | |
| 176 Stream<String> streamToLines(Stream<String> stream) { | |
| 177 var buffer = new StringBuffer(); | |
| 178 return stream.transform(new StreamTransformer.from( | |
| 179 onData: (chunk, sink) { | |
| 180 var lines = chunk.split(_lineRegexp); | |
| 181 var leftover = lines.removeLast(); | |
| 182 for (var line in lines) { | |
| 183 if (!buffer.isEmpty) { | |
| 184 buffer.add(line); | |
| 185 line = buffer.toString(); | |
| 186 buffer.clear(); | |
| 187 } | |
| 188 | |
| 189 sink.add(line); | |
| 190 } | |
| 191 buffer.add(leftover); | |
| 192 }, onDone: (sink) { | |
| 193 if (!buffer.isEmpty) sink.add(buffer.toString()); | |
| 194 sink.close(); | |
| 195 })); | |
| 196 } | |
| 197 | |
| 198 // TODO(nweiz): unify the following functions with the utility functions in | 121 // TODO(nweiz): unify the following functions with the utility functions in |
| 199 // pkg/http. | 122 // pkg/http. |
| 200 | 123 |
| 201 /// Like [String.split], but only splits on the first occurrence of the pattern. | 124 /// Like [String.split], but only splits on the first occurrence of the pattern. |
| 202 /// This will always return an array of two elements or fewer. | 125 /// This will always return an array of two elements or fewer. |
| 203 List<String> split1(String toSplit, String pattern) { | 126 List<String> split1(String toSplit, String pattern) { |
| 204 if (toSplit.isEmpty) return <String>[]; | 127 if (toSplit.isEmpty) return <String>[]; |
| 205 | 128 |
| 206 var index = toSplit.indexOf(pattern); | 129 var index = toSplit.indexOf(pattern); |
| 207 if (index == -1) return [toSplit]; | 130 if (index == -1) return [toSplit]; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 | 170 |
| 248 /// Add all key/value pairs from [source] to [destination], overwriting any | 171 /// Add all key/value pairs from [source] to [destination], overwriting any |
| 249 /// pre-existing values. | 172 /// pre-existing values. |
| 250 void mapAddAll(Map destination, Map source) => | 173 void mapAddAll(Map destination, Map source) => |
| 251 source.forEach((key, value) => destination[key] = value); | 174 source.forEach((key, value) => destination[key] = value); |
| 252 | 175 |
| 253 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes | 176 /// Decodes a URL-encoded string. Unlike [decodeUriComponent], this includes |
| 254 /// replacing `+` with ` `. | 177 /// replacing `+` with ` `. |
| 255 String urlDecode(String encoded) => | 178 String urlDecode(String encoded) => |
| 256 decodeUriComponent(encoded.replaceAll("+", " ")); | 179 decodeUriComponent(encoded.replaceAll("+", " ")); |
| OLD | NEW |