| 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 |
| 85 /// Returns the [Encoding] that corresponds to [charset]. Throws a | 86 /// Returns the [Encoding] that corresponds to [charset]. Throws a |
| 86 /// [FormatException] if no [Encoding] was found that corresponds to [charset]. | 87 /// [FormatException] if no [Encoding] was found that corresponds to [charset]. |
| 87 /// [charset] may not be null. | 88 /// [charset] may not be null. |
| 88 Encoding requiredEncodingForCharset(String charset) { | 89 Encoding requiredEncodingForCharset(String charset) { |
| 89 var encoding = _encodingForCharset(charset); | 90 var encoding = Encoding.fromName(charset); |
| 90 if (encoding != null) return encoding; | 91 if (encoding != null) return encoding; |
| 91 throw new FormatException('Unsupported encoding "$charset".'); | 92 throw new FormatException('Unsupported encoding "$charset".'); |
| 92 } | 93 } |
| 93 | 94 |
| 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]. | 95 /// Converts [bytes] into a [String] according to [encoding]. |
| 106 String decodeString(List<int> bytes, Encoding encoding) { | 96 String decodeString(List<int> bytes, Encoding encoding) { |
| 107 // TODO(nweiz): implement this once issue 6284 is fixed. | 97 // TODO(nweiz): implement this once issue 6284 is fixed. |
| 108 return new String.fromCharCodes(bytes); | 98 return new String.fromCharCodes(bytes); |
| 109 } | 99 } |
| 110 | 100 |
| 111 /// Converts [string] into a byte array according to [encoding]. | 101 /// Converts [string] into a byte array according to [encoding]. |
| 112 List<int> encodeString(String string, Encoding encoding) { | 102 List<int> encodeString(String string, Encoding encoding) { |
| 113 // TODO(nweiz): implement this once issue 6284 is fixed. | 103 // TODO(nweiz): implement this once issue 6284 is fixed. |
| 114 return string.codeUnits; | 104 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 | 233 /// 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. | 234 /// iteration to stop and will be piped through the return value. |
| 245 Future forEachFuture(Iterable input, Future fn(element)) { | 235 Future forEachFuture(Iterable input, Future fn(element)) { |
| 246 var iterator = input.iterator; | 236 var iterator = input.iterator; |
| 247 Future nextElement(_) { | 237 Future nextElement(_) { |
| 248 if (!iterator.moveNext()) return new Future.immediate(null); | 238 if (!iterator.moveNext()) return new Future.immediate(null); |
| 249 return fn(iterator.current).then(nextElement); | 239 return fn(iterator.current).then(nextElement); |
| 250 } | 240 } |
| 251 return nextElement(null); | 241 return nextElement(null); |
| 252 } | 242 } |
| OLD | NEW |