1 #include <u.h> 2 #include <libc.h> 3 4 int wstrutflen(Rune * s)5wstrutflen(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)15wstrtoutf(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