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:io'; | 9 import 'dart:io'; |
9 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
10 import 'dart:utf'; | |
11 | 11 |
12 import 'byte_stream.dart'; | 12 import 'byte_stream.dart'; |
13 | 13 |
14 /// Converts a URL query string (or `application/x-www-form-urlencoded` body) | 14 /// Converts a URL query string (or `application/x-www-form-urlencoded` body) |
15 /// into a [Map] from parameter names to values. | 15 /// into a [Map] from parameter names to values. |
16 /// | 16 /// |
17 /// queryToMap("foo=bar&baz=bang&qux"); | 17 /// queryToMap("foo=bar&baz=bang&qux"); |
18 /// //=> {"foo": "bar", "baz": "bang", "qux": ""} | 18 /// //=> {"foo": "bar", "baz": "bang", "qux": ""} |
19 Map<String, String> queryToMap(String queryList) { | 19 Map<String, String> queryToMap(String queryList) { |
20 var map = {}; | 20 var map = {}; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 return [ | 59 return [ |
60 toSplit.substring(0, index), | 60 toSplit.substring(0, index), |
61 toSplit.substring(index + pattern.length) | 61 toSplit.substring(index + pattern.length) |
62 ]; | 62 ]; |
63 } | 63 } |
64 | 64 |
65 /// Returns the [Encoding] that corresponds to [charset]. Returns [fallback] if | 65 /// Returns the [Encoding] that corresponds to [charset]. Returns [fallback] if |
66 /// [charset] is null or if no [Encoding] was found that corresponds to | 66 /// [charset] is null or if no [Encoding] was found that corresponds to |
67 /// [charset]. | 67 /// [charset]. |
68 Encoding encodingForCharset( | 68 Encoding encodingForCharset( |
69 String charset, [Encoding fallback = Encoding.ISO_8859_1]) { | 69 String charset, [Encoding fallback = LATIN1]) { |
70 if (charset == null) return fallback; | 70 if (charset == null) return fallback; |
71 var encoding = Encoding.fromName(charset); | 71 var encoding = Encoding.getByName(charset); |
72 return encoding == null ? fallback : encoding; | 72 return encoding == null ? fallback : encoding; |
73 } | 73 } |
74 | 74 |
75 | 75 |
76 /// Returns the [Encoding] that corresponds to [charset]. Throws a | 76 /// Returns the [Encoding] that corresponds to [charset]. Throws a |
77 /// [FormatException] if no [Encoding] was found that corresponds to [charset]. | 77 /// [FormatException] if no [Encoding] was found that corresponds to [charset]. |
78 /// [charset] may not be null. | 78 /// [charset] may not be null. |
79 Encoding requiredEncodingForCharset(String charset) { | 79 Encoding requiredEncodingForCharset(String charset) { |
80 var encoding = Encoding.fromName(charset); | 80 var encoding = Encoding.getByName(charset); |
81 if (encoding != null) return encoding; | 81 if (encoding != null) return encoding; |
82 throw new FormatException('Unsupported encoding "$charset".'); | 82 throw new FormatException('Unsupported encoding "$charset".'); |
83 } | 83 } |
84 | 84 |
85 /// Converts [bytes] into a [String] according to [encoding]. | |
86 String decodeString(List<int> bytes, Encoding encoding) { | |
87 // TODO(nweiz): implement this once issue 6284 is fixed. | |
88 return decodeUtf8(bytes); | |
89 } | |
90 | |
91 /// Converts [string] into a byte array according to [encoding]. | |
92 List<int> encodeString(String string, Encoding encoding) { | |
93 // TODO(nweiz): implement this once issue 6284 is fixed. | |
94 return encodeUtf8(string); | |
95 } | |
96 | |
97 /// A regular expression that matches strings that are composed entirely of | 85 /// A regular expression that matches strings that are composed entirely of |
98 /// ASCII-compatible characters. | 86 /// ASCII-compatible characters. |
99 final RegExp _ASCII_ONLY = new RegExp(r"^[\x00-\x7F]+$"); | 87 final RegExp _ASCII_ONLY = new RegExp(r"^[\x00-\x7F]+$"); |
100 | 88 |
101 /// Returns whether [string] is composed entirely of ASCII-compatible | 89 /// Returns whether [string] is composed entirely of ASCII-compatible |
102 /// characters. | 90 /// characters. |
103 bool isPlainAscii(String string) => _ASCII_ONLY.hasMatch(string); | 91 bool isPlainAscii(String string) => _ASCII_ONLY.hasMatch(string); |
104 | 92 |
105 /// Converts [input] into a [Uint8List]. If [input] is a [TypedData], this just | 93 /// Converts [input] into a [Uint8List]. If [input] is a [TypedData], this just |
106 /// returns a view on [input]. | 94 /// returns a view on [input]. |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 /// The return values of all [Future]s are discarded. Any errors will cause the | 213 /// The return values of all [Future]s are discarded. Any errors will cause the |
226 /// iteration to stop and will be piped through the return value. | 214 /// iteration to stop and will be piped through the return value. |
227 Future forEachFuture(Iterable input, Future fn(element)) { | 215 Future forEachFuture(Iterable input, Future fn(element)) { |
228 var iterator = input.iterator; | 216 var iterator = input.iterator; |
229 Future nextElement(_) { | 217 Future nextElement(_) { |
230 if (!iterator.moveNext()) return new Future.value(); | 218 if (!iterator.moveNext()) return new Future.value(); |
231 return fn(iterator.current).then(nextElement); | 219 return fn(iterator.current).then(nextElement); |
232 } | 220 } |
233 return nextElement(null); | 221 return nextElement(null); |
234 } | 222 } |
OLD | NEW |