Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library convert.hex.decoder; | |
| 6 | |
| 7 import 'dart:convert'; | |
| 8 import 'dart:typed_data'; | |
| 9 | |
| 10 import 'package:charcode/ascii.dart'; | |
| 11 | |
| 12 /// The canonical instance of [HexDecoder]. | |
| 13 const hexDecoder = const HexDecoder._(); | |
| 14 | |
| 15 /// A converter that decodes hexadecimal strings into byte arrays. | |
| 16 /// | |
| 17 /// Because two hexadecimal digits correspond to a single byte, this will throw | |
|
Lasse Reichstein Nielsen
2015/09/23 08:32:04
Maybe mention that this is RFC 4648 base-16 encodi
nweiz
2015/09/23 22:04:20
Done, in the HexCodec documentation.
| |
| 18 /// a [FormatException] if given an odd-length string. It will also throw a | |
| 19 /// [FormatException] if given a string containing non-hexadecimal code units. | |
| 20 class HexDecoder extends Converter<String, List<int>> { | |
| 21 const HexDecoder._(); | |
| 22 | |
| 23 List<int> convert(String string) { | |
| 24 if (!string.length.isEven) { | |
| 25 throw new FormatException("Expected an even-length hexadecimal string, " | |
| 26 "was odd."); | |
|
Lasse Reichstein Nielsen
2015/09/23 08:32:04
Add ", string, string.length" as arguments to Form
nweiz
2015/09/23 22:04:21
Done.
| |
| 27 } | |
| 28 | |
| 29 var bytes = new Uint8List(string.length ~/ 2); | |
| 30 _decode(string.runes, bytes, 0); | |
|
Lasse Reichstein Nielsen
2015/09/23 08:32:04
Use string.codeUnits instead. It's more efficient
nweiz
2015/09/23 22:04:20
Done.
| |
| 31 return bytes; | |
| 32 } | |
| 33 | |
| 34 StringConversionSink startChunkedConversion(Sink<List<int>> sink) => | |
| 35 new _HexDecoderSink(sink); | |
| 36 } | |
| 37 | |
| 38 /// A sink for chunked hexadecimal decoding. | |
|
Lasse Reichstein Nielsen
2015/09/23 08:32:04
A conversion sink for ...
("A sink" is too generic
nweiz
2015/09/23 22:04:21
Done.
| |
| 39 class _HexDecoderSink extends StringConversionSinkBase { | |
| 40 /// The underlying sink to which decoded byte arrays will be passed. | |
| 41 final Sink<List<int>> _sink; | |
| 42 | |
| 43 /// The trailing digit from the previous string. | |
| 44 /// | |
| 45 /// This will be non-`null` if the most recent string had an odd number of | |
| 46 /// hexadecimal digits. Since it's the most significant digit, it's always a | |
| 47 /// multiple of 16. | |
| 48 int _lastDigit; | |
| 49 | |
| 50 _HexDecoderSink(this._sink); | |
| 51 | |
| 52 void addSlice(String string, int start, int end, bool isLast) { | |
|
Lasse Reichstein Nielsen
2015/09/23 08:32:03
Maybe use:
end = RangeError.checkValidRange(star
nweiz
2015/09/23 22:04:21
Done, although I'm not checking for null explicitl
| |
| 53 if (start > end) { | |
| 54 throw new ArgumentError( | |
| 55 "start $start must be less than or equal to end $end."); | |
| 56 } | |
| 57 | |
| 58 var bytes; | |
| 59 var bytesStart; | |
| 60 if (_lastDigit == null) { | |
|
Lasse Reichstein Nielsen
2015/09/23 08:32:03
dart2js would probably prefer if the variable alwa
nweiz
2015/09/23 22:04:20
I really don't like using magic numbers to indicat
Lasse Reichstein Nielsen
2015/09/24 08:07:01
True. This is definitely speculative, so keep it a
| |
| 61 bytes = new Uint8List((end - start) ~/ 2); | |
| 62 bytesStart = 0; | |
| 63 } else { | |
| 64 start++; | |
| 65 bytes = new Uint8List(1 + (end - start) ~/ 2); | |
| 66 bytes[0] = _lastDigit + _digitForCodeUnit(string.codeUnitAt(start - 1)); | |
|
Lasse Reichstein Nielsen
2015/09/23 08:32:04
Slightly confusing to increment start first and th
nweiz
2015/09/23 22:04:20
Done.
| |
| 67 bytesStart = 1; | |
| 68 } | |
| 69 | |
| 70 var runes = string.runes.skip(start).take(end); | |
|
Lasse Reichstein Nielsen
2015/09/23 08:32:03
Should have be either .take(end).skip(start) or .s
nweiz
2015/09/23 22:04:21
Changed to use codeUnits, although I'm still using
| |
| 71 _lastDigit = _decode(runes, bytes, bytesStart); | |
| 72 | |
| 73 _sink.add(bytes); | |
| 74 if (isLast) close(); | |
| 75 } | |
| 76 | |
| 77 void close() { | |
| 78 if (_lastDigit != null) { | |
| 79 throw new FormatException("Expected an even-length hexadecimal string, " | |
| 80 "was odd."); | |
|
Lasse Reichstein Nielsen
2015/09/23 08:32:03
Same message as above (if you changed it there).
nweiz
2015/09/23 22:04:20
Done.
| |
| 81 } | |
| 82 | |
| 83 _sink.close(); | |
| 84 } | |
|
Lasse Reichstein Nielsen
2015/09/23 08:32:04
Consider extending with asUtf8Sink because you can
nweiz
2015/09/23 22:04:20
Done.
| |
| 85 } | |
| 86 | |
| 87 /// Decodes [runes] and writes the result into [destination]. | |
| 88 /// | |
| 89 /// This begins writing into destination at the index [start]. | |
| 90 int _decode(Iterable<int> runes, List<int> destination, int start) { | |
|
Lasse Reichstein Nielsen
2015/09/23 08:32:03
Make it: _decode(List<int> codeUnits, int start, i
nweiz
2015/09/23 22:04:21
Is that going to be substantially more efficient?
Lasse Reichstein Nielsen
2015/09/24 08:07:01
Most likely yes.
This is the inner loop of the tra
nweiz
2015/09/24 23:23:42
Done.
| |
| 91 var iterator = runes.iterator; | |
| 92 for (var i = start; true; i++) { | |
|
Lasse Reichstein Nielsen
2015/09/23 08:32:04
remove the "true". An omitted condition in a for s
nweiz
2015/09/23 22:04:21
Done.
| |
| 93 if (!iterator.moveNext()) return null; | |
| 94 var firstDigit = _digitForCodeUnit(iterator.current) * 16; | |
| 95 | |
| 96 if (!iterator.moveNext()) return firstDigit; | |
| 97 var secondDigit = _digitForCodeUnit(iterator.current); | |
| 98 | |
| 99 destination[i] = firstDigit + secondDigit; | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 /// Returns the digit (0 through 15) corresponding to the hexadecimal code unit | |
| 104 /// [codeUnit]. | |
| 105 /// | |
| 106 /// If [codeUnit] isn't a valid hexadecimal code unit, throws a | |
| 107 /// [FormatException]. | |
| 108 int _digitForCodeUnit(int codeUnit) { | |
| 109 if (codeUnit >= $0 && codeUnit <= $9) return codeUnit - $0; | |
| 110 if (codeUnit >= $a && codeUnit <= $f) return codeUnit - $a + 10; | |
| 111 if (codeUnit >= $A && codeUnit <= $F) return codeUnit - $A + 10; | |
| 112 | |
|
Lasse Reichstein Nielsen
2015/09/23 08:32:04
Consider optimizing this to:
int digit = codeUnit
nweiz
2015/09/23 22:04:21
If this is worth heavily optimizing, would it be b
Lasse Reichstein Nielsen
2015/09/24 08:07:01
It's executed twice in the inner loop of the trans
nweiz
2015/09/24 23:23:41
Done.
| |
| 113 throw new FormatException("Invalid hexadecimal code point " | |
| 114 "U+${codeUnit.toRadixString(16)}."); | |
|
Lasse Reichstein Nielsen
2015/09/23 08:32:04
code point -> code unit
add: .padLeft(4, '0') so t
nweiz
2015/09/23 22:04:21
Done.
| |
| 115 } | |
| OLD | NEW |