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 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 var byteOr = 0; | 59 var byteOr = 0; |
60 for (var i = start; i < end; i++) { | 60 for (var i = start; i < end; i++) { |
61 var byte = bytes[i]; | 61 var byte = bytes[i]; |
62 byteOr |= byte; | 62 byteOr |= byte; |
63 | 63 |
64 // If the byte is an uppercase letter, convert it to lowercase to check if | 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 | 65 // it's unreserved. This works because uppercase letters in ASCII are |
66 // exactly `0b100000 = 0x20` less than lowercase letters, so if we ensure | 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. | 67 // that that bit is 1 we ensure that the letter is lowercase. |
68 var letter = 0x20 | byte; | 68 var letter = 0x20 | byte; |
| 69 // If the byte is a digit, convert it to its value. All other byte values |
| 70 // are converted to a value greater than 9. |
| 71 // Negative values are detected elsewhere. |
| 72 var digit = 0x30 ^ byte; |
69 if ((letter >= $a && letter <= $z) || | 73 if ((letter >= $a && letter <= $z) || |
| 74 digit <= 9 || |
70 byte == $dash || | 75 byte == $dash || |
71 byte == $dot || | 76 byte == $dot || |
72 byte == $underscore || | 77 byte == $underscore || |
73 byte == $tilde) { | 78 byte == $tilde) { |
74 // Unreserved characters are safe to write as-is. | 79 // Unreserved characters are safe to write as-is. |
75 buffer.writeCharCode(byte); | 80 buffer.writeCharCode(byte); |
76 continue; | 81 continue; |
77 } | 82 } |
78 | 83 |
79 buffer.writeCharCode($percent); | 84 buffer.writeCharCode($percent); |
(...skipping 15 matching lines...) Expand all Loading... |
95 "Invalid byte ${byte < 0 ? "-" : ""}0x${byte.abs().toRadixString(16)}.", | 100 "Invalid byte ${byte < 0 ? "-" : ""}0x${byte.abs().toRadixString(16)}.", |
96 bytes, i); | 101 bytes, i); |
97 } | 102 } |
98 | 103 |
99 throw 'unreachable'; | 104 throw 'unreachable'; |
100 } | 105 } |
101 | 106 |
102 /// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit | 107 /// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit |
103 /// [digit]. | 108 /// [digit]. |
104 int _codeUnitForDigit(int digit) => digit < 10 ? digit + $0 : digit + $A - 10; | 109 int _codeUnitForDigit(int digit) => digit < 10 ? digit + $0 : digit + $A - 10; |
OLD | NEW |