-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmt.c
454 lines (415 loc) · 11.1 KB
/
fmt.c
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <float.h>
#include <ctype.h>
#include <math.h>
#include "assert.h"
#include "except.h"
#include "fmt.h"
#include "mem.h"
#define T Fmt_T
struct buf {
char *buf;
char *bp;
int size;
};
#define pad(n, c) do { \
int nn = (n); \
while (nn-- > 0) \
put((c), cl); \
} while (0)
static void cvt_s(int code, va_list *app,
int put(int c, void *cl), void *cl,
unsigned char flags[], int width,
int precision)
{
char *str = va_arg(*app, char *);
assert(str);
Fmt_puts(str, strlen(str), put, cl, flags,
width, precision);
}
static void cvt_d(int code, va_list *app,
int put(int c, void *cl), void *cl,
unsigned char flags[], int width,
int precision)
{
int val = va_arg(*app, int);
unsigned m;
char buf[43];
char *p = buf + sizeof(buf);
if (val == INT_MIN)
m = INT_MAX + 1U;
else if (val < 0)
m = -val;
else
m = val;
do
*--p = m % 10 + '0';
while ((m /= 10) > 0);
if (val < 0)
*--p = '-';
Fmt_putd(p, (buf + sizeof(buf)) - p, put, cl, flags,
width, precision);
}
static void cvt_u(int code, va_list *app,
int put(int c, void *cl), void *cl,
unsigned char flags[], int width,
int precision)
{
unsigned m = va_arg(*app, unsigned);
char buf[43];
char *p = buf + sizeof(buf);
do
*--p = m % 10 + '0';
while ((m /= 10) > 0);
Fmt_putd(p, (buf + sizeof(buf)) - p, put, cl, flags,
width, precision);
}
static void cvt_o(int code, va_list *app,
int put(int c, void *cl), void *cl,
unsigned char flags[], int width,
int precision)
{
unsigned m = va_arg(*app, unsigned);
char buf[43];
char *p = buf + sizeof(buf);
do
*--p = (m & 0x7) + '0';
while ((m >>= 3) != 0);
Fmt_putd(p, (buf + sizeof(buf)) - p, put, cl, flags,
width, precision);
}
static void cvt_p(int code, va_list *app,
int put(int c, void *cl), void *cl,
unsigned char flags[], int width,
int precision)
{
unsigned long m = (unsigned long)va_arg(*app, void *);
char buf[43];
char *p = buf + sizeof(buf);
precision = INT_MIN;
do
*--p = "0123456789abcdef"[m & 0xf];
while ((m >>= 4) != 0);
Fmt_putd(p, (buf + sizeof(buf)) - p, put, cl, flags,
width, precision);
}
static void cvt_c(int code, va_list *app,
int put(int c, void *cl), void *cl,
unsigned char flags[], int width,
int precision)
{
if (width == INT_MIN)
width = 0;
if (width < 0) {
flags['-'] = 1;
width = -width;
}
if (!flags['-'])
pad(width - 1, ' ');
put((unsigned char)va_arg(*app, int), cl);
if (flags['-'])
pad(width - 1, ' ');
}
static void cvt_x(int code, va_list *app,
int put(int c, void *cl), void *cl,
unsigned char flags[], int width,
int precision)
{
unsigned m = va_arg(*app, unsigned);
char buf[43];
char *p = buf + sizeof(buf);
do
*--p = "0123456789abcdef"[m & 0xf];
while ((m >>= 4) != 0);
Fmt_putd(p, (buf + sizeof(buf)) - p, put, cl, flags,
width, precision);
}
static void cvt_f(int code, va_list *app,
int put(int c, void *cl), void *cl,
unsigned char flags[], int width,
int precision)
{
char buf[DBL_MAX_10_EXP + 1 + 1 + 99 + 1];
if (precision < 0)
precision = 6;
if (code == 'g' && precision == 0)
precision = 1;
{
static char fmt[] = "%.dd?";
assert(precision <= 99);
fmt[4] = code;
fmt[3] = precision % 10 + '0';
fmt[2] = (precision / 10) % 10 + '0';
sprintf(buf, fmt, va_arg(*app, double));
}
Fmt_putd(buf, strlen(buf), put, cl, flags,
width, precision);
}
const Except_T Fmt_Overflow = { "Formatting Overflow"};
char *Fmt_flags = "-+ 0";
static T cvt[256] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, cvt_c, cvt_d, cvt_f, cvt_f, cvt_f,
0, 0, 0, 0, 0, 0, 0, cvt_o,
cvt_p, 0, 0, cvt_s, 0, cvt_u, 0, 0,
cvt_p, 0, 0, cvt_s, 0, cvt_u, 0, 0,
cvt_x, 0, 0, 0, 0, 0, 0, 0
};
static int outc(int c, void *cl)
{
FILE *f = cl;
return putc(c, f);
}
static int insert(int c, void *cl)
{
struct buf *p = cl;
if (p->bp >= p->buf + p->size)
RAISE(Fmt_Overflow);
*p->bp++ = c;
return c;
}
static int append(int c, void *cl)
{
struct buf *p = cl;
if (p->bp >= p->buf + p->size) {
RESIZE(p->buf, 2 * p->size);
p->bp = p->buf + p->size;
p->size *= 2;
}
*p->bp++ = c;
return c;
}
void Fmt_puts(const char *str, int len,
int put(int c, void *cl), void *cl,
unsigned char flags[], int width, int precision)
{
assert(str);
assert(len >= 0);
assert(flags);
if (width == INT_MIN)
width = 0;
if (width < 0) {
flags['-'] = 1;
width = -width;
}
if (precision >= 0)
flags['0'] = 0;
if (precision >= 0 && precision < len)
len = precision;
if (!flags['-'])
pad(width - len, ' ');
{
int i;
for (i = 0; i < len; i++)
put((unsigned char)*str++, cl);
}
if (flags['-'])
pad(width - len, ' ');
}
void Fmt_fmt(int put(int c, void *), void *cl,
const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
Fmt_vfmt(put, cl, fmt, ap);
va_end(ap);
}
void Fmt_print(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
Fmt_vfmt(outc, stdout, fmt, ap);
va_end(ap);
}
void Fmt_fprint(FILE *stream, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
Fmt_vfmt(outc, stream, fmt, ap);
va_end(ap);
}
int Fmt_sfmt(char *buf, int size, const char *fmt, ...)
{
va_list ap;
int len;
va_start(ap, fmt);
len = Fmt_vsfmt(buf, size, fmt, ap);
va_end(ap);
return len;
}
int Fmt_vsfmt(char *buf, int size, const char *fmt,
va_list ap)
{
struct buf cl;
assert(buf);
assert(size > 0);
assert(fmt);
cl.buf = cl.bp = buf;
cl.size = size;
Fmt_vfmt(insert, &cl, fmt, ap);
insert(0, &cl);
return cl.bp - cl.buf - 1;
}
char *Fmt_string(const char *fmt, ...)
{
char *str;
va_list ap;
assert(fmt);
va_start(ap, fmt);
str = Fmt_vstring(fmt, ap);
va_end(ap);
return str;
}
char *Fmt_vstring(const char *fmt, va_list ap)
{
struct buf cl;
assert(fmt);
cl.size = 256;
cl.buf = cl.bp = ALLOC(cl.size);
Fmt_vfmt(append, &cl, fmt, ap);
append(0, &cl);
return RESIZE(cl.buf, cl.bp - cl.buf);
}
T Fmt_register(int code, T newcvt)
{
T old;
assert(0 < code &&
code < (int)(sizeof(cvt) / sizeof(cvt[0])));
old = cvt[code];
cvt[code] = newcvt;
return old;
}
void Fmt_vfmt(int put(int c, void *cl), void *cl,
const char *fmt, va_list ap)
{
assert(put);
assert(fmt);
while (*fmt) {
/* ^% or %% */
if (*fmt != '%' || *++fmt == '%')
put((unsigned char)*fmt++, cl);
else {//Get here *++fmt == '%' has been executed.
unsigned char c, flags[256];
int width = INT_MIN, precision = INT_MIN;
memset(flags, '\0', sizeof(flags));
/* get optional flags */
if (Fmt_flags) {
unsigned char c = *fmt;
for (; c && strchr(Fmt_flags, c); c = *++fmt) {
assert(flags[c] < 255);
flags[c]++;
}
}
/* get optional field width */
if (*fmt == '*' || isdigit(*fmt)) {
int n;
if (*fmt == '*') {
n = va_arg(ap, int);
assert(n != INT_MIN);
fmt++;
} else {
for (n = 0; isdigit(*fmt); fmt++) {
int d = *fmt - '0';
/* overflow in case */
assert(n <= (INT_MAX - d) / 10);
n = 10 * n + d;
}
}
width = n;
}
/* get optional precision */
if (*fmt == '.' && (*++fmt == '*' || isdigit(*fmt))) {
/* Get here the *++fmt has been executed */
int n;
if (*fmt == '*') {
n = va_arg(ap, int);
assert(n != INT_MIN);
fmt++;
} else {
for (n = 0; isdigit(*fmt); fmt++) {
int d = *fmt - '0';
assert(n <= (INT_MAX - d) / 10);
n = 10 * n + d;
}
}
precision = n;
}
c = *fmt++;
assert(cvt[c]);
(*cvt[c])(c, &ap, put, cl, flags, width, precision);
}
}
}
void Fmt_putd(const char *str, int len,
int put(int c, void *cl), void *cl,
unsigned char flags[], int width, int precision)
{
int sign;
assert(str);
assert(len >= 0);
assert(flags);
if (width == INT_MIN)
width = 0;
if (width < 0) {
flags['-'] = 1;
width = -width;
}
if (precision >= 0)
flags['0'] = 0;
if (len > 0 && (*str == '-' || *str == '+')) {
sign = *str++;
len--;
} else if (flags['+'])
sign = '+';
else if (flags[' '])
sign = ' ';
else
sign = 0;
{
int n;
if (precision < 0)
precision = 1;
if (len < precision)
n = precision;
else if (precision == 0 && len == 1 && str[0] == '0')
n = 0;
else
n = len;
if (sign)
n++;
if (flags['-']) {
if (sign)
put(sign, cl);
} else if (flags['0']) {
if (sign)
put(sign, cl);
pad(width - n, '0');
} else {
pad(width - n, ' ');
if (sign)
put(sign, cl);
}
pad(precision - len, '0');
{
int i;
for (i = 0; i < len; i++)
put((unsigned char)*str++, cl);
}
if (flags['-'])
pad(width - n, ' ');
}
}