summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main.c24
1 files 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;
}
}