| 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:crypto'; |
| 7 import 'dart:io'; | 8 import 'dart:io'; |
| 8 import 'dart:isolate'; | 9 import 'dart:isolate'; |
| 9 import 'dart:scalarlist'; | 10 import 'dart:scalarlist'; |
| 10 import 'dart:uri'; | 11 import 'dart:uri'; |
| 12 import 'dart:utf'; |
| 11 | 13 |
| 12 /// 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) |
| 13 /// into a [Map] from parameter names to values. | 15 /// into a [Map] from parameter names to values. |
| 14 /// | 16 /// |
| 15 /// queryToMap("foo=bar&baz=bang&qux"); | 17 /// queryToMap("foo=bar&baz=bang&qux"); |
| 16 /// //=> {"foo": "bar", "baz": "bang", "qux": ""} | 18 /// //=> {"foo": "bar", "baz": "bang", "qux": ""} |
| 17 Map<String, String> queryToMap(String queryList) { | 19 Map<String, String> queryToMap(String queryList) { |
| 18 var map = <String>{}; | 20 var map = <String>{}; |
| 19 for (var pair in queryList.split("&")) { | 21 for (var pair in queryList.split("&")) { |
| 20 var split = split1(pair, "="); | 22 var split = split1(pair, "="); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 if (toSplit.isEmpty) return <String>[]; | 63 if (toSplit.isEmpty) return <String>[]; |
| 62 | 64 |
| 63 var index = toSplit.indexOf(pattern); | 65 var index = toSplit.indexOf(pattern); |
| 64 if (index == -1) return [toSplit]; | 66 if (index == -1) return [toSplit]; |
| 65 return [ | 67 return [ |
| 66 toSplit.substring(0, index), | 68 toSplit.substring(0, index), |
| 67 toSplit.substring(index + pattern.length) | 69 toSplit.substring(index + pattern.length) |
| 68 ]; | 70 ]; |
| 69 } | 71 } |
| 70 | 72 |
| 71 /// Returns the [Encoding] that corresponds to [charset]. Returns | 73 /// Returns the [Encoding] that corresponds to [charset]. Returns [fallback] if |
| 72 /// [Encoding.ISO_8859_1] if [charset] is null or if no [Encoding] was found | 74 /// [charset] is null or if no [Encoding] was found that corresponds to |
| 73 /// that corresponds to [charset]. | 75 /// [charset]. |
| 74 Encoding encodingForCharset(String charset) { | 76 Encoding encodingForCharset( |
| 75 if (charset == null) return Encoding.ISO_8859_1; | 77 String charset, [Encoding fallback = Encoding.ISO_8859_1]) { |
| 78 if (charset == null) return fallback; |
| 76 var encoding = _encodingForCharset(charset); | 79 var encoding = _encodingForCharset(charset); |
| 77 return encoding == null ? Encoding.ISO_8859_1 : encoding; | 80 return encoding == null ? fallback : encoding; |
| 78 } | 81 } |
| 79 | 82 |
| 80 /// Returns the [Encoding] that corresponds to [charset]. Throws a [FormatExcept
ion] | 83 /// Returns the [Encoding] that corresponds to [charset]. Throws a |
| 81 /// if no [Encoding] was found that corresponds to [charset]. [charset] may not | 84 /// [FormatException] if no [Encoding] was found that corresponds to [charset]. |
| 82 /// be null. | 85 /// [charset] may not be null. |
| 83 Encoding requiredEncodingForCharset(String charset) { | 86 Encoding requiredEncodingForCharset(String charset) { |
| 84 var encoding = _encodingForCharset(charset); | 87 var encoding = _encodingForCharset(charset); |
| 85 if (encoding != null) return encoding; | 88 if (encoding != null) return encoding; |
| 86 throw new FormatException('Unsupported encoding "$charset".'); | 89 throw new FormatException('Unsupported encoding "$charset".'); |
| 87 } | 90 } |
| 88 | 91 |
| 89 /// Returns the [Encoding] that corresponds to [charset]. Returns null if no | 92 /// Returns the [Encoding] that corresponds to [charset]. Returns null if no |
| 90 /// [Encoding] was found that corresponds to [charset]. [charset] may not be | 93 /// [Encoding] was found that corresponds to [charset]. [charset] may not be |
| 91 /// null. | 94 /// null. |
| 92 Encoding _encodingForCharset(String charset) { | 95 Encoding _encodingForCharset(String charset) { |
| 93 charset = charset.toLowerCase(); | 96 charset = charset.toLowerCase(); |
| 94 if (charset == 'ascii' || charset == 'us-ascii') return Encoding.ASCII; | 97 if (charset == 'ascii' || charset == 'us-ascii') return Encoding.ASCII; |
| 95 if (charset == 'utf-8') return Encoding.UTF_8; | 98 if (charset == 'utf-8') return Encoding.UTF_8; |
| 96 if (charset == 'iso-8859-1') return Encoding.ISO_8859_1; | 99 if (charset == 'iso-8859-1') return Encoding.ISO_8859_1; |
| 97 return null; | 100 return null; |
| 98 } | 101 } |
| 99 | 102 |
| 100 /// Converts [bytes] into a [String] according to [encoding]. | 103 /// Converts [bytes] into a [String] according to [encoding]. |
| 101 String decodeString(List<int> bytes, Encoding encoding) { | 104 String decodeString(List<int> bytes, Encoding encoding) { |
| 102 // TODO(nweiz): implement this once issue 6284 is fixed. | 105 // TODO(nweiz): implement this once issue 6284 is fixed. |
| 103 return new String.fromCharCodes(bytes); | 106 return new String.fromCharCodes(bytes); |
| 104 } | 107 } |
| 105 | 108 |
| 106 /// Converts [string] into a byte array according to [encoding]. | 109 /// Converts [string] into a byte array according to [encoding]. |
| 107 List<int> encodeString(String string, Encoding encoding) { | 110 List<int> encodeString(String string, Encoding encoding) { |
| 108 // TODO(nweiz): implement this once issue 6284 is fixed. | 111 // TODO(nweiz): implement this once issue 6284 is fixed. |
| 109 return string.charCodes; | 112 return string.charCodes; |
| 110 } | 113 } |
| 111 | 114 |
| 115 /// A regular expression that matches strings that are composed entirely of |
| 116 /// ASCII-compatible characters. |
| 117 final RegExp _ASCII_ONLY = const RegExp(r"^[\x00-\x7F]+$"); |
| 118 |
| 119 /// Returns whether [string] is composed entirely of ASCII-compatible |
| 120 /// characters. |
| 121 bool isPlainAscii(String string) => _ASCII_ONLY.hasMatch(string); |
| 122 |
| 112 /// Converts [input] into a [Uint8List]. If [input] is a [ByteArray] or | 123 /// Converts [input] into a [Uint8List]. If [input] is a [ByteArray] or |
| 113 /// [ByteArrayViewable], this just returns a view on [input]. | 124 /// [ByteArrayViewable], this just returns a view on [input]. |
| 114 Uint8List toUint8List(List<int> input) { | 125 Uint8List toUint8List(List<int> input) { |
| 115 if (input is Uint8List) return input; | 126 if (input is Uint8List) return input; |
| 116 if (input is ByteArrayViewable) input = input.asByteArray(); | 127 if (input is ByteArrayViewable) input = input.asByteArray(); |
| 117 if (input is ByteArray) return new Uint8List.view(input); | 128 if (input is ByteArray) return new Uint8List.view(input); |
| 118 var output = new Uint8List(input.length); | 129 var output = new Uint8List(input.length); |
| 119 output.setRange(0, input.length, input); | 130 output.setRange(0, input.length, input); |
| 120 return output; | 131 return output; |
| 121 } | 132 } |
| 122 | 133 |
| 123 /// Buffers all input from an InputStream and returns it as a future. | 134 /// Buffers all input from an InputStream and returns it as a future. |
| 124 Future<List<int>> consumeInputStream(InputStream stream) { | 135 Future<List<int>> consumeInputStream(InputStream stream) { |
| 125 var completer = new Completer<List<int>>(); | 136 var completer = new Completer<List<int>>(); |
| 126 /// TODO(nweiz): use BufferList when issue 6409 is fixed | 137 /// TODO(nweiz): use BufferList when issue 6409 is fixed |
| 127 var buffer = <int>[]; | 138 var buffer = <int>[]; |
| 128 stream.onClosed = () => completer.complete(buffer); | 139 stream.onClosed = () => completer.complete(buffer); |
| 129 stream.onData = () => buffer.addAll(stream.read()); | 140 stream.onData = () => buffer.addAll(stream.read()); |
| 130 stream.onError = completer.completeException; | 141 stream.onError = completer.completeException; |
| 131 return completer.future; | 142 return completer.future; |
| 132 } | 143 } |
| 133 | 144 |
| 134 /// Takes all input from [source] and writes it to [sink]. | 145 /// Takes all input from [source] and writes it to [sink], then closes [sink]. |
| 135 void pipeInputToInput(InputStream source, ListInputStream sink) { | 146 void pipeInputToInput(InputStream source, ListInputStream sink) { |
| 136 source.onClosed = () => sink.markEndOfStream(); | 147 source.onClosed = () => sink.markEndOfStream(); |
| 137 source.onData = () => sink.write(source.read()); | 148 source.onData = () => sink.write(source.read()); |
| 138 // TODO(nweiz): propagate source errors to the sink. See issue 3657. | 149 // TODO(nweiz): propagate source errors to the sink. See issue 3657. |
| 139 } | 150 } |
| 140 | 151 |
| 152 /// Takes all input from [source] and writes it to [sink], but does not close |
| 153 /// [sink] when [source] is closed. Returns a [Future] that completes when |
| 154 /// [source] is closed. |
| 155 Future writeInputToInput(InputStream source, ListInputStream sink) { |
| 156 var completer = new Completer(); |
| 157 source.onClosed = () => completer.complete(null); |
| 158 source.onData = () => sink.write(source.read()); |
| 159 // TODO(nweiz): propagate source errors to the sink. See issue 3657. |
| 160 return completer.future; |
| 161 } |
| 162 |
| 141 /// Returns a [Future] that asynchronously completes to `null`. | 163 /// Returns a [Future] that asynchronously completes to `null`. |
| 142 Future get async { | 164 Future get async { |
| 143 var completer = new Completer(); | 165 var completer = new Completer(); |
| 144 new Timer(0, (_) => completer.complete(null)); | 166 new Timer(0, (_) => completer.complete(null)); |
| 145 return completer.future; | 167 return completer.future; |
| 146 } | 168 } |
| 169 |
| 170 // TOOD(nweiz): Get rid of this once https://codereview.chromium.org/11293132/ |
| 171 // is in. |
| 172 /// Runs [fn] for each element in [input] in order, moving to the next element |
| 173 /// only when the [Future] returned by [fn] completes. Returns a [Future] that |
| 174 /// completes when all elements have been processed. |
| 175 /// |
| 176 /// The return values of all [Future]s are discarded. Any errors will cause the |
| 177 /// iteration to stop and will be piped through the return value. |
| 178 Future forEachFuture(Iterable input, Future fn(element)) { |
| 179 var iterator = input.iterator(); |
| 180 Future nextElement(_) { |
| 181 if (!iterator.hasNext) return new Future.immediate(null); |
| 182 return fn(iterator.next()).chain(nextElement); |
| 183 } |
| 184 return nextElement(null); |
| 185 } |
| OLD | NEW |