| 297 | |
| 298 | // Whether the second byte of a UTF-8 sequence is valid for this lead byte. Any later byte only has to be a continuation byte. The range is narrower for the lead bytes that would otherwise allow an overlong encoding, a surrogate, or a code point above U+10FFFF. |
| 299 | function isValidSecondByte(leadByte, byte) { |
| 300 | if (byte < 0x80 || byte > 0xBF) { |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | switch (leadByte) { |
| 305 | case 0xE0: { |
| 306 | return byte >= 0xA0; |
| 307 | } |
| 308 | |
| 309 | case 0xED: { |
| 310 | return byte <= 0x9F; |
| 311 | } |
| 312 | |
| 313 | case 0xF0: { |
| 314 | return byte >= 0x90; |
| 315 | } |
| 316 | |
| 317 | case 0xF4: { |
| 318 | return byte <= 0x8F; |
| 319 | } |
| 320 | |
| 321 | default: { |
| 322 | return true; |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | // Decode the UTF-8 sequence that starts with the percent escape at `position`. The decoded text and the position just past the sequence are returned. |
| 328 | function decodeSequence(value, position) { |