Skip to content

Commit

Permalink
Fix ImTextCountUtf8BytesFromChar and ImTextCharToUtf8, these APIs ass…
Browse files Browse the repository at this point in the history
…ume the input is an unicode code point, not UTF-16
  • Loading branch information
cloudwu committed May 8, 2019
1 parent fc2dff0 commit 3bedb30
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1746,11 +1746,15 @@ static inline int ImTextCharToUtf8(char* buf, int buf_size, unsigned int c)
buf[1] = (char)(0x80 + (c & 0x3f));
return 2;
}
if (c >= 0xdc00 && c < 0xe000)
if (c < 0x10000)
{
return 0;
if (buf_size < 3) return 0;
buf[0] = (char)(0xe0 + (c >> 12));
buf[1] = (char)(0x80 + ((c>> 6) & 0x3f));
buf[2] = (char)(0x80 + ((c ) & 0x3f));
return 3;
}
if (c >= 0xd800 && c < 0xdc00)
if (c <= 0x10FFFF)
{
if (buf_size < 4) return 0;
buf[0] = (char)(0xf0 + (c >> 18));
Expand All @@ -1759,14 +1763,8 @@ static inline int ImTextCharToUtf8(char* buf, int buf_size, unsigned int c)
buf[3] = (char)(0x80 + ((c ) & 0x3f));
return 4;
}
//else if (c < 0x10000)
{
if (buf_size < 3) return 0;
buf[0] = (char)(0xe0 + (c >> 12));
buf[1] = (char)(0x80 + ((c>> 6) & 0x3f));
buf[2] = (char)(0x80 + ((c ) & 0x3f));
return 3;
}
// Invalid code point, the max unicode is 0x10FFFF
return 0;
}

// Not optimal but we very rarely use this function.
Expand All @@ -1780,8 +1778,8 @@ static inline int ImTextCountUtf8BytesFromChar(unsigned int c)
{
if (c < 0x80) return 1;
if (c < 0x800) return 2;
if (c >= 0xdc00 && c < 0xe000) return 0;
if (c >= 0xd800 && c < 0xdc00) return 4;
if (c < 0x10000) return 3;
if (c <= 0x10FFFF) return 4;
return 3;
}

Expand Down

0 comments on commit 3bedb30

Please sign in to comment.