Port mouse event encoding code to C

This commit is contained in:
Kovid Goyal
2017-09-14 16:44:00 +05:30
parent b9ac13c379
commit 43c4c71abd
5 changed files with 98 additions and 63 deletions

View File

@@ -237,3 +237,30 @@ decode_utf8(uint32_t* state, uint32_t* codep, uint8_t byte) {
*state = utf8_data[256 + *state*16 + type];
return *state;
}
unsigned int
encode_utf8(uint32_t ch, char* dest) {
if (ch < 0x80) {
dest[0] = (char)ch;
return 1;
}
if (ch < 0x800) {
dest[0] = (ch>>6) | 0xC0;
dest[1] = (ch & 0x3F) | 0x80;
return 2;
}
if (ch < 0x10000) {
dest[0] = (ch>>12) | 0xE0;
dest[1] = ((ch>>6) & 0x3F) | 0x80;
dest[2] = (ch & 0x3F) | 0x80;
return 3;
}
if (ch < 0x110000) {
dest[0] = (ch>>18) | 0xF0;
dest[1] = ((ch>>12) & 0x3F) | 0x80;
dest[2] = ((ch>>6) & 0x3F) | 0x80;
dest[3] = (ch & 0x3F) | 0x80;
return 4;
}
return 0;
}