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

Side by Side Diff: packages/convert/lib/src/percent/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
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.percent.encoder;
6
7 import 'dart:convert';
8
9 import 'package:charcode/ascii.dart';
10
11 /// The canonical instance of [PercentEncoder].
12 const percentEncoder = const PercentEncoder._();
13
14 /// A converter that encodes byte arrays into percent-encoded strings.
15 ///
16 /// [encoder] encodes all bytes other than ASCII letters, decimal digits, or one
17 /// of `-._~`. This matches the behavior of [Uri.encodeQueryComponent] except
18 /// that it doesn't encode `0x20` bytes to the `+` character.
19 ///
20 /// This will throw a [RangeError] if the byte array has any digits that don't
21 /// fit in the gamut of a byte.
22 class PercentEncoder extends Converter<List<int>, String> {
23 const PercentEncoder._();
24
25 String convert(List<int> bytes) => _convert(bytes, 0, bytes.length);
26
27 ByteConversionSink startChunkedConversion(Sink<String> sink) =>
28 new _PercentEncoderSink(sink);
29 }
30
31 /// A conversion sink for chunked percentadecimal encoding.
32 class _PercentEncoderSink extends ByteConversionSinkBase {
33 /// The underlying sink to which decoded byte arrays will be passed.
34 final Sink<String> _sink;
35
36 _PercentEncoderSink(this._sink);
37
38 void add(List<int> chunk) {
39 _sink.add(_convert(chunk, 0, chunk.length));
40 }
41
42 void addSlice(List<int> chunk, int start, int end, bool isLast) {
43 RangeError.checkValidRange(start, end, chunk.length);
44 _sink.add(_convert(chunk, start, end));
45 if (isLast) _sink.close();
46 }
47
48 void close() {
49 _sink.close();
50 }
51 }
52
53 String _convert(List<int> bytes, int start, int end) {
54 var buffer = new StringBuffer();
55
56 // A bitwise OR of all bytes in [bytes]. This allows us to check for
57 // out-of-range bytes without adding more branches than necessary to the
58 // core loop.
59 var byteOr = 0;
60 for (var i = start; i < end; i++) {
61 var byte = bytes[i];
62 byteOr |= byte;
63
64 // If the byte is an uppercase letter, convert it to lowercase to check if
65 // it's unreserved. This works because uppercase letters in ASCII are
66 // exactly `0b100000 = 0x20` less than lowercase letters, so if we ensure
67 // that that bit is 1 we ensure that the letter is lowercase.
68 var letter = 0x20 | byte;
69 if ((letter >= $a && letter <= $z) ||
70 (byte >= $0 && byte <= $9) ||
71 byte == $dash ||
72 byte == $dot ||
73 byte == $underscore ||
74 byte == $tilde) {
75 // Unreserved characters are safe to write as-is.
76 buffer.writeCharCode(byte);
77 continue;
78 }
79
80 buffer.writeCharCode($percent);
81
82 // The bitwise arithmetic here is equivalent to `byte ~/ 16` and `byte % 16`
83 // for valid byte values, but is easier for dart2js to optimize given that
84 // it can't prove that [byte] will always be positive.
85 buffer.writeCharCode(_codeUnitForDigit((byte & 0xF0) >> 4));
86 buffer.writeCharCode(_codeUnitForDigit(byte & 0x0F));
87 }
88
89 if (byteOr >= 0 && byteOr <= 255) return buffer.toString();
90
91 // If there was an invalid byte, find it and throw an exception.
92 for (var i = start; i < end; i++) {
93 var byte = bytes[i];
94 if (byte >= 0 && byte <= 0xff) continue;
95 throw new FormatException(
96 "Invalid byte ${byte < 0 ? "-" : ""}0x${byte.abs().toRadixString(16)}.",
97 bytes, i);
98 }
99
100 throw 'unreachable';
101 }
102
103 /// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit
104 /// [digit].
105 int _codeUnitForDigit(int digit) => digit < 10 ? digit + $0 : digit + $A - 10;
OLDNEW
« no previous file with comments | « packages/convert/lib/src/percent/decoder.dart ('k') | packages/convert/lib/src/string_accumulator_sink.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698