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:io'; | 8 import 'dart:io'; |
9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
10 import 'dart:utf'; | 10 import 'dart:utf'; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 } | 106 } |
107 | 107 |
108 /// A regular expression that matches strings that are composed entirely of | 108 /// A regular expression that matches strings that are composed entirely of |
109 /// ASCII-compatible characters. | 109 /// ASCII-compatible characters. |
110 final RegExp _ASCII_ONLY = new RegExp(r"^[\x00-\x7F]+$"); | 110 final RegExp _ASCII_ONLY = new RegExp(r"^[\x00-\x7F]+$"); |
111 | 111 |
112 /// Returns whether [string] is composed entirely of ASCII-compatible | 112 /// Returns whether [string] is composed entirely of ASCII-compatible |
113 /// characters. | 113 /// characters. |
114 bool isPlainAscii(String string) => _ASCII_ONLY.hasMatch(string); | 114 bool isPlainAscii(String string) => _ASCII_ONLY.hasMatch(string); |
115 | 115 |
116 /// Converts [input] into a [Uint8List]. If [input] is a [ByteArray] or | 116 /// Converts [input] into a [Uint8List]. If [input] is a [TypedData], this just |
117 /// [ByteArrayViewable], this just returns a view on [input]. | 117 /// returns a view on [input]. |
118 Uint8List toUint8List(List<int> input) { | 118 Uint8List toUint8List(List<int> input) { |
119 if (input is Uint8List) return input; | 119 if (input is Uint8List) return input; |
120 if (input is ByteData) return new Uint8List.view(input.buffer); | 120 if (input is TypedData) { |
| 121 // TODO(nweiz): remove this "as" check when issue 11080 is fixed. |
| 122 return new Uint8List.view((input as TypedData).buffer); |
| 123 } |
121 var output = new Uint8List(input.length); | 124 var output = new Uint8List(input.length); |
122 output.setRange(0, input.length, input); | 125 output.setRange(0, input.length, input); |
123 return output; | 126 return output; |
124 } | 127 } |
125 | 128 |
126 /// If [stream] is already a [ByteStream], returns it. Otherwise, wraps it in a | 129 /// If [stream] is already a [ByteStream], returns it. Otherwise, wraps it in a |
127 /// [ByteStream]. | 130 /// [ByteStream]. |
128 ByteStream toByteStream(Stream<List<int>> stream) { | 131 ByteStream toByteStream(Stream<List<int>> stream) { |
129 if (stream is ByteStream) return stream; | 132 if (stream is ByteStream) return stream; |
130 return new ByteStream(stream); | 133 return new ByteStream(stream); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 /// The return values of all [Future]s are discarded. Any errors will cause the | 236 /// The return values of all [Future]s are discarded. Any errors will cause the |
234 /// iteration to stop and will be piped through the return value. | 237 /// iteration to stop and will be piped through the return value. |
235 Future forEachFuture(Iterable input, Future fn(element)) { | 238 Future forEachFuture(Iterable input, Future fn(element)) { |
236 var iterator = input.iterator; | 239 var iterator = input.iterator; |
237 Future nextElement(_) { | 240 Future nextElement(_) { |
238 if (!iterator.moveNext()) return new Future.value(); | 241 if (!iterator.moveNext()) return new Future.value(); |
239 return fn(iterator.current).then(nextElement); | 242 return fn(iterator.current).then(nextElement); |
240 } | 243 } |
241 return nextElement(null); | 244 return nextElement(null); |
242 } | 245 } |
OLD | NEW |