return UCS-4 code of the specified UTF-8 sequence, or 0 for invalid UTF-8, set endptr after the sequence
| 427 | |
| 428 | // return UCS-4 code of the specified UTF-8 sequence, or 0 for invalid UTF-8, set endptr after the sequence |
| 429 | uint32_t Screen::wchar(const char *ptr, const char **endptr) |
| 430 | { |
| 431 | uint32_t c1, c2, c3, c4; |
| 432 | |
| 433 | c1 = static_cast<unsigned char>(*ptr++); |
| 434 | |
| 435 | if (c1 <= 0x7f) |
| 436 | { |
| 437 | if (endptr != NULL) |
| 438 | *endptr = ptr; |
| 439 | return c1; |
| 440 | } |
| 441 | |
| 442 | if (c1 < 0xc2 || c1 > 0xf4) |
| 443 | { |
| 444 | if (endptr != NULL) |
| 445 | *endptr = ptr; |
| 446 | return 0; // incomplete or invalid UTF-8 |
| 447 | } |
| 448 | |
| 449 | c2 = static_cast<unsigned char>(*ptr++); |
| 450 | |
| 451 | if ((c2 & 0xc0) != 0x80 || (c1 == 0xed && c2 > 0x9f)) |
| 452 | { |
| 453 | if (endptr != NULL) |
| 454 | *endptr = ptr; |
| 455 | return 0; // incomplete UTF-8 or surrogates |
| 456 | } |
| 457 | |
| 458 | c2 &= 0x3f; |
| 459 | |
| 460 | if (c1 < 0xe0) |
| 461 | { |
| 462 | if (endptr != NULL) |
| 463 | *endptr = ptr; |
| 464 | return (((c1 & 0x1f) << 6) | c2); |
| 465 | } |
| 466 | |
| 467 | c3 = static_cast<unsigned char>(*ptr++); |
| 468 | |
| 469 | if ((c3 & 0xc0) != 0x80) |
| 470 | { |
| 471 | if (endptr != NULL) |
| 472 | *endptr = ptr; |
| 473 | return 0; // incomplete UTF-8 |
| 474 | } |
| 475 | |
| 476 | c3 &= 0x3f; |
| 477 | |
| 478 | if (c1 < 0xf0) |
| 479 | { |
| 480 | if (endptr != NULL) |
| 481 | *endptr = ptr; |
| 482 | return (((c1 & 0x0f) << 12) | (c2 << 6) | c3); |
| 483 | } |
| 484 | |
| 485 | c4 = static_cast<unsigned char>(*ptr++); |
| 486 |
nothing calls this directly
no outgoing calls
no test coverage detected