Index: lib/src/hex/decoder.dart |
diff --git a/lib/src/hex/decoder.dart b/lib/src/hex/decoder.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8554ed0ded873e9cd5cf45e700934ff3af6d6450 |
--- /dev/null |
+++ b/lib/src/hex/decoder.dart |
@@ -0,0 +1,115 @@ |
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+library convert.hex.decoder; |
+ |
+import 'dart:convert'; |
+import 'dart:typed_data'; |
+ |
+import 'package:charcode/ascii.dart'; |
+ |
+/// The canonical instance of [HexDecoder]. |
+const hexDecoder = const HexDecoder._(); |
+ |
+/// A converter that decodes hexadecimal strings into byte arrays. |
+/// |
+/// 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.
|
+/// a [FormatException] if given an odd-length string. It will also throw a |
+/// [FormatException] if given a string containing non-hexadecimal code units. |
+class HexDecoder extends Converter<String, List<int>> { |
+ const HexDecoder._(); |
+ |
+ List<int> convert(String string) { |
+ if (!string.length.isEven) { |
+ throw new FormatException("Expected an even-length hexadecimal string, " |
+ "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.
|
+ } |
+ |
+ var bytes = new Uint8List(string.length ~/ 2); |
+ _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.
|
+ return bytes; |
+ } |
+ |
+ StringConversionSink startChunkedConversion(Sink<List<int>> sink) => |
+ new _HexDecoderSink(sink); |
+} |
+ |
+/// 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.
|
+class _HexDecoderSink extends StringConversionSinkBase { |
+ /// The underlying sink to which decoded byte arrays will be passed. |
+ final Sink<List<int>> _sink; |
+ |
+ /// The trailing digit from the previous string. |
+ /// |
+ /// This will be non-`null` if the most recent string had an odd number of |
+ /// hexadecimal digits. Since it's the most significant digit, it's always a |
+ /// multiple of 16. |
+ int _lastDigit; |
+ |
+ _HexDecoderSink(this._sink); |
+ |
+ 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
|
+ if (start > end) { |
+ throw new ArgumentError( |
+ "start $start must be less than or equal to end $end."); |
+ } |
+ |
+ var bytes; |
+ var bytesStart; |
+ 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
|
+ bytes = new Uint8List((end - start) ~/ 2); |
+ bytesStart = 0; |
+ } else { |
+ start++; |
+ bytes = new Uint8List(1 + (end - start) ~/ 2); |
+ 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.
|
+ bytesStart = 1; |
+ } |
+ |
+ 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
|
+ _lastDigit = _decode(runes, bytes, bytesStart); |
+ |
+ _sink.add(bytes); |
+ if (isLast) close(); |
+ } |
+ |
+ void close() { |
+ if (_lastDigit != null) { |
+ throw new FormatException("Expected an even-length hexadecimal string, " |
+ "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.
|
+ } |
+ |
+ _sink.close(); |
+ } |
Lasse Reichstein Nielsen
2015/09/23 08:32:04
Consider extending with asUtf8Sink because you can
nweiz
2015/09/23 22:04:20
Done.
|
+} |
+ |
+/// Decodes [runes] and writes the result into [destination]. |
+/// |
+/// This begins writing into destination at the index [start]. |
+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.
|
+ var iterator = runes.iterator; |
+ 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.
|
+ if (!iterator.moveNext()) return null; |
+ var firstDigit = _digitForCodeUnit(iterator.current) * 16; |
+ |
+ if (!iterator.moveNext()) return firstDigit; |
+ var secondDigit = _digitForCodeUnit(iterator.current); |
+ |
+ destination[i] = firstDigit + secondDigit; |
+ } |
+} |
+ |
+/// Returns the digit (0 through 15) corresponding to the hexadecimal code unit |
+/// [codeUnit]. |
+/// |
+/// If [codeUnit] isn't a valid hexadecimal code unit, throws a |
+/// [FormatException]. |
+int _digitForCodeUnit(int codeUnit) { |
+ if (codeUnit >= $0 && codeUnit <= $9) return codeUnit - $0; |
+ if (codeUnit >= $a && codeUnit <= $f) return codeUnit - $a + 10; |
+ if (codeUnit >= $A && codeUnit <= $F) return codeUnit - $A + 10; |
+ |
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.
|
+ throw new FormatException("Invalid hexadecimal code point " |
+ "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.
|
+} |