From d562c3cfed3080013ee4fc68c7956c874f1323b8 Mon Sep 17 00:00:00 2001 From: default Date: Tue, 9 May 2023 14:18:15 +0200 Subject: [PATCH] Backport from xs. --- snac.c | 3 ++- xs_encdec.h | 33 --------------------------------- xs_unicode.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ xs_version.h | 2 +- 4 files changed, 49 insertions(+), 35 deletions(-) create mode 100644 xs_unicode.h diff --git a/snac.c b/snac.c index a5b2080..8005546 100644 --- a/snac.c +++ b/snac.c @@ -5,8 +5,9 @@ #include "xs.h" #include "xs_io.h" -#include "xs_encdec.h" +#include "xs_unicode.h" #include "xs_json.h" +#include "xs_encdec.h" #include "xs_curl.h" #include "xs_openssl.h" #include "xs_socket.h" diff --git a/xs_encdec.h b/xs_encdec.h index 2502520..d3178f4 100644 --- a/xs_encdec.h +++ b/xs_encdec.h @@ -14,7 +14,6 @@ xs_str *xs_base64_enc(const xs_val *data, int sz); xs_val *xs_base64_dec(const xs_str *data, int *size); int xs_is_base64(const char *str); - xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint); #ifdef XS_IMPLEMENTATION @@ -383,38 +382,6 @@ int xs_is_base64(const char *str) } -/** utf-8 **/ - -xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint) -/* encodes an Unicode codepoint to utf8 */ -{ - unsigned char tmp[4]; - int n = 0; - - if (cpoint < 0x80) - tmp[n++] = cpoint & 0xff; - else - if (cpoint < 0x800) { - tmp[n++] = 0xc0 | (cpoint >> 6); - tmp[n++] = 0x80 | (cpoint & 0x3f); - } - else - if (cpoint < 0x10000) { - tmp[n++] = 0xe0 | (cpoint >> 12); - tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f); - tmp[n++] = 0x80 | (cpoint & 0x3f); - } - else - if (cpoint < 0x200000) { - tmp[n++] = 0xf0 | (cpoint >> 18); - tmp[n++] = 0x80 | ((cpoint >> 12) & 0x3f); - tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f); - tmp[n++] = 0x80 | (cpoint & 0x3f); - } - - return xs_append_m(str, (char *)tmp, n); -} - #endif /* XS_IMPLEMENTATION */ #endif /* _XS_ENCDEC_H */ 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 @@ +/* copyright (c) 2022 - 2023 grunfink / MIT license */ + +#ifndef _XS_UNICODE_H + +#define _XS_UNICODE_H + + xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint); + + +#ifdef XS_IMPLEMENTATION + +/** utf-8 **/ + +xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint) +/* encodes an Unicode codepoint to utf8 */ +{ + unsigned char tmp[4]; + int n = 0; + + if (cpoint < 0x80) + tmp[n++] = cpoint & 0xff; + else + if (cpoint < 0x800) { + tmp[n++] = 0xc0 | (cpoint >> 6); + tmp[n++] = 0x80 | (cpoint & 0x3f); + } + else + if (cpoint < 0x10000) { + tmp[n++] = 0xe0 | (cpoint >> 12); + tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f); + tmp[n++] = 0x80 | (cpoint & 0x3f); + } + else + if (cpoint < 0x200000) { + tmp[n++] = 0xf0 | (cpoint >> 18); + tmp[n++] = 0x80 | ((cpoint >> 12) & 0x3f); + tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f); + tmp[n++] = 0x80 | (cpoint & 0x3f); + } + + return xs_append_m(str, (char *)tmp, n); +} + +#endif /* XS_IMPLEMENTATION */ + +#endif /* _XS_UNICODE_H */ diff --git a/xs_version.h b/xs_version.h index bc3d738..c038df3 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* b4f118990f3b8327a033a28bd9ca687c75b23dee */ +/* d8ec27efc55ba67403e88bfbe7d2ce9905364d6d */