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.decoder; | 5 library convert.hex.decoder; |
6 | 6 |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 import 'dart:typed_data'; | 8 import 'dart:typed_data'; |
9 | 9 |
10 import '../utils.dart'; | 10 import '../utils.dart'; |
11 | 11 |
12 /// The canonical instance of [HexDecoder]. | 12 /// The canonical instance of [HexDecoder]. |
13 const hexDecoder = const HexDecoder._(); | 13 const hexDecoder = const HexDecoder._(); |
14 | 14 |
15 /// A converter that decodes hexadecimal strings into byte arrays. | 15 /// A converter that decodes hexadecimal strings into byte arrays. |
16 /// | 16 /// |
17 /// Because two hexadecimal digits correspond to a single byte, this will throw | 17 /// Because two hexadecimal digits correspond to a single byte, this will throw |
18 /// a [FormatException] if given an odd-length string. It will also throw a | 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. | 19 /// [FormatException] if given a string containing non-hexadecimal code units. |
20 class HexDecoder | 20 class HexDecoder extends Converter<String, List<int>> { |
21 extends ChunkedConverter<String, List<int>, String, List<int>> { | |
22 const HexDecoder._(); | 21 const HexDecoder._(); |
23 | 22 |
24 List<int> convert(String string) { | 23 List<int> convert(String string) { |
25 if (!string.length.isEven) { | 24 if (!string.length.isEven) { |
26 throw new FormatException("Invalid input length, must be even.", | 25 throw new FormatException("Invalid input length, must be even.", |
27 string, string.length); | 26 string, string.length); |
28 } | 27 } |
29 | 28 |
30 var bytes = new Uint8List(string.length ~/ 2); | 29 var bytes = new Uint8List(string.length ~/ 2); |
31 _decode(string.codeUnits, 0, string.length, bytes, 0); | 30 _decode(string.codeUnits, 0, string.length, bytes, 0); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 var destinationIndex = destinationStart; | 163 var destinationIndex = destinationStart; |
165 for (var i = sourceStart; i < sourceEnd - 1; i += 2) { | 164 for (var i = sourceStart; i < sourceEnd - 1; i += 2) { |
166 var firstDigit = digitForCodeUnit(codeUnits, i); | 165 var firstDigit = digitForCodeUnit(codeUnits, i); |
167 var secondDigit = digitForCodeUnit(codeUnits, i + 1); | 166 var secondDigit = digitForCodeUnit(codeUnits, i + 1); |
168 destination[destinationIndex++] = 16 * firstDigit + secondDigit; | 167 destination[destinationIndex++] = 16 * firstDigit + secondDigit; |
169 } | 168 } |
170 | 169 |
171 if ((sourceEnd - sourceStart).isEven) return null; | 170 if ((sourceEnd - sourceStart).isEven) return null; |
172 return 16 * digitForCodeUnit(codeUnits, sourceEnd - 1); | 171 return 16 * digitForCodeUnit(codeUnits, sourceEnd - 1); |
173 } | 172 } |
OLD | NEW |