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:crypto'; | 8 import 'dart:crypto'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:scalarlist'; | 10 import 'dart:scalarlist'; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 toSplit.substring(index + pattern.length) | 71 toSplit.substring(index + pattern.length) |
72 ]; | 72 ]; |
73 } | 73 } |
74 | 74 |
75 /// Returns the [Encoding] that corresponds to [charset]. Returns [fallback] if | 75 /// Returns the [Encoding] that corresponds to [charset]. Returns [fallback] if |
76 /// [charset] is null or if no [Encoding] was found that corresponds to | 76 /// [charset] is null or if no [Encoding] was found that corresponds to |
77 /// [charset]. | 77 /// [charset]. |
78 Encoding encodingForCharset( | 78 Encoding encodingForCharset( |
79 String charset, [Encoding fallback = Encoding.ISO_8859_1]) { | 79 String charset, [Encoding fallback = Encoding.ISO_8859_1]) { |
80 if (charset == null) return fallback; | 80 if (charset == null) return fallback; |
81 var encoding = _encodingForCharset(charset); | 81 var encoding = Encoding.fromName(charset); |
82 return encoding == null ? fallback : encoding; | 82 return encoding == null ? fallback : encoding; |
83 } | 83 } |
84 | 84 |
85 /// Returns the [Encoding] that corresponds to [charset]. Throws a | |
86 /// [FormatException] if no [Encoding] was found that corresponds to [charset]. | |
87 /// [charset] may not be null. | |
88 Encoding requiredEncodingForCharset(String charset) { | |
nweiz
2013/03/07 19:32:05
Now I'm concerned that there are cases where [requ
Søren Gjesse
2013/03/08 09:47:46
Done.
| |
89 var encoding = _encodingForCharset(charset); | |
90 if (encoding != null) return encoding; | |
91 throw new FormatException('Unsupported encoding "$charset".'); | |
92 } | |
93 | |
94 /// Returns the [Encoding] that corresponds to [charset]. Returns null if no | |
95 /// [Encoding] was found that corresponds to [charset]. [charset] may not be | |
96 /// null. | |
97 Encoding _encodingForCharset(String charset) { | |
98 charset = charset.toLowerCase(); | |
99 if (charset == 'ascii' || charset == 'us-ascii') return Encoding.ASCII; | |
100 if (charset == 'utf-8') return Encoding.UTF_8; | |
101 if (charset == 'iso-8859-1') return Encoding.ISO_8859_1; | |
102 return null; | |
103 } | |
104 | |
105 /// Converts [bytes] into a [String] according to [encoding]. | 85 /// Converts [bytes] into a [String] according to [encoding]. |
106 String decodeString(List<int> bytes, Encoding encoding) { | 86 String decodeString(List<int> bytes, Encoding encoding) { |
107 // TODO(nweiz): implement this once issue 6284 is fixed. | 87 // TODO(nweiz): implement this once issue 6284 is fixed. |
108 return new String.fromCharCodes(bytes); | 88 return new String.fromCharCodes(bytes); |
109 } | 89 } |
110 | 90 |
111 /// Converts [string] into a byte array according to [encoding]. | 91 /// Converts [string] into a byte array according to [encoding]. |
112 List<int> encodeString(String string, Encoding encoding) { | 92 List<int> encodeString(String string, Encoding encoding) { |
113 // TODO(nweiz): implement this once issue 6284 is fixed. | 93 // TODO(nweiz): implement this once issue 6284 is fixed. |
114 return string.codeUnits; | 94 return string.codeUnits; |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 /// The return values of all [Future]s are discarded. Any errors will cause the | 223 /// The return values of all [Future]s are discarded. Any errors will cause the |
244 /// iteration to stop and will be piped through the return value. | 224 /// iteration to stop and will be piped through the return value. |
245 Future forEachFuture(Iterable input, Future fn(element)) { | 225 Future forEachFuture(Iterable input, Future fn(element)) { |
246 var iterator = input.iterator; | 226 var iterator = input.iterator; |
247 Future nextElement(_) { | 227 Future nextElement(_) { |
248 if (!iterator.moveNext()) return new Future.immediate(null); | 228 if (!iterator.moveNext()) return new Future.immediate(null); |
249 return fn(iterator.current).then(nextElement); | 229 return fn(iterator.current).then(nextElement); |
250 } | 230 } |
251 return nextElement(null); | 231 return nextElement(null); |
252 } | 232 } |
OLD | NEW |