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 = encodingFromName(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 = encodingFromName(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]. | 85 /// Converts [bytes] into a [String] according to [encoding]. |
86 String decodeString(List<int> bytes, Encoding encoding) { | 86 String decodeString(List<int> bytes, Encoding encoding) { |
nweiz
2013/08/23 19:40:00
This (and [encodeString] should just be replaced w
floitsch
2013/08/26 09:33:40
Done.
| |
87 // TODO(nweiz): implement this once issue 6284 is fixed. | 87 return encoding.decode(bytes); |
88 return decodeUtf8(bytes); | |
89 } | 88 } |
90 | 89 |
91 /// Converts [string] into a byte array according to [encoding]. | 90 /// Converts [string] into a byte array according to [encoding]. |
92 List<int> encodeString(String string, Encoding encoding) { | 91 List<int> encodeString(String string, Encoding encoding) { |
93 // TODO(nweiz): implement this once issue 6284 is fixed. | 92 return encoding.encode(string); |
94 return encodeUtf8(string); | |
95 } | 93 } |
96 | 94 |
97 /// A regular expression that matches strings that are composed entirely of | 95 /// A regular expression that matches strings that are composed entirely of |
98 /// ASCII-compatible characters. | 96 /// ASCII-compatible characters. |
99 final RegExp _ASCII_ONLY = new RegExp(r"^[\x00-\x7F]+$"); | 97 final RegExp _ASCII_ONLY = new RegExp(r"^[\x00-\x7F]+$"); |
100 | 98 |
101 /// Returns whether [string] is composed entirely of ASCII-compatible | 99 /// Returns whether [string] is composed entirely of ASCII-compatible |
102 /// characters. | 100 /// characters. |
103 bool isPlainAscii(String string) => _ASCII_ONLY.hasMatch(string); | 101 bool isPlainAscii(String string) => _ASCII_ONLY.hasMatch(string); |
104 | 102 |
(...skipping 120 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 | 223 /// 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. | 224 /// iteration to stop and will be piped through the return value. |
227 Future forEachFuture(Iterable input, Future fn(element)) { | 225 Future forEachFuture(Iterable input, Future fn(element)) { |
228 var iterator = input.iterator; | 226 var iterator = input.iterator; |
229 Future nextElement(_) { | 227 Future nextElement(_) { |
230 if (!iterator.moveNext()) return new Future.value(); | 228 if (!iterator.moveNext()) return new Future.value(); |
231 return fn(iterator.current).then(nextElement); | 229 return fn(iterator.current).then(nextElement); |
232 } | 230 } |
233 return nextElement(null); | 231 return nextElement(null); |
234 } | 232 } |
OLD | NEW |