summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--snac.c3
-rw-r--r--xs_encdec.h33
-rw-r--r--xs_unicode.h46
-rw-r--r--xs_version.h2
4 files changed, 49 insertions, 35 deletions
diff --git a/snac.c b/snac.c
index a5b2080..8005546 100644
--- a/snac.c
+++ b/snac.c
@@ -5,8 +5,9 @@
5 5
6#include "xs.h" 6#include "xs.h"
7#include "xs_io.h" 7#include "xs_io.h"
8#include "xs_encdec.h" 8#include "xs_unicode.h"
9#include "xs_json.h" 9#include "xs_json.h"
10#include "xs_encdec.h"
10#include "xs_curl.h" 11#include "xs_curl.h"
11#include "xs_openssl.h" 12#include "xs_openssl.h"
12#include "xs_socket.h" 13#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 @@
14 xs_str *xs_base64_enc(const xs_val *data, int sz); 14 xs_str *xs_base64_enc(const xs_val *data, int sz);
15 xs_val *xs_base64_dec(const xs_str *data, int *size); 15 xs_val *xs_base64_dec(const xs_str *data, int *size);
16 int xs_is_base64(const char *str); 16 int xs_is_base64(const char *str);
17 xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint);
18 17
19 18
20#ifdef XS_IMPLEMENTATION 19#ifdef XS_IMPLEMENTATION
@@ -383,38 +382,6 @@ int xs_is_base64(const char *str)
383} 382}
384 383
385 384
386/** utf-8 **/
387
388xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint)
389/* encodes an Unicode codepoint to utf8 */
390{
391 unsigned char tmp[4];
392 int n = 0;
393
394 if (cpoint < 0x80)
395 tmp[n++] = cpoint & 0xff;
396 else
397 if (cpoint < 0x800) {
398 tmp[n++] = 0xc0 | (cpoint >> 6);
399 tmp[n++] = 0x80 | (cpoint & 0x3f);
400 }
401 else
402 if (cpoint < 0x10000) {
403 tmp[n++] = 0xe0 | (cpoint >> 12);
404 tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f);
405 tmp[n++] = 0x80 | (cpoint & 0x3f);
406 }
407 else
408 if (cpoint < 0x200000) {
409 tmp[n++] = 0xf0 | (cpoint >> 18);
410 tmp[n++] = 0x80 | ((cpoint >> 12) & 0x3f);
411 tmp[n++] = 0x80 | ((cpoint >> 6) & 0x3f);
412 tmp[n++] = 0x80 | (cpoint & 0x3f);
413 }
414
415 return xs_append_m(str, (char *)tmp, n);
416}
417
418#endif /* XS_IMPLEMENTATION */ 385#endif /* XS_IMPLEMENTATION */
419 386
420#endif /* _XS_ENCDEC_H */ 387#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 @@
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
14xs_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 */
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 */