aboutsummaryrefslogtreecommitdiff
path: root/lib/lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/lib.c')
-rw-r--r--lib/lib.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 752fd0a1..c4e70dfe 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -346,6 +346,25 @@ int stridx(char *haystack, char needle)
return off-haystack;
}
+// Convert wc to utf8, returning bytes written. Does not null terminate.
+int wctoutf8(char *s, unsigned wc)
+{
+ int len = (wc>0x7ff)+(wc>0xffff), mask = 12+len+!!len;
+
+ if (wc<128) {
+ *s = wc;
+ return 1;
+ } else {
+ do {
+ s[1+len] = 0x80+(wc&0x3f);
+ wc >>= 7;
+ } while (len--);
+ *s = wc|mask;
+ }
+
+ return 2+len;
+}
+
// Convert utf8 sequence to a unicode wide character
// returns bytes consumed, or -1 if err, or -2 if need more data.
int utf8towc(wchar_t *wc, char *str, unsigned len)