aboutsummaryrefslogtreecommitdiff
path: root/lib/lib.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2020-10-11 02:59:54 -0500
committerRob Landley <rob@landley.net>2020-10-11 02:59:54 -0500
commit67bd0be1a4ed817954c9dcededf9bd9cb8c2f431 (patch)
tree4df0aa1044af467469e77c757bbf58dffe2f6178 /lib/lib.c
parent0f2658c806586190be3aca21826e77fff9e50f1b (diff)
downloadtoybox-67bd0be1a4ed817954c9dcededf9bd9cb8c2f431.tar.gz
toysh: more variable/wildcard plumbing and tests.
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)