Convert UTF-8 to 32-bit character, process single character input. A nearly-branchless UTF-8 decoder, based on work of Christopher Wellons (https://github.com/skeeto/branchless-utf8). We handle UTF-8 decoding error by skipping forward.
| 1962 | // A nearly-branchless UTF-8 decoder, based on work of Christopher Wellons (https://github.com/skeeto/branchless-utf8). |
| 1963 | // We handle UTF-8 decoding error by skipping forward. |
| 1964 | int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end) |
| 1965 | { |
| 1966 | 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 }; |
| 1967 | static const int masks[] = { 0x00, 0x7f, 0x1f, 0x0f, 0x07 }; |
| 1968 | static const uint32_t mins[] = { 0x400000, 0, 0x80, 0x800, 0x10000 }; |
| 1969 | static const int shiftc[] = { 0, 18, 12, 6, 0 }; |
| 1970 | static const int shifte[] = { 0, 6, 4, 2, 0 }; |
| 1971 | int len = lengths[*(const unsigned char*)in_text >> 3]; |
| 1972 | int wanted = len + !len; |
| 1973 | |
| 1974 | if (in_text_end == NULL) |
| 1975 | in_text_end = in_text + wanted; // Max length, nulls will be taken into account. |
| 1976 | |
| 1977 | // Copy at most 'len' bytes, stop copying at 0 or past in_text_end. Branch predictor does a good job here, |
| 1978 | // so it is fast even with excessive branching. |
| 1979 | unsigned char s[4]; |
| 1980 | s[0] = in_text + 0 < in_text_end ? in_text[0] : 0; |
| 1981 | s[1] = in_text + 1 < in_text_end ? in_text[1] : 0; |
| 1982 | s[2] = in_text + 2 < in_text_end ? in_text[2] : 0; |
| 1983 | s[3] = in_text + 3 < in_text_end ? in_text[3] : 0; |
| 1984 | |
| 1985 | // Assume a four-byte character and load four bytes. Unused bits are shifted out. |
| 1986 | *out_char = (uint32_t)(s[0] & masks[len]) << 18; |
| 1987 | *out_char |= (uint32_t)(s[1] & 0x3f) << 12; |
| 1988 | *out_char |= (uint32_t)(s[2] & 0x3f) << 6; |
| 1989 | *out_char |= (uint32_t)(s[3] & 0x3f) << 0; |
| 1990 | *out_char >>= shiftc[len]; |
| 1991 | |
| 1992 | // Accumulate the various error conditions. |
| 1993 | int e = 0; |
| 1994 | e = (*out_char < mins[len]) << 6; // non-canonical encoding |
| 1995 | e |= ((*out_char >> 11) == 0x1b) << 7; // surrogate half? |
| 1996 | e |= (*out_char > IM_UNICODE_CODEPOINT_MAX) << 8; // out of range? |
| 1997 | e |= (s[1] & 0xc0) >> 2; |
| 1998 | e |= (s[2] & 0xc0) >> 4; |
| 1999 | e |= (s[3] ) >> 6; |
| 2000 | e ^= 0x2a; // top two bits of each tail byte correct? |
| 2001 | e >>= shifte[len]; |
| 2002 | |
| 2003 | if (e) |
| 2004 | { |
| 2005 | // No bytes are consumed when *in_text == 0 || in_text == in_text_end. |
| 2006 | // One byte is consumed in case of invalid first byte of in_text. |
| 2007 | // All available bytes (at most `len` bytes) are consumed on incomplete/invalid second to last bytes. |
| 2008 | // Invalid or incomplete input may consume less bytes than wanted, therefore every byte has to be inspected in s. |
| 2009 | wanted = ImMin(wanted, !!s[0] + !!s[1] + !!s[2] + !!s[3]); |
| 2010 | *out_char = IM_UNICODE_CODEPOINT_INVALID; |
| 2011 | } |
| 2012 | |
| 2013 | return wanted; |
| 2014 | } |
| 2015 | |
| 2016 | int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_text_remaining) |
| 2017 | { |
no test coverage detected