Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(254)

Side by Side Diff: packages/convert/lib/src/hex/encoder.dart

Issue 2990843002: Removed fixed dependencies (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « packages/convert/lib/src/hex/decoder.dart ('k') | packages/convert/lib/src/percent.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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.encoder;
6
7 import 'dart:convert';
8 import 'dart:typed_data';
9
10 import 'package:charcode/ascii.dart';
11
12 /// The canonical instance of [HexEncoder].
13 const hexEncoder = const HexEncoder._();
14
15 /// A converter that encodes byte arrays into hexadecimal strings.
16 ///
17 /// This will throw a [RangeError] if the byte array has any digits that don't
18 /// fit in the gamut of a byte.
19 class HexEncoder extends Converter<List<int>, String> {
20 const HexEncoder._();
21
22 String convert(List<int> bytes) => _convert(bytes, 0, bytes.length);
23
24 ByteConversionSink startChunkedConversion(Sink<String> sink) =>
25 new _HexEncoderSink(sink);
26 }
27
28 /// A conversion sink for chunked hexadecimal encoding.
29 class _HexEncoderSink extends ByteConversionSinkBase {
30 /// The underlying sink to which decoded byte arrays will be passed.
31 final Sink<String> _sink;
32
33 _HexEncoderSink(this._sink);
34
35 void add(List<int> chunk) {
36 _sink.add(_convert(chunk, 0, chunk.length));
37 }
38
39 void addSlice(List<int> chunk, int start, int end, bool isLast) {
40 RangeError.checkValidRange(start, end, chunk.length);
41 _sink.add(_convert(chunk, start, end));
42 if (isLast) _sink.close();
43 }
44
45 void close() {
46 _sink.close();
47 }
48 }
49
50 String _convert(List<int> bytes, int start, int end) {
51 // A Uint8List is more efficient than a StringBuffer given that we know that
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
57 // 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
59 // core loop.
60 var byteOr = 0;
61 for (var i = start; i < end; i++) {
62 var byte = bytes[i];
63 byteOr |= byte;
64
65 // 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
67 // it can't prove that [byte] will always be positive.
68 buffer[bufferIndex++] = _codeUnitForDigit((byte & 0xF0) >> 4);
69 buffer[bufferIndex++] = _codeUnitForDigit(byte & 0x0F);
70 }
71
72 if (byteOr >= 0 && byteOr <= 255) return new String.fromCharCodes(buffer);
73
74 // If there was an invalid byte, find it and throw an exception.
75 for (var i = start; i < end; i++) {
76 var byte = bytes[i];
77 if (byte >= 0 && byte <= 0xff) continue;
78 throw new FormatException(
79 "Invalid byte ${byte < 0 ? "-" : ""}0x${byte.abs().toRadixString(16)}.",
80 bytes, i);
81 }
82
83 throw 'unreachable';
84 }
85
86 /// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit
87 /// [digit].
88 int _codeUnitForDigit(int digit) => digit < 10 ? digit + $0 : digit + $a - 10;
OLDNEW
« no previous file with comments | « packages/convert/lib/src/hex/decoder.dart ('k') | packages/convert/lib/src/percent.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698