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.decoder; | 5 library convert.percent.decoder; |
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 import 'package:typed_data/typed_data.dart'; | 10 import 'package:typed_data/typed_data.dart'; |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 var codeUnit = codeUnits[i]; | 192 var codeUnit = codeUnits[i]; |
193 if (codeUnits[i] != $percent) { | 193 if (codeUnits[i] != $percent) { |
194 codeUnitOr |= codeUnit; | 194 codeUnitOr |= codeUnit; |
195 continue; | 195 continue; |
196 } | 196 } |
197 | 197 |
198 // We found a %. The slice from `sliceStart` to `i` represents characters | 198 // We found a %. The slice from `sliceStart` to `i` represents characters |
199 // than can be copied to the buffer as-is. | 199 // than can be copied to the buffer as-is. |
200 if (i > sliceStart) { | 200 if (i > sliceStart) { |
201 _checkForInvalidCodeUnit(codeUnitOr, codeUnits, sliceStart, i); | 201 _checkForInvalidCodeUnit(codeUnitOr, codeUnits, sliceStart, i); |
202 buffer.addAll(codeUnits.getRange(sliceStart, i)); | 202 buffer.addAll(codeUnits, sliceStart, i); |
203 } | 203 } |
204 | 204 |
205 // Now decode the percent-encoded byte and add it as well. | 205 // Now decode the percent-encoded byte and add it as well. |
206 i++; | 206 i++; |
207 if (i >= end) return _lastPercent; | 207 if (i >= end) return _lastPercent; |
208 | 208 |
209 var firstDigit = digitForCodeUnit(codeUnits, i); | 209 var firstDigit = digitForCodeUnit(codeUnits, i); |
210 i++; | 210 i++; |
211 if (i >= end) return 16 * firstDigit; | 211 if (i >= end) return 16 * firstDigit; |
212 | 212 |
213 var secondDigit = digitForCodeUnit(codeUnits, i); | 213 var secondDigit = digitForCodeUnit(codeUnits, i); |
214 buffer.add(16 * firstDigit + secondDigit); | 214 buffer.add(16 * firstDigit + secondDigit); |
215 | 215 |
216 // The next iteration will look for non-% characters again. | 216 // The next iteration will look for non-% characters again. |
217 sliceStart = i + 1; | 217 sliceStart = i + 1; |
218 } | 218 } |
219 | 219 |
220 if (end > sliceStart) { | 220 if (end > sliceStart) { |
221 _checkForInvalidCodeUnit(codeUnitOr, codeUnits, sliceStart, end); | 221 _checkForInvalidCodeUnit(codeUnitOr, codeUnits, sliceStart, end); |
222 if (start == sliceStart) { | 222 if (start == sliceStart) { |
223 buffer.addAll(codeUnits); | 223 buffer.addAll(codeUnits); |
224 } else { | 224 } else { |
225 buffer.addAll(codeUnits.getRange(sliceStart, end)); | 225 buffer.addAll(codeUnits, sliceStart, end); |
226 } | 226 } |
227 } | 227 } |
228 | 228 |
229 return null; | 229 return null; |
230 } | 230 } |
231 | 231 |
232 void _checkForInvalidCodeUnit(int codeUnitOr, List<int> codeUnits, int start, | 232 void _checkForInvalidCodeUnit(int codeUnitOr, List<int> codeUnits, int start, |
233 int end) { | 233 int end) { |
234 if (codeUnitOr >= 0 && codeUnitOr <= 0x7f) return; | 234 if (codeUnitOr >= 0 && codeUnitOr <= 0x7f) return; |
235 | 235 |
236 for (var i = start; i < end; i++) { | 236 for (var i = start; i < end; i++) { |
237 var codeUnit = codeUnits[i]; | 237 var codeUnit = codeUnits[i]; |
238 if (codeUnit >= 0 && codeUnit <= 0x7f) continue; | 238 if (codeUnit >= 0 && codeUnit <= 0x7f) continue; |
239 throw new FormatException( | 239 throw new FormatException( |
240 "Non-ASCII code unit " | 240 "Non-ASCII code unit " |
241 "U+${codeUnit.toRadixString(16).padLeft(4, '0')}", | 241 "U+${codeUnit.toRadixString(16).padLeft(4, '0')}", |
242 codeUnits, i); | 242 codeUnits, i); |
243 } | 243 } |
244 } | 244 } |
OLD | NEW |