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. |
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 the byte is an uppercase letter, convert it to lowercase to check if |
| 65 // it's unreserved. This works because uppercase letters in ASCII are |
| 66 // exactly `0b100000 = 0x20` less than lowercase letters, so if we ensure |
| 67 // that that bit is 1 we ensure that the letter is lowercase. |
| 68 var letter = 0x20 | byte; |
| 69 if ((letter >= $a && letter <= $z) || |
| 70 byte == $dash || |
| 71 byte == $dot || |
| 72 byte == $underscore || |
| 73 byte == $tilde) { |
| 74 // Unreserved characters are safe to write as-is. |
| 75 buffer.writeCharCode(byte); |
| 76 continue; |
| 77 } |
| 78 |
| 79 buffer.writeCharCode($percent); |
| 80 |
65 // The bitwise arithmetic here is equivalent to `byte ~/ 16` and `byte % 16` | 81 // 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 | 82 // for valid byte values, but is easier for dart2js to optimize given that |
67 // it can't prove that [byte] will always be positive. | 83 // it can't prove that [byte] will always be positive. |
68 buffer[bufferIndex++] = _codeUnitForDigit((byte & 0xF0) >> 4); | 84 buffer.writeCharCode(_codeUnitForDigit((byte & 0xF0) >> 4)); |
69 buffer[bufferIndex++] = _codeUnitForDigit(byte & 0x0F); | 85 buffer.writeCharCode(_codeUnitForDigit(byte & 0x0F)); |
70 } | 86 } |
71 | 87 |
72 if (byteOr >= 0 && byteOr <= 255) return new String.fromCharCodes(buffer); | 88 if (byteOr >= 0 && byteOr <= 255) return buffer.toString(); |
73 | 89 |
74 // If there was an invalid byte, find it and throw an exception. | 90 // If there was an invalid byte, find it and throw an exception. |
75 for (var i = start; i < end; i++) { | 91 for (var i = start; i < end; i++) { |
76 var byte = bytes[i]; | 92 var byte = bytes[i]; |
77 if (byte >= 0 && byte <= 0xff) continue; | 93 if (byte >= 0 && byte <= 0xff) continue; |
78 throw new FormatException("Invalid byte 0x${byte.toRadixString(16)}.", | 94 throw new FormatException( |
| 95 "Invalid byte ${byte < 0 ? "-" : ""}0x${byte.abs().toRadixString(16)}.", |
79 bytes, i); | 96 bytes, i); |
80 } | 97 } |
81 | 98 |
82 throw 'unreachable'; | 99 throw 'unreachable'; |
83 } | 100 } |
84 | 101 |
85 /// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit | 102 /// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit |
86 /// [digit]. | 103 /// [digit]. |
87 int _codeUnitForDigit(int digit) => digit < 10 ? digit + $0 : digit + $a - 10; | 104 int _codeUnitForDigit(int digit) => digit < 10 ? digit + $0 : digit + $A - 10; |
OLD | NEW |