mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-09 07:13:43 -05:00
Factor out encode_utf8 from luni_send into utils.c.
I knew there had to already be a UTF-8 encoder _somewhere_ in this code base, but it took me a while to find it! Now it's reusable in other contexts.
This commit is contained in:
22
utils.c
22
utils.c
@ -958,3 +958,25 @@ bool strendswith(const char *s, const char *t)
|
||||
size_t slen = strlen(s), tlen = strlen(t);
|
||||
return slen >= tlen && !strcmp(s + (slen - tlen), t);
|
||||
}
|
||||
|
||||
size_t encode_utf8(void *output, unsigned long ch)
|
||||
{
|
||||
unsigned char *start = (unsigned char *)output, *p = start;
|
||||
|
||||
if (ch < 0x80) {
|
||||
*p++ = ch;
|
||||
} else if (ch < 0x800) {
|
||||
*p++ = 0xC0 | (ch >> 6);
|
||||
*p++ = 0x80 | (ch & 0x3F);
|
||||
} else if (ch < 0x10000) {
|
||||
*p++ = 0xE0 | (ch >> 12);
|
||||
*p++ = 0x80 | ((ch >> 6) & 0x3F);
|
||||
*p++ = 0x80 | (ch & 0x3F);
|
||||
} else {
|
||||
*p++ = 0xF0 | (ch >> 18);
|
||||
*p++ = 0x80 | ((ch >> 12) & 0x3F);
|
||||
*p++ = 0x80 | ((ch >> 6) & 0x3F);
|
||||
*p++ = 0x80 | (ch & 0x3F);
|
||||
}
|
||||
return p - start;
|
||||
}
|
||||
|
Reference in New Issue
Block a user