| 359 | static constexpr const offset_pt invalid_pt = { -1, 0 }; |
| 360 | |
| 361 | static offset_pt utf8_decode_check(const std::string & str, std::string::size_type i) { |
| 362 | uint32_t b0, b1, b2, b3; |
| 363 | |
| 364 | b0 = static_cast<unsigned char>(str[i]); |
| 365 | |
| 366 | if (b0 < 0x80) { |
| 367 | // 1-byte character |
| 368 | return { 1, b0 }; |
| 369 | } else if (b0 < 0xC0) { |
| 370 | // Unexpected continuation byte |
| 371 | return invalid_pt; |
| 372 | } else if (b0 < 0xE0) { |
| 373 | // 2-byte character |
| 374 | if (((b1 = str[i+1]) & 0xC0) != 0x80) |
| 375 | return invalid_pt; |
| 376 | |
| 377 | char32_t pt = (b0 & 0x1F) << 6 | (b1 & 0x3F); |
| 378 | if (pt < 0x80) |
| 379 | return invalid_pt; |
| 380 | |
| 381 | return { 2, pt }; |
| 382 | } else if (b0 < 0xF0) { |
| 383 | // 3-byte character |
| 384 | if (((b1 = str[i+1]) & 0xC0) != 0x80) |
| 385 | return invalid_pt; |
| 386 | if (((b2 = str[i+2]) & 0xC0) != 0x80) |
| 387 | return invalid_pt; |
| 388 | |
| 389 | char32_t pt = (b0 & 0x0F) << 12 | (b1 & 0x3F) << 6 | (b2 & 0x3F); |
| 390 | if (pt < 0x800) |
| 391 | return invalid_pt; |
| 392 | |
| 393 | return { 3, pt }; |
| 394 | } else if (b0 < 0xF8) { |
| 395 | // 4-byte character |
| 396 | if (((b1 = str[i+1]) & 0xC0) != 0x80) |
| 397 | return invalid_pt; |
| 398 | if (((b2 = str[i+2]) & 0xC0) != 0x80) |
| 399 | return invalid_pt; |
| 400 | if (((b3 = str[i+3]) & 0xC0) != 0x80) |
| 401 | return invalid_pt; |
| 402 | |
| 403 | char32_t pt = (b0 & 0x0F) << 18 | (b1 & 0x3F) << 12 |
| 404 | | (b2 & 0x3F) << 6 | (b3 & 0x3F); |
| 405 | if (pt < 0x10000 || pt >= 0x110000) |
| 406 | return invalid_pt; |
| 407 | |
| 408 | return { 4, pt }; |
| 409 | } else { |
| 410 | // Codepoint out of range |
| 411 | return invalid_pt; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | static char32_t utf8_decode(const std::string & str, std::string::size_type & i) { |
| 416 | offset_pt res = utf8_decode_check(str, i); |