OLD | NEW |
---|---|
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 convert.hex.encoder; | 5 library convert.percent.encoder; |
6 | 6 |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 import 'dart:typed_data'; | |
9 | 8 |
10 import 'package:charcode/ascii.dart'; | 9 import 'package:charcode/ascii.dart'; |
11 | 10 |
12 /// The canonical instance of [HexEncoder]. | 11 /// The canonical instance of [PercentEncoder]. |
13 const hexEncoder = const HexEncoder._(); | 12 const percentEncoder = const PercentEncoder._(); |
14 | 13 |
15 /// A converter that encodes byte arrays into hexadecimal strings. | 14 /// A converter that encodes byte arrays into percent-encoded strings. |
15 /// | |
16 /// [encoder] encodes all bytes other than ASCII letters, decimal digits, or one | |
17 /// of `-._~`. This matches the behavior of [Uri.encodeQueryComponent] except | |
18 /// that it doesn't encode `0x20` bytes to the `+` character. | |
16 /// | 19 /// |
17 /// This will throw a [RangeError] if the byte array has any digits that don't | 20 /// This will throw a [RangeError] if the byte array has any digits that don't |
18 /// fit in the gamut of a byte. | 21 /// fit in the gamut of a byte. |
19 class HexEncoder extends Converter<List<int>, String> { | 22 class PercentEncoder extends Converter<List<int>, String> { |
20 const HexEncoder._(); | 23 const PercentEncoder._(); |
21 | 24 |
22 String convert(List<int> bytes) => _convert(bytes, 0, bytes.length); | 25 String convert(List<int> bytes) => _convert(bytes, 0, bytes.length); |
23 | 26 |
24 ByteConversionSink startChunkedConversion(Sink<String> sink) => | 27 ByteConversionSink startChunkedConversion(Sink<String> sink) => |
25 new _HexEncoderSink(sink); | 28 new _PercentEncoderSink(sink); |
26 } | 29 } |
27 | 30 |
28 /// A conversion sink for chunked hexadecimal encoding. | 31 /// A conversion sink for chunked percentadecimal encoding. |
Lasse Reichstein Nielsen
2015/10/08 10:30:36
Ah, that's where percentadecimal comes from :)
nweiz
2015/10/08 20:44:52
Done.
| |
29 class _HexEncoderSink extends ByteConversionSinkBase { | 32 class _PercentEncoderSink extends ByteConversionSinkBase { |
30 /// The underlying sink to which decoded byte arrays will be passed. | 33 /// The underlying sink to which decoded byte arrays will be passed. |
31 final Sink<String> _sink; | 34 final Sink<String> _sink; |
32 | 35 |
33 _HexEncoderSink(this._sink); | 36 _PercentEncoderSink(this._sink); |
34 | 37 |
35 void add(List<int> chunk) { | 38 void add(List<int> chunk) { |
36 _sink.add(_convert(chunk, 0, chunk.length)); | 39 _sink.add(_convert(chunk, 0, chunk.length)); |
37 } | 40 } |
38 | 41 |
39 void addSlice(List<int> chunk, int start, int end, bool isLast) { | 42 void addSlice(List<int> chunk, int start, int end, bool isLast) { |
40 RangeError.checkValidRange(start, end, chunk.length); | 43 RangeError.checkValidRange(start, end, chunk.length); |
41 _sink.add(_convert(chunk, start, end)); | 44 _sink.add(_convert(chunk, start, end)); |
42 if (isLast) _sink.close(); | 45 if (isLast) _sink.close(); |
43 } | 46 } |
44 | 47 |
45 void close() { | 48 void close() { |
46 _sink.close(); | 49 _sink.close(); |
47 } | 50 } |
48 } | 51 } |
49 | 52 |
50 String _convert(List<int> bytes, int start, int end) { | 53 String _convert(List<int> bytes, int start, int end) { |
51 // A Uint8List is more efficient than a StringBuffer given that we know that | 54 var buffer = new StringBuffer(); |
52 // we're only emitting ASCII-compatible characters, and that we know the | |
53 // length ahead of time. | |
54 var buffer = new Uint8List((end - start) * 2); | |
55 var bufferIndex = 0; | |
56 | 55 |
57 // A bitwise OR of all bytes in [bytes]. This allows us to check for | 56 // A bitwise OR of all bytes in [bytes]. This allows us to check for |
58 // out-of-range bytes without adding more branches than necessary to the | 57 // out-of-range bytes without adding more branches than necessary to the |
59 // core loop. | 58 // core loop. |
60 var byteOr = 0; | 59 var byteOr = 0; |
61 for (var i = start; i < end; i++) { | 60 for (var i = start; i < end; i++) { |
62 var byte = bytes[i]; | 61 var byte = bytes[i]; |
63 byteOr |= byte; | 62 byteOr |= byte; |
64 | 63 |
64 if ((byte >= $a && byte <= $z) || | |
65 (byte >= $A && byte <= $Z) || | |
Lasse Reichstein Nielsen
2015/10/08 10:30:36
You can save a test by doing case-canonicalization
nweiz
2015/10/08 20:44:52
Done.
| |
66 byte == $dash || | |
67 byte == $dot || | |
68 byte == $underscore || | |
69 byte == $tilde) { | |
70 // Unreserved characters are safe to write as-is. | |
71 buffer.writeCharCode(byte); | |
72 continue; | |
73 } | |
74 | |
75 buffer.writeCharCode($percent); | |
76 | |
65 // The bitwise arithmetic here is equivalent to `byte ~/ 16` and `byte % 16` | 77 // The bitwise arithmetic here is equivalent to `byte ~/ 16` and `byte % 16` |
66 // for valid byte values, but is easier for dart2js to optimize given that | 78 // for valid byte values, but is easier for dart2js to optimize given that |
67 // it can't prove that [byte] will always be positive. | 79 // it can't prove that [byte] will always be positive. |
68 buffer[bufferIndex++] = _codeUnitForDigit((byte & 0xF0) >> 4); | 80 buffer.writeCharCode(_codeUnitForDigit((byte & 0xF0) >> 4)); |
69 buffer[bufferIndex++] = _codeUnitForDigit(byte & 0x0F); | 81 buffer.writeCharCode(_codeUnitForDigit(byte & 0x0F)); |
70 } | 82 } |
71 | 83 |
72 if (byteOr >= 0 && byteOr <= 255) return new String.fromCharCodes(buffer); | 84 if (byteOr >= 0 && byteOr <= 255) return buffer.toString(); |
73 | 85 |
74 // If there was an invalid byte, find it and throw an exception. | 86 // If there was an invalid byte, find it and throw an exception. |
75 for (var i = start; i < end; i++) { | 87 for (var i = start; i < end; i++) { |
76 var byte = bytes[i]; | 88 var byte = bytes[i]; |
77 if (byte >= 0 && byte <= 0xff) continue; | 89 if (byte >= 0 && byte <= 0xff) continue; |
78 throw new FormatException("Invalid byte 0x${byte.toRadixString(16)}.", | 90 throw new FormatException("Invalid byte 0x${byte.toRadixString(16)}.", |
Lasse Reichstein Nielsen
2015/10/08 10:30:37
This will look bad if "byte" is negative.
Maybe: $
nweiz
2015/10/08 20:44:52
Done.
| |
79 bytes, i); | 91 bytes, i); |
80 } | 92 } |
81 | 93 |
82 throw 'unreachable'; | 94 throw 'unreachable'; |
83 } | 95 } |
84 | 96 |
85 /// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit | 97 /// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit |
86 /// [digit]. | 98 /// [digit]. |
87 int _codeUnitForDigit(int digit) => digit < 10 ? digit + $0 : digit + $a - 10; | 99 int _codeUnitForDigit(int digit) => digit < 10 ? digit + $0 : digit + $A - 10; |
OLD | NEW |