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:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 | 105 |
106 /// Returns whether [string] is composed entirely of ASCII-compatible | 106 /// Returns whether [string] is composed entirely of ASCII-compatible |
107 /// characters. | 107 /// characters. |
108 bool isPlainAscii(String string) => _ASCII_ONLY.hasMatch(string); | 108 bool isPlainAscii(String string) => _ASCII_ONLY.hasMatch(string); |
109 | 109 |
110 /// Converts [input] into a [Uint8List]. | 110 /// Converts [input] into a [Uint8List]. |
111 /// | 111 /// |
112 /// If [input] is a [TypedData], this just returns a view on [input]. | 112 /// If [input] is a [TypedData], this just returns a view on [input]. |
113 Uint8List toUint8List(List<int> input) { | 113 Uint8List toUint8List(List<int> input) { |
114 if (input is Uint8List) return input; | 114 if (input is Uint8List) return input; |
115 if (input is TypedData) return new Uint8List.view(input.buffer); | 115 if (input is TypedData) { |
| 116 // TODO(nweiz): remove "as" when issue 11080 is fixed. |
| 117 return new Uint8List.view((input as TypedData).buffer); |
| 118 } |
116 return new Uint8List.fromList(input); | 119 return new Uint8List.fromList(input); |
117 } | 120 } |
118 | 121 |
119 /// If [stream] is already a [ByteStream], returns it. Otherwise, wraps it in a | 122 /// If [stream] is already a [ByteStream], returns it. Otherwise, wraps it in a |
120 /// [ByteStream]. | 123 /// [ByteStream]. |
121 ByteStream toByteStream(Stream<List<int>> stream) { | 124 ByteStream toByteStream(Stream<List<int>> stream) { |
122 if (stream is ByteStream) return stream; | 125 if (stream is ByteStream) return stream; |
123 return new ByteStream(stream); | 126 return new ByteStream(stream); |
124 } | 127 } |
125 | 128 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 /// The return values of all [Future]s are discarded. Any errors will cause the | 229 /// The return values of all [Future]s are discarded. Any errors will cause the |
227 /// iteration to stop and will be piped through the return value. | 230 /// iteration to stop and will be piped through the return value. |
228 Future forEachFuture(Iterable input, Future fn(element)) { | 231 Future forEachFuture(Iterable input, Future fn(element)) { |
229 var iterator = input.iterator; | 232 var iterator = input.iterator; |
230 Future nextElement(_) { | 233 Future nextElement(_) { |
231 if (!iterator.moveNext()) return new Future.value(); | 234 if (!iterator.moveNext()) return new Future.value(); |
232 return fn(iterator.current).then(nextElement); | 235 return fn(iterator.current).then(nextElement); |
233 } | 236 } |
234 return nextElement(null); | 237 return nextElement(null); |
235 } | 238 } |
OLD | NEW |