commit d562c3c

default  ·  2023-05-09 12:18:15 +0000 UTC
parent 510477c
Backport from xs.
4 files changed,  +49, -35
M snac.c
M snac.c
+2, -1
 1@@ -5,8 +5,9 @@
 2 
 3 #include "xs.h"
 4 #include "xs_io.h"
 5-#include "xs_encdec.h"
 6+#include "xs_unicode.h"
 7 #include "xs_json.h"
 8+#include "xs_encdec.h"
 9 #include "xs_curl.h"
10 #include "xs_openssl.h"
11 #include "xs_socket.h"
+0, -33
 1@@ -14,7 +14,6 @@
 2  xs_str *xs_base64_enc(const xs_val *data, int sz);
 3  xs_val *xs_base64_dec(const xs_str *data, int *size);
 4  int xs_is_base64(const char *str);
 5- xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint);
 6 
 7 
 8 #ifdef XS_IMPLEMENTATION
 9@@ -383,38 +382,6 @@ int xs_is_base64(const char *str)
10 }
11 
12 
13-/** utf-8 **/
14-
15-xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint)
16-/* encodes an Unicode codepoint to utf8 */
17-{
18-    unsigned char tmp[4];
19-    int n = 0;
20-
21-    if (cpoint < 0x80)
22-        tmp[n++] = cpoint & 0xff;
23-    else
24-    if (cpoint < 0x800) {
25-        tmp[n++] = 0xc0 | (cpoint >> 6);
26-        tmp[n++] = 0x80 | (cpoint & 0x3f);
27-    }
28-    else
29-    if (cpoint < 0x10000) {
30-        tmp[n++] = 0xe0 | (cpoint >> 12);
31-        tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f);
32-        tmp[n++] = 0x80 | (cpoint & 0x3f);
33-    }
34-    else
35-    if (cpoint < 0x200000) {
36-        tmp[n++] = 0xf0 | (cpoint >> 18);
37-        tmp[n++] = 0x80 | ((cpoint >> 12) & 0x3f);
38-        tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f);
39-        tmp[n++] = 0x80 | (cpoint & 0x3f);
40-    }
41-
42-    return xs_append_m(str, (char *)tmp, n);
43-}
44-
45 #endif /* XS_IMPLEMENTATION */
46 
47 #endif /* _XS_ENCDEC_H */
+46, -0
 1@@ -0,0 +1,46 @@
 2+/* copyright (c) 2022 - 2023 grunfink / MIT license */
 3+
 4+#ifndef _XS_UNICODE_H
 5+
 6+#define _XS_UNICODE_H
 7+
 8+ xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint);
 9+
10+
11+#ifdef XS_IMPLEMENTATION
12+
13+/** utf-8 **/
14+
15+xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint)
16+/* encodes an Unicode codepoint to utf8 */
17+{
18+    unsigned char tmp[4];
19+    int n = 0;
20+
21+    if (cpoint < 0x80)
22+        tmp[n++] = cpoint & 0xff;
23+    else
24+    if (cpoint < 0x800) {
25+        tmp[n++] = 0xc0 | (cpoint >> 6);
26+        tmp[n++] = 0x80 | (cpoint & 0x3f);
27+    }
28+    else
29+    if (cpoint < 0x10000) {
30+        tmp[n++] = 0xe0 | (cpoint >> 12);
31+        tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f);
32+        tmp[n++] = 0x80 | (cpoint & 0x3f);
33+    }
34+    else
35+    if (cpoint < 0x200000) {
36+        tmp[n++] = 0xf0 | (cpoint >> 18);
37+        tmp[n++] = 0x80 | ((cpoint >> 12) & 0x3f);
38+        tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f);
39+        tmp[n++] = 0x80 | (cpoint & 0x3f);
40+    }
41+
42+    return xs_append_m(str, (char *)tmp, n);
43+}
44+
45+#endif /* XS_IMPLEMENTATION */
46+
47+#endif /* _XS_UNICODE_H */
+1, -1
1@@ -1 +1 @@
2-/* b4f118990f3b8327a033a28bd9ca687c75b23dee */
3+/* d8ec27efc55ba67403e88bfbe7d2ce9905364d6d */