Revert "Replace UTF-8 decoder with branchless version by Christopher Wellons." (#3558)

This reverts commit b3576dd354.
This commit is contained in:
ocornut 2020-10-25 16:27:32 +01:00
parent b3576dd354
commit b7530e5d04
2 changed files with 57 additions and 53 deletions

View File

@ -59,9 +59,6 @@ Other Changes:
- Tab Bar: Do not display a tooltip if the name already fits over a given tab. (#3521) - Tab Bar: Do not display a tooltip if the name already fits over a given tab. (#3521)
- Drag and Drop: Fix drag and drop to tie same-size drop targets by choosen the later one. Fixes dragging - Drag and Drop: Fix drag and drop to tie same-size drop targets by choosen the later one. Fixes dragging
into a full-window-sized dockspace inside a zero-padded window. (#3519, #2717) [@Black-Cat] into a full-window-sized dockspace inside a zero-padded window. (#3519, #2717) [@Black-Cat]
- Misc: Replaced UTF-8 decoder by branchless one by Christopher Wellons (30~40% faster). [@rokups]
Super minor fix handling incomplete UTF-8 contents: if input does not contain enough bytes, decoder
returns IM_UNICODE_CODEPOINT_INVALID and consume remaining bytes (vs old decoded consumed only 1 byte).
- Backends: OpenGL3: Use glGetString(GL_VERSION) query instead of glGetIntegerv(GL_MAJOR_VERSION, ...) - Backends: OpenGL3: Use glGetString(GL_VERSION) query instead of glGetIntegerv(GL_MAJOR_VERSION, ...)
when the later returns zero (e.g. Desktop GL 2.x). (#3530) [@xndcn] when the later returns zero (e.g. Desktop GL 2.x). (#3530) [@xndcn]
- Backends: OpenGL3: Backup and restore GL_PRIMITIVE_RESTART state. (#3544) [@Xipiryon] - Backends: OpenGL3: Backup and restore GL_PRIMITIVE_RESTART state. (#3544) [@Xipiryon]

107
imgui.cpp
View File

@ -1553,59 +1553,66 @@ void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_f
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Convert UTF-8 to 32-bit character, process single character input. // Convert UTF-8 to 32-bit character, process single character input.
// Based on work of Christopher Wellons (https://github.com/skeeto/branchless-utf8) // Based on stb_from_utf8() from github.com/nothings/stb/
// We handle UTF-8 decoding error by skipping forward. // We handle UTF-8 decoding error by skipping forward.
int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end) int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end)
{ {
static const char lengths[] = { 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 }; unsigned int c = (unsigned int)-1;
static const int masks[] = { 0x00, 0x7f, 0x1f, 0x0f, 0x07 }; const unsigned char* str = (const unsigned char*)in_text;
static const uint32_t mins[] = { 4194304, 0, 128, 2048, 65536 }; if (!(*str & 0x80))
static const int shiftc[] = { 0, 18, 12, 6, 0 }; {
static const int shifte[] = { 0, 6, 4, 2, 0 }; c = (unsigned int)(*str++);
unsigned char s[4]; *out_char = c;
int len = lengths[*(const unsigned char*)in_text >> 3]; return 1;
}
if (in_text_end == NULL) if ((*str & 0xe0) == 0xc0)
in_text_end = in_text + len + !len; // Max length, nulls will be taken into account. {
*out_char = IM_UNICODE_CODEPOINT_INVALID; // will be invalid but not end of string
// Copy at most 'len' bytes, stop copying at 0 or past in_text_end. if (in_text_end && in_text_end - (const char*)str < 2) return 1;
s[0] = in_text + 0 < in_text_end ? in_text[0] : 0; if (*str < 0xc2) return 2;
s[1] = s[0] && in_text + 1 < in_text_end ? in_text[1] : 0; c = (unsigned int)((*str++ & 0x1f) << 6);
s[2] = s[1] && in_text + 2 < in_text_end ? in_text[2] : 0; if ((*str & 0xc0) != 0x80) return 2;
s[3] = s[2] && in_text + 3 < in_text_end ? in_text[3] : 0; c += (*str++ & 0x3f);
*out_char = c;
// Compute the pointer to the next character early so that the next return 2;
// iteration can start working on the next character. Neither Clang }
// nor GCC figure out this reordering on their own. if ((*str & 0xf0) == 0xe0)
//const unsigned char *next = s + len + !len; {
*out_char = IM_UNICODE_CODEPOINT_INVALID; // will be invalid but not end of string
// No bytes are consumed when *in_text == 0 || in_text == in_text_end. if (in_text_end && in_text_end - (const char*)str < 3) return 1;
// One byte is consumed in case of invalid first byte of in_text. if (*str == 0xe0 && (str[1] < 0xa0 || str[1] > 0xbf)) return 3;
// All available bytes (at most `len` bytes) are consumed on incomplete/invalid second to last bytes. if (*str == 0xed && str[1] > 0x9f) return 3; // str[1] < 0x80 is checked below
int consumed = !!s[0] + !!s[1] + !!s[2] + !!s[3]; c = (unsigned int)((*str++ & 0x0f) << 12);
if ((*str & 0xc0) != 0x80) return 3;
// Assume a four-byte character and load four bytes. Unused bits are shifted out. c += (unsigned int)((*str++ & 0x3f) << 6);
*out_char = (uint32_t)(s[0] & masks[len]) << 18; if ((*str & 0xc0) != 0x80) return 3;
*out_char |= (uint32_t)(s[1] & 0x3f) << 12; c += (*str++ & 0x3f);
*out_char |= (uint32_t)(s[2] & 0x3f) << 6; *out_char = c;
*out_char |= (uint32_t)(s[3] & 0x3f) << 0; return 3;
*out_char >>= shiftc[len]; }
if ((*str & 0xf8) == 0xf0)
// Accumulate the various error conditions. {
int e = 0; *out_char = IM_UNICODE_CODEPOINT_INVALID; // will be invalid but not end of string
e = (*out_char < mins[len]) << 6; // non-canonical encoding if (in_text_end && in_text_end - (const char*)str < 4) return 1;
e |= ((*out_char >> 11) == 0x1b) << 7; // surrogate half? if (*str > 0xf4) return 4;
e |= (*out_char > IM_UNICODE_CODEPOINT_MAX) << 8; // out of range? if (*str == 0xf0 && (str[1] < 0x90 || str[1] > 0xbf)) return 4;
e |= (s[1] & 0xc0) >> 2; if (*str == 0xf4 && str[1] > 0x8f) return 4; // str[1] < 0x80 is checked below
e |= (s[2] & 0xc0) >> 4; c = (unsigned int)((*str++ & 0x07) << 18);
e |= (s[3] ) >> 6; if ((*str & 0xc0) != 0x80) return 4;
e ^= 0x2a; // top two bits of each tail byte correct? c += (unsigned int)((*str++ & 0x3f) << 12);
e >>= shifte[len]; if ((*str & 0xc0) != 0x80) return 4;
c += (unsigned int)((*str++ & 0x3f) << 6);
if (e) if ((*str & 0xc0) != 0x80) return 4;
*out_char = IM_UNICODE_CODEPOINT_INVALID; c += (*str++ & 0x3f);
// utf-8 encodings of values used in surrogate pairs are invalid
return consumed; if ((c & 0xFFFFF800) == 0xD800) return 4;
// If codepoint does not fit in ImWchar, use replacement character U+FFFD instead
if (c > IM_UNICODE_CODEPOINT_MAX) c = IM_UNICODE_CODEPOINT_INVALID;
*out_char = c;
return 4;
}
*out_char = 0;
return 0;
} }
int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_text_remaining) int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_text_remaining)