| 2600 | //----------------------------------------------------------------------------- |
| 2601 | |
| 2602 | IM_MSVC_RUNTIME_CHECKS_OFF |
| 2603 | |
| 2604 | // Convert UTF-8 to 32-bit character, process single character input. |
| 2605 | // A nearly-branchless UTF-8 decoder, based on work of Christopher Wellons (https://github.com/skeeto/branchless-utf8). |
| 2606 | // We handle UTF-8 decoding error by skipping forward. |
| 2607 | int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end) |
| 2608 | { |
| 2609 | static const char lengths[32] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 4, 0 }; |
| 2610 | static const int masks[] = { 0x00, 0x7f, 0x1f, 0x0f, 0x07 }; |
| 2611 | static const uint32_t mins[] = { 0x400000, 0, 0x80, 0x800, 0x10000 }; |
| 2612 | static const int shiftc[] = { 0, 18, 12, 6, 0 }; |
| 2613 | static const int shifte[] = { 0, 6, 4, 2, 0 }; |
| 2614 | int len = lengths[*(const unsigned char*)in_text >> 3]; |
| 2615 | int wanted = len + (len ? 0 : 1); |
| 2616 | |
| 2617 | // IMPORTANT: if in_text_end == NULL it assume we have enough space! |
| 2618 | if (in_text_end == NULL) |
| 2619 | in_text_end = in_text + wanted; // Max length, nulls will be taken into account. |
| 2620 | |
| 2621 | // Copy at most 'len' bytes, stop copying at 0 or past in_text_end. Branch predictor does a good job here, |
| 2622 | // so it is fast even with excessive branching. |
| 2623 | unsigned char s[4]; |
| 2624 | s[0] = in_text + 0 < in_text_end ? in_text[0] : 0; |
| 2625 | s[1] = in_text + 1 < in_text_end ? in_text[1] : 0; |
| 2626 | s[2] = in_text + 2 < in_text_end ? in_text[2] : 0; |
| 2627 | s[3] = in_text + 3 < in_text_end ? in_text[3] : 0; |
| 2628 | |
| 2629 | // Assume a four-byte character and load four bytes. Unused bits are shifted out. |
| 2630 | *out_char = (uint32_t)(s[0] & masks[len]) << 18; |
| 2631 | *out_char |= (uint32_t)(s[1] & 0x3f) << 12; |
| 2632 | *out_char |= (uint32_t)(s[2] & 0x3f) << 6; |
| 2633 | *out_char |= (uint32_t)(s[3] & 0x3f) << 0; |
| 2634 | *out_char >>= shiftc[len]; |
| 2635 | |
| 2636 | // Accumulate the various error conditions. |
| 2637 | int e = 0; |
| 2638 | e = (*out_char < mins[len]) << 6; // non-canonical encoding |
| 2639 | e |= ((*out_char >> 11) == 0x1b) << 7; // surrogate half? |
| 2640 | e |= (*out_char > IM_UNICODE_CODEPOINT_MAX) << 8; // out of range we can store in ImWchar (FIXME: May evolve) |
| 2641 | e |= (s[1] & 0xc0) >> 2; |
| 2642 | e |= (s[2] & 0xc0) >> 4; |
| 2643 | e |= (s[3] ) >> 6; |
| 2644 | e ^= 0x2a; // top two bits of each tail byte correct? |
| 2645 | e >>= shifte[len]; |
| 2646 | |
| 2647 | if (e) |
| 2648 | { |
| 2649 | // No bytes are consumed when *in_text == 0 || in_text == in_text_end. |
| 2650 | // One byte is consumed in case of invalid first byte of in_text. |
| 2651 | // All available bytes (at most `len` bytes) are consumed on incomplete/invalid second to last bytes. |
| 2652 | // Invalid or incomplete input may consume less bytes than wanted, therefore every byte has to be inspected in s. |
| 2653 | wanted = ImMin(wanted, !!s[0] + !!s[1] + !!s[2] + !!s[3]); |
| 2654 | *out_char = IM_UNICODE_CODEPOINT_INVALID; |
| 2655 | } |
| 2656 | |
| 2657 | return wanted; |
| 2658 | } |
| 2659 |
no test coverage detected