xref: /plan9/sys/src/cmd/unix/drawterm/gui-win32/wstrtoutf.c (revision 8ccd4a6360d974db7bd7bbd4f37e7018419ea908)
1 #include <u.h>
2 #include <libc.h>
3 
4 int
wstrutflen(Rune * s)5 wstrutflen(Rune *s)
6 {
7 	int n;
8 
9 	for(n=0; *s; n+=runelen(*s),s++)
10 		;
11 	return n;
12 }
13 
14 int
wstrtoutf(char * s,Rune * t,int n)15 wstrtoutf(char *s, Rune *t, int n)
16 {
17 	int i;
18 	char *s0;
19 
20 	s0 = s;
21 	if(n <= 0)
22 		return wstrutflen(t)+1;
23 	while(*t) {
24 		if(n < UTFmax+1 && n < runelen(*t)+1) {
25 			*s = 0;
26 			return i+wstrutflen(t)+1;
27 		}
28 		i = runetochar(s, t);
29 		s += i;
30 		n -= i;
31 		t++;
32 	}
33 	*s = 0;
34 	return s-s0;
35 }
36