From 67bd0be1a4ed817954c9dcededf9bd9cb8c2f431 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 11 Oct 2020 02:59:54 -0500 Subject: toysh: more variable/wildcard plumbing and tests. --- lib/lib.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'lib/lib.c') 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) -- cgit v1.2.3