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.percent.encoder; | 5 library convert.percent.encoder; |
6 | 6 |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 | 8 |
9 import 'package:charcode/ascii.dart'; | 9 import 'package:charcode/ascii.dart'; |
10 | 10 |
11 /// The canonical instance of [PercentEncoder]. | 11 /// The canonical instance of [PercentEncoder]. |
12 const percentEncoder = const PercentEncoder._(); | 12 const percentEncoder = const PercentEncoder._(); |
13 | 13 |
14 /// A converter that encodes byte arrays into percent-encoded strings. | 14 /// A converter that encodes byte arrays into percent-encoded strings. |
15 /// | 15 /// |
16 /// [encoder] encodes all bytes other than ASCII letters, decimal digits, or one | 16 /// [encoder] encodes all bytes other than ASCII letters, decimal digits, or one |
17 /// of `-._~`. This matches the behavior of [Uri.encodeQueryComponent] except | 17 /// of `-._~`. This matches the behavior of [Uri.encodeQueryComponent] except |
18 /// that it doesn't encode `0x20` bytes to the `+` character. | 18 /// that it doesn't encode `0x20` bytes to the `+` character. |
19 /// | 19 /// |
20 /// This will throw a [RangeError] if the byte array has any digits that don't | 20 /// This will throw a [RangeError] if the byte array has any digits that don't |
21 /// fit in the gamut of a byte. | 21 /// fit in the gamut of a byte. |
22 class PercentEncoder extends Converter<List<int>, String> { | 22 class PercentEncoder |
| 23 extends ChunkedConverter<List<int>, String, List<int>, String> { |
23 const PercentEncoder._(); | 24 const PercentEncoder._(); |
24 | 25 |
25 String convert(List<int> bytes) => _convert(bytes, 0, bytes.length); | 26 String convert(List<int> bytes) => _convert(bytes, 0, bytes.length); |
26 | 27 |
27 ByteConversionSink startChunkedConversion(Sink<String> sink) => | 28 ByteConversionSink startChunkedConversion(Sink<String> sink) => |
28 new _PercentEncoderSink(sink); | 29 new _PercentEncoderSink(sink); |
29 } | 30 } |
30 | 31 |
31 /// A conversion sink for chunked percentadecimal encoding. | 32 /// A conversion sink for chunked percentadecimal encoding. |
32 class _PercentEncoderSink extends ByteConversionSinkBase { | 33 class _PercentEncoderSink extends ByteConversionSinkBase { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 "Invalid byte ${byte < 0 ? "-" : ""}0x${byte.abs().toRadixString(16)}.", | 96 "Invalid byte ${byte < 0 ? "-" : ""}0x${byte.abs().toRadixString(16)}.", |
96 bytes, i); | 97 bytes, i); |
97 } | 98 } |
98 | 99 |
99 throw 'unreachable'; | 100 throw 'unreachable'; |
100 } | 101 } |
101 | 102 |
102 /// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit | 103 /// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit |
103 /// [digit]. | 104 /// [digit]. |
104 int _codeUnitForDigit(int digit) => digit < 10 ? digit + $0 : digit + $A - 10; | 105 int _codeUnitForDigit(int digit) => digit < 10 ? digit + $0 : digit + $A - 10; |
OLD | NEW |