From e1e296f8ee6b11b0f7c0af961fd12b3562595c68 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 10 Sep 2023 23:13:51 -0400 Subject: WOO IT WORKS A LOT BETTER WHEN I ACTUALLY READ --- main.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index 69a3966..cf258c1 100644 --- a/main.c +++ b/main.c @@ -37,19 +37,29 @@ static void usage(int argc, char **argv, char *message, ...) { exit(1); } -static inline size_t utf8_encode(uint8_t *buf, uint16_t codepoint) { +static inline size_t utf8_encode(uint8_t *buf, uint32_t codepoint) { if ((codepoint & 0x007f) == codepoint) { - buf[0] = codepoint & 0xff; + buf[0] = codepoint & 0x007f; + return 1; } else if ((codepoint & 0x07ff) == codepoint) { - buf[0] = (codepoint & 0x07c0) >> 6; - buf[1] = codepoint & 0x003f; + buf[0] = 0xc0 | ((codepoint & 0x07c0) >> 6); + buf[1] = 0x80 | (codepoint & 0x003f); + return 2; } else if ((codepoint & 0xffff) == codepoint) { - buf[0] = (codepoint & 0xf000) >> 12; - buf[1] = (codepoint & 0x0fc0) >> 6; - buf[2] = codepoint & 0x003f; + buf[0] = 0xe0 | ((codepoint & 0xf000) >> 12); + buf[1] = 0x80 | ((codepoint & 0x0fc0) >> 6); + buf[2] = 0x80 | (codepoint & 0x003f); + return 3; + } else { + buf[0] = 0xf0 | ((codepoint & 0x1c0000) >> 18); + buf[1] = 0x80 | ((codepoint & 0x03f000) >> 12); + buf[2] = 0x80 | ((codepoint & 0x000fc0) >> 6); + buf[3] = 0x80 | (codepoint & 0x00003f); + + return 4; } } -- cgit v1.2.3