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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
use sys
use "alloc.use"
use "die.use"
use "types.use"
use "utf.use"
use "syswrap.use"
use "varargs.use"
use "extremum.use"
use "chartype.use"
use "fltfmt.use"
/*
printf-like functions. These use a different syntax from the C printf,
as described below:
%s - A string, ie, a utf8 encoded byte slice.
%t - A boolean
%b - A byte.
%w - A 16 bit integer
%i - A 32 bit integer
%l - A 64 bit integer
%z - A size
%p - A pointer
%c - A char
*/
pkg std =
const put : (fmt : byte[:], args : ... -> size)
const fput : (fd : fd, fmt : byte[:], args : ... -> size)
const putv : (fmt : byte[:], ap : valist -> size)
const fputv : (fd : fd, fmt : byte[:], ap : valist -> size)
const fmt : (fmt : byte[:], args : ... -> byte[:])
const fmtv : (fmt : byte[:], ap : valist -> byte[:])
const bfmt : (buf : byte[:], fmt : byte[:], args : ... -> size)
const bfmtv : (buf : byte[:], fmt : byte[:], ap : valist -> size)
$noret const fatal : (status : int, fmt : byte[:], args : ... -> void)
$noret const fatalv : (status : int, fmt : byte[:], ap : valist -> void)
;;
/* Writes a string of text up to 2 kb in size to stdout */
const put = {fmt, args
-> fputv(1, fmt, vastart(&args))
}
const fput = {fd, fmt, args
-> fputv(fd, fmt, vastart(&args))
}
const putv = {fmt, ap
-> fputv(1, fmt, ap)
}
/* Writes a string of text up to 2kb long to stdout, using a valist
as the source of the arguments */
const fputv = {fd, fmt, ap
var buf : byte[2048]
var n
n = bfmtv(buf[:], fmt, ap)
write(fd, buf[:n])
-> n
}
/* same as 'put', but exits the program after printing */
const fatal = {status, fmt, args
putv(fmt, vastart(&args))
exit(status)
}
/* same as 'putv', but exits the program after printing */
const fatalv = {status, fmt, ap
putv(fmt, ap)
exit(status)
}
/* formats a string, allocating the slice. FIXME: calculate the
size needed. */
const fmt = {fmt, args
-> fmtv(fmt, vastart(&args))
}
/* formats a string, allocating the slice. FIXME: calculate the
size needed. Takes a valist as it's last argument. */
const fmtv = {fmt, ap
var buf
var sz
buf = slalloc(2048)
sz = bfmtv(buf, fmt, ap)
-> buf[:sz]
}
/* formats a string of text as specified by 'fmt' into 'buf' */
const bfmt = {buf, fmt, args
-> bfmtv(buf, fmt, vastart(&args))
}
const digitchars = [
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
]
generic intfmt = {buf : byte[:], bits : @a::(integral,numeric), base, signed, padto, padfill
var isneg
var val
var b : char[32]
var i, j, n, npad
n = 0
i = 0
if signed && bits < 0
val = -bits castto(uint64)
isneg = true
else
val = bits castto(uint64)
val &= ~0 >> (8*(sizeof(uint64)-sizeof(@a)))
isneg = false
;;
if val == 0
b[0] = '0'
i++
;;
while val != 0
b[i] = digitchars[val % base]
val /= base
i++
;;
npad = clamp(padto - i, 0, padto)
n = 0
if isneg
npad--
;;
if padfill == '0' && isneg && n < buf.len
n += encode(buf[n:], '-')
;;
for j = 0; j < min(npad, buf.len); j++
if n >= buf.len
break
;;
n += encode(buf[n:], padfill)
;;
if padfill != '0' && isneg && n < buf.len
n += encode(buf[n:], '-')
;;
for j = i; j != 0; j--
if n >= buf.len
break
;;
n += encode(buf[n:], b[j - 1])
;;
-> n
}
/* formats a string of text as specified by 'fmt' into 'buf',
using a valist for the arguments */
const bfmtv = {buf, fmt, ap
var b
var c
var n
var padto
var base
var signed, padfill
var s_val : byte[:]
var t_val : bool
var b_val : int8, ub_val : uint8
var w_val : int16, uw_val : uint16
var i_val : int32, ui_val : uint32
var l_val : int64, ul_val : uint64
var z_val : size
var p_val : byte#
var c_val : char
var f_val : flt64, F_val : flt32
n = 0
while fmt.len != 0
(c, fmt) = striter(fmt)
if c == '%'
base = 10
padto = 0
signed = true
padfill = ' '
(c, fmt) = striter(fmt)
/* modifiers */
while fmt.len > 0
match c
| 'x':
(c, fmt) = striter(fmt)
base = 16
signed = false
| 'u':
(c, fmt) = striter(fmt)
signed = false
| '0':
(c, fmt) = striter(fmt)
padfill = '0'
| _: /* nothing */
;;
if isdigit(c) && padto == 0
/*
We can't get a 0 on the first iteration, since
that was matched above. So, no special checks
for nonzero on the first iteration.
*/
padto = 0
while isdigit(c)
padto = padto*10 + charval(c, 10)
(c, fmt) = striter(fmt)
;;
else
break
;;
;;
/* format specifiers */
match c
| 's':
(s_val, ap) = vanext(ap)
n += strfmt(buf[n:], s_val, padto, padfill)
| 't':
(t_val, ap) = vanext(ap)
n += boolfmt(buf[n:], t_val, padto, padfill)
| 'f':
(f_val, ap) = vanext(ap)
b = buf[n:]
/* FIXME(ori): bug, b[n:].len fails since b[n:] isn't an lval */
n += flt64bfmt(buf[n:], f_val, 0, b.len)
/* FIXME: flt casts are currently broken
| 'F':
(F_val, ap) = vanext(ap)
n += fltfmt(buf[n:], F_val castto(flt64))
*/
/* format integers */
| 'b':
if signed
(b_val, ap) = vanext(ap)
n += intfmt(buf[n:], b_val, base, signed, padto, padfill)
else
(ub_val, ap) = vanext(ap)
n += intfmt(buf[n:], ub_val, base, signed, padto, padfill)
;;
| 'w':
if signed
(w_val, ap) = vanext(ap)
n += intfmt(buf[n:], w_val, base, signed, padto, padfill)
else
(uw_val, ap) = vanext(ap)
n += intfmt(buf[n:], uw_val, base, signed, padto, padfill)
;;
| 'i':
if signed
(i_val, ap) = vanext(ap)
n += intfmt(buf[n:], i_val, base, signed, padto, padfill)
else
(ui_val, ap) = vanext(ap)
n += intfmt(buf[n:], ui_val, base, signed, padto, padfill)
;;
| 'l':
if signed
(l_val, ap) = vanext(ap)
n += intfmt(buf[n:], l_val, base, signed, padto, padfill)
else
(ul_val, ap) = vanext(ap)
n += intfmt(buf[n:], ul_val, base, signed, padto, padfill)
;;
| 'z':
(z_val, ap) = vanext(ap)
n += intfmt(buf[n:], z_val castto(int64), base, signed, padto, padfill)
| 'p':
(p_val, ap) = vanext(ap)
n += intfmt(buf[n:], p_val castto(int64), 16, false, padto, padfill)
| 'c':
(c_val, ap) = vanext(ap)
n += encode(buf[n:], c_val)
| '%':
n += encode(buf[n:], '%')
| _:
die("Unknown format specifier\n")
;;
else
n += encode(buf[n:], c)
;;
;;
-> n
}
const strfmt = {buf, str, padto, padfill
var i, n, npad
n = 0
npad = clamp(padto - str.len, 0, padto)
for i = 0; i < padto - str.len; i++
n += encode(buf[n:], padfill)
;;
for i = 0; i < min(str.len, buf.len); i++
buf[n++] = str[i]
;;
-> n
}
const boolfmt = {buf, val, padto, padfill
var s
if val
s = "true"
else
s = "false"
;;
-> strfmt(buf, s, padto, padfill)
}
|