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