MCPcopy Create free account
hub / github.com/danoon2/Boxedwine / ImTextCharFromUtf8

Function ImTextCharFromUtf8

lib/imgui/imgui.cpp:1554–1610  ·  view source on GitHub ↗

Convert UTF-8 to 32-bit character, process single character input. Based on stb_from_utf8() from github.com/nothings/stb/ We handle UTF-8 decoding error by skipping forward.

Source from the content-addressed store, hash-verified

1552// Based on stb_from_utf8() from github.com/nothings/stb/
1553// We handle UTF-8 decoding error by skipping forward.
1554int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end)
1555{
1556 unsigned int c = (unsigned int)-1;
1557 const unsigned char* str = (const unsigned char*)in_text;
1558 if (!(*str & 0x80))
1559 {
1560 c = (unsigned int)(*str++);
1561 *out_char = c;
1562 return 1;
1563 }
1564 if ((*str & 0xe0) == 0xc0)
1565 {
1566 *out_char = IM_UNICODE_CODEPOINT_INVALID; // will be invalid but not end of string
1567 if (in_text_end && in_text_end - (const char*)str < 2) return 1;
1568 if (*str < 0xc2) return 2;
1569 c = (unsigned int)((*str++ & 0x1f) << 6);
1570 if ((*str & 0xc0) != 0x80) return 2;
1571 c += (*str++ & 0x3f);
1572 *out_char = c;
1573 return 2;
1574 }
1575 if ((*str & 0xf0) == 0xe0)
1576 {
1577 *out_char = IM_UNICODE_CODEPOINT_INVALID; // will be invalid but not end of string
1578 if (in_text_end && in_text_end - (const char*)str < 3) return 1;
1579 if (*str == 0xe0 && (str[1] < 0xa0 || str[1] > 0xbf)) return 3;
1580 if (*str == 0xed && str[1] > 0x9f) return 3; // str[1] < 0x80 is checked below
1581 c = (unsigned int)((*str++ & 0x0f) << 12);
1582 if ((*str & 0xc0) != 0x80) return 3;
1583 c += (unsigned int)((*str++ & 0x3f) << 6);
1584 if ((*str & 0xc0) != 0x80) return 3;
1585 c += (*str++ & 0x3f);
1586 *out_char = c;
1587 return 3;
1588 }
1589 if ((*str & 0xf8) == 0xf0)
1590 {
1591 *out_char = IM_UNICODE_CODEPOINT_INVALID; // will be invalid but not end of string
1592 if (in_text_end && in_text_end - (const char*)str < 4) return 1;
1593 if (*str > 0xf4) return 4;
1594 if (*str == 0xf0 && (str[1] < 0x90 || str[1] > 0xbf)) return 4;
1595 if (*str == 0xf4 && str[1] > 0x8f) return 4; // str[1] < 0x80 is checked below
1596 c = (unsigned int)((*str++ & 0x07) << 18);
1597 if ((*str & 0xc0) != 0x80) return 4;
1598 c += (unsigned int)((*str++ & 0x3f) << 12);
1599 if ((*str & 0xc0) != 0x80) return 4;
1600 c += (unsigned int)((*str++ & 0x3f) << 6);
1601 if ((*str & 0xc0) != 0x80) return 4;
1602 c += (*str++ & 0x3f);
1603 // utf-8 encodings of values used in surrogate pairs are invalid
1604 if ((c & 0xFFFFF800) == 0xD800) return 4;
1605 *out_char = c;
1606 return 4;
1607 }
1608 *out_char = 0;
1609 return 0;
1610}
1611

Callers 9

ImTextStrFromUtf8Function · 0.85
ImTextCountCharsFromUtf8Function · 0.85
AddTextMethod · 0.85
CalcWordWrapPositionAMethod · 0.85
CalcTextSizeAMethod · 0.85
RenderTextMethod · 0.85
InputTextExMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected