xref: /inferno-os/appl/lib/convcs/utf8_btos.b (revision 9b29ac7ea714507a9c0690620c02c8ca5ab25f90)
1implement Btos;
2
3include "sys.m";
4include "convcs.m";
5
6sys : Sys;
7
8init(nil : string) : string
9{
10	sys = load Sys Sys->PATH;
11	return nil;
12}
13
14btos(nil : Convcs->State, b : array of byte, n : int) : (Convcs->State, string, int)
15{
16	nbytes := 0;
17	str := "";
18
19	if (n == -1) {
20		# gather as much as possible
21		nbytes = sys->utfbytes(b, len b);
22		if (nbytes > 0)
23			str = string b[:nbytes];
24	} else {
25		for (; nbytes < len b && len str < n;) {
26			(ch, l, nil) := sys->byte2char(b, nbytes);
27			if (l <= 0)
28				break;
29			str[len str] = ch;
30			nbytes += l;
31		}
32	}
33	return (nil, str, nbytes);
34}
35