Index: lib/src/hex/encoder.dart |
diff --git a/lib/src/hex/encoder.dart b/lib/src/hex/encoder.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c9650ab753c8dfa7b2fce9851ef78ca3479a35a5 |
--- /dev/null |
+++ b/lib/src/hex/encoder.dart |
@@ -0,0 +1,82 @@ |
+// 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.encoder; |
+ |
+import 'dart:convert'; |
+import 'dart:typed_data'; |
+ |
+import 'package:charcode/ascii.dart'; |
+ |
+/// The canonical instance of [HexEncoder]. |
+const hexEncoder = const HexEncoder._(); |
+ |
+/// A converter that encodes byte arrays into hexadecimal strings. |
+/// |
+/// This will throw a [RangeError] if the byte array has any digits that don't |
+/// fit in the gamut of a byte. |
+class HexEncoder extends Converter<List<int>, String> { |
+ const HexEncoder._(); |
+ |
+ String convert(List<int> bytes) => _convert(bytes, 0, bytes.length); |
+ |
+ ByteConversionSink startChunkedConversion(Sink<String> sink) => |
+ new _HexEncoderSink(sink); |
+} |
+ |
+/// A conversion sink for chunked hexadecimal encoding. |
+class _HexEncoderSink extends ByteConversionSinkBase { |
+ /// The underlying sink to which decoded byte arrays will be passed. |
+ final Sink<String> _sink; |
+ |
+ _HexEncoderSink(this._sink); |
+ |
+ void add(List<int> chunk) { |
+ _sink.add(_convert(chunk, 0, chunk.length)); |
+ } |
+ |
+ void addSlice(List<int> chunk, int start, int end, bool isLast) { |
+ RangeError.checkValidRange(start, end, chunk.length); |
+ _sink.add(_convert(chunk, start, end)); |
+ if (isLast) _sink.close(); |
+ } |
+ |
+ void close() { |
+ _sink.close(); |
+ } |
+} |
+ |
+String _convert(List<int> bytes, int start, int end) { |
+ // A Uint8List is more efficient than a StringBuffer given that we know that |
+ // we're only emitting ASCII-compatible characters. |
Lasse Reichstein Nielsen
2015/09/24 08:07:02
, and that we know the length ahead of time.
nweiz
2015/09/24 23:23:42
Done.
|
+ var buffer = new Uint8List((end - start) * 2); |
+ var bufferIndex = 0; |
+ |
+ // A bitwise OR of all bytes in [bytes]. This allows us to check for |
+ // out-of-range bytes without adding more branches than necessary to the |
+ // core loop. |
+ var byteOr = 0; |
+ for (var i = start; i < end; i++) { |
+ var byte = bytes[i]; |
+ byteOr |= byte; |
+ buffer[bufferIndex++] = _codeUnitForDigit(byte ~/ 16); |
+ buffer[bufferIndex++] = _codeUnitForDigit(byte % 16); |
sra1
2015/09/24 18:39:35
consider:
buffer[bufferIndex++] = _codeUnitFo
|
+ } |
+ |
+ if (byteOr >= 0 && byteOr <= 255) return new String.fromCharCodes(buffer); |
+ |
+ // If there was an invalid byte, find it and throw an exception. |
+ for (var i = start; i < end; i++) { |
+ var byte = bytes[i]; |
+ if (byte >= 0 && byte <= 0xff) continue; |
+ throw new FormatException("Invalid byte 0x${byte.toRadixString(16)}.", |
+ bytes, i); |
+ } |
+ |
+ throw 'unreachable'; |
Lasse Reichstein Nielsen
2015/09/24 08:07:02
Hmm! I can see why the "throw 'unreachable' is nee
nweiz
2015/09/24 23:23:42
I think I prefer a standard-looking for loop with
|
+} |
+ |
+/// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit |
+/// [digit]. |
+int _codeUnitForDigit(int digit) => digit < 10 ? digit + $0 : digit + $a - 10; |