| 276 | |
| 277 | // The number of bytes in the UTF-8 sequence that starts with this lead byte, or zero when the byte cannot start one. That is a continuation byte (0x80 to 0xBF), and the lead bytes that are only ever invalid: 0xC0 and 0xC1 can only start an overlong encoding, and 0xF5 to 0xFF can only start a code point above U+10FFFF. |
| 278 | function utf8SequenceLength(leadByte) { |
| 279 | if (leadByte < 0x80) { |
| 280 | return 1; |
| 281 | } |
| 282 | |
| 283 | if (leadByte < 0xC2) { |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | if (leadByte < 0xE0) { |
| 288 | return 2; |
| 289 | } |
| 290 | |
| 291 | if (leadByte < 0xF0) { |
| 292 | return 3; |
| 293 | } |
| 294 | |
| 295 | return leadByte <= 0xF4 ? 4 : 0; |
| 296 | } |
| 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) { |