Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library utils; | |
| 6 | |
| 7 import 'dart:io'; | |
| 8 import 'dart:isolate'; | |
| 9 import 'dart:scalarlist'; | |
| 10 import 'dart:uri'; | |
| 11 | |
| 12 /// Convert a URL query string (or `application/x-www-form-urlencoded` body) | |
| 13 /// into a [Map] from parameter names to values. | |
|
Bob Nystrom
2012/10/31 01:17:44
Show an example here and elsewhere. In general, I'
nweiz
2012/10/31 18:20:59
Done.
| |
| 14 Map<String, String> queryToMap(String queryList) { | |
| 15 var map = <String>{}; | |
| 16 for (var pair in queryList.split("&")) { | |
| 17 var split = split1(pair, "="); | |
| 18 if (split.isEmpty) continue; | |
| 19 var key = urlDecode(split[0]); | |
| 20 var value = urlDecode(split.length > 1 ? split[1] : ""); | |
| 21 map[key] = value; | |
| 22 } | |
| 23 return map; | |
| 24 } | |
| 25 | |
| 26 /// Convert a [Map] from parameter names to values to a URL query string. | |
| 27 String mapToQuery(Map<String, String> map) { | |
| 28 var pairs = <List<String>>[]; | |
| 29 map.forEach((key, value) => | |
| 30 pairs.add([encodeUriComponent(key), encodeUriComponent(value)])); | |
|
Bob Nystrom
2012/10/31 01:17:44
Nit: Indent +2. If it were me, I would probably le
nweiz
2012/10/31 18:20:59
Done.
| |
| 31 return Strings.join(pairs.map((pair) => "${pair[0]}=${pair[1]}"), "&"); | |
| 32 } | |
| 33 | |
| 34 /// Add all key/value pairs from [source] to [destination], overwriting any | |
| 35 /// pre-existing values. | |
| 36 void mapAddAll(Map destination, Map source) => | |
| 37 source.forEach((key, value) => destination[key] = value); | |
|
Bob Nystrom
2012/10/31 01:17:44
+2 here and elsewhere.
nweiz
2012/10/31 18:20:59
As I mentioned elsewhere, I feel like this is more
| |
| 38 | |
| 39 /// Decode a URL-encoded string. Unlike [decodeUriComponent], this includes | |
| 40 /// replacing `+` with ` `. | |
| 41 String urlDecode(String encoded) => | |
| 42 decodeUriComponent(encoded.replaceAll("+", " ")); | |
| 43 | |
| 44 /// Like [String.split], but only splits on the first occurrence of the pattern. | |
| 45 /// This will always return an array of two elements or fewer. | |
| 46 List<String> split1(String toSplit, String pattern) { | |
| 47 if (toSplit.isEmpty) return <String>[]; | |
| 48 | |
| 49 var index = toSplit.indexOf(pattern); | |
| 50 if (index == -1) return [toSplit]; | |
| 51 return [toSplit.substring(0, index), | |
| 52 toSplit.substring(index + pattern.length)]; | |
|
Bob Nystrom
2012/10/31 01:17:44
+2.
nweiz
2012/10/31 18:20:59
Changed the formatting in a different way, let me
Bob Nystrom
2012/11/01 19:53:59
+1. I like it.
| |
| 53 } | |
| 54 | |
| 55 /// Return the [Encoding] that corresponds to [charset]. Return | |
| 56 /// [Encoding.ISO_8859_1] if [charset] is null or if no [Encoding] was found | |
| 57 /// that corresponds to [charset]. | |
| 58 Encoding encodingForCharset(String charset) { | |
| 59 if (charset == null) return Encoding.ISO_8859_1; | |
| 60 var encoding = _encodingForCharset(charset); | |
| 61 return encoding == null ? Encoding.ISO_8859_1 : encoding; | |
| 62 } | |
| 63 | |
| 64 /// Return the [Encoding] that corresponds to [charset]. Throw an | |
| 65 /// [UnsupportedError] if no [Encoding] was found that corresponds to [charset]. | |
| 66 /// [charset] may not be null. | |
| 67 Encoding requiredEncodingForCharset(String charset) { | |
| 68 var encoding = _encodingForCharset(charset); | |
| 69 if (encoding != null) return encoding; | |
| 70 throw new UnsupportedError('Unsupported encoding "$charset".'); | |
|
Bob Nystrom
2012/10/31 01:17:44
I think this should be ArgumentError or possibly F
nweiz
2012/10/31 18:20:59
Changed to ArgumentError.
| |
| 71 } | |
| 72 | |
| 73 /// Return the [Encoding] that corresponds to [charset]. Return null if no | |
| 74 /// [Encoding] was found that corresponds to [charset]. [charset] may not be | |
| 75 /// null. | |
| 76 Encoding _encodingForCharset(String charset) { | |
| 77 charset = charset.toLowerCase(); | |
| 78 if (charset == 'ascii' || charset == 'us-ascii') return Encoding.ASCII; | |
| 79 if (charset == 'utf-8') return Encoding.UTF_8; | |
| 80 if (charset == 'iso-8859-1') return Encoding.ISO_8859_1; | |
| 81 return null; | |
| 82 } | |
| 83 | |
| 84 /// Convert [bytes] into a [String] according to [encoding]. | |
| 85 String decodeString(List<int> bytes, Encoding encoding) { | |
| 86 // TODO(nweiz): implement this once issue 6284 is fixed. | |
| 87 return new String.fromCharCodes(bytes); | |
| 88 } | |
| 89 | |
| 90 /// Convert [string] into a byte array according to [encoding]. | |
| 91 List<int> encodeString(String string, Encoding encoding) { | |
| 92 // TODO(nweiz): implement this once issue 6284 is fixed. | |
| 93 return string.charCodes; | |
| 94 } | |
| 95 | |
| 96 /// Convert [input] into a [Uint8List]. If [input] is a [ByteArray] or | |
| 97 /// [ByteArrayViewable], this just returns a view on [input]. | |
| 98 Uint8List uint8List(List<int> input) { | |
|
Bob Nystrom
2012/10/31 01:17:44
This name isn't very verby. How about "toUint8List
nweiz
2012/10/31 18:20:59
Done.
| |
| 99 if (input is Uint8List) return input; | |
| 100 if (input is ByteArrayViewable) input = input.asByteArray(); | |
| 101 if (input is ByteArray) return new Uint8List.view(input); | |
|
Bob Nystrom
2012/10/31 01:17:44
Does the order of these two lines matter? If not,
nweiz
2012/10/31 18:20:59
Yes, the previous line causes "input" to become a
| |
| 102 var output = new Uint8List(input.length); | |
| 103 output.setRange(0, input.length, input); | |
| 104 return output; | |
| 105 } | |
| 106 | |
| 107 /// Buffers all input from an InputStream and returns it as a future. | |
| 108 Future<List<int>> consumeInputStream(InputStream stream) { | |
| 109 var completer = new Completer<List<int>>(); | |
| 110 /// TODO(nweiz): use BufferList when issue 6409 is fixed | |
| 111 var buffer = <int>[]; | |
| 112 stream.onClosed = () => completer.complete(buffer); | |
| 113 stream.onData = () => buffer.addAll(stream.read()); | |
| 114 stream.onError = (e) => completer.completeException(e); | |
|
Bob Nystrom
2012/10/31 01:17:44
stream.onError = completer.completeException;
nweiz
2012/10/31 18:20:59
Done.
| |
| 115 return completer.future; | |
| 116 } | |
| 117 | |
| 118 /// Takes all input from [source] and writes it to [sink]. | |
| 119 void pipeInputToInput(InputStream source, ListInputStream sink) { | |
|
Bob Nystrom
2012/10/31 01:17:44
This method name seems crazy, but then I guess tho
nweiz
2012/10/31 18:20:59
In the context of this method, "sink" is coneptual
| |
| 120 source.onClosed = () => sink.markEndOfStream(); | |
| 121 source.onData = () => sink.write(source.read()); | |
| 122 // TODO(nweiz): propagate source errors to the sink. See issue 3657. | |
| 123 } | |
| 124 | |
| 125 /// Returns a [Future] that asynchronously completes to `null`. | |
| 126 Future get async { | |
| 127 var completer = new Completer(); | |
| 128 new Timer(0, (_) => completer.complete(null)); | |
| 129 return completer.future; | |
| 130 } | |
| OLD | NEW |