diff options
| author | 2023-05-09 14:18:15 +0200 | |
|---|---|---|
| committer | 2023-05-09 14:18:15 +0200 | |
| commit | d562c3cfed3080013ee4fc68c7956c874f1323b8 (patch) | |
| tree | c547b250935c38beb42aa27358f6cd30f186f313 /xs_unicode.h | |
| parent | Updated documentation. (diff) | |
| download | snac2-d562c3cfed3080013ee4fc68c7956c874f1323b8.tar.gz snac2-d562c3cfed3080013ee4fc68c7956c874f1323b8.tar.xz snac2-d562c3cfed3080013ee4fc68c7956c874f1323b8.zip | |
Backport from xs.
Diffstat (limited to 'xs_unicode.h')
| -rw-r--r-- | xs_unicode.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/xs_unicode.h b/xs_unicode.h new file mode 100644 index 0000000..6f78d58 --- /dev/null +++ b/xs_unicode.h | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /* copyright (c) 2022 - 2023 grunfink / MIT license */ | ||
| 2 | |||
| 3 | #ifndef _XS_UNICODE_H | ||
| 4 | |||
| 5 | #define _XS_UNICODE_H | ||
| 6 | |||
| 7 | xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint); | ||
| 8 | |||
| 9 | |||
| 10 | #ifdef XS_IMPLEMENTATION | ||
| 11 | |||
| 12 | /** utf-8 **/ | ||
| 13 | |||
| 14 | xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint) | ||
| 15 | /* encodes an Unicode codepoint to utf8 */ | ||
| 16 | { | ||
| 17 | unsigned char tmp[4]; | ||
| 18 | int n = 0; | ||
| 19 | |||
| 20 | if (cpoint < 0x80) | ||
| 21 | tmp[n++] = cpoint & 0xff; | ||
| 22 | else | ||
| 23 | if (cpoint < 0x800) { | ||
| 24 | tmp[n++] = 0xc0 | (cpoint >> 6); | ||
| 25 | tmp[n++] = 0x80 | (cpoint & 0x3f); | ||
| 26 | } | ||
| 27 | else | ||
| 28 | if (cpoint < 0x10000) { | ||
| 29 | tmp[n++] = 0xe0 | (cpoint >> 12); | ||
| 30 | tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f); | ||
| 31 | tmp[n++] = 0x80 | (cpoint & 0x3f); | ||
| 32 | } | ||
| 33 | else | ||
| 34 | if (cpoint < 0x200000) { | ||
| 35 | tmp[n++] = 0xf0 | (cpoint >> 18); | ||
| 36 | tmp[n++] = 0x80 | ((cpoint >> 12) & 0x3f); | ||
| 37 | tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f); | ||
| 38 | tmp[n++] = 0x80 | (cpoint & 0x3f); | ||
| 39 | } | ||
| 40 | |||
| 41 | return xs_append_m(str, (char *)tmp, n); | ||
| 42 | } | ||
| 43 | |||
| 44 | #endif /* XS_IMPLEMENTATION */ | ||
| 45 | |||
| 46 | #endif /* _XS_UNICODE_H */ | ||