-
Notifications
You must be signed in to change notification settings - Fork 14
/
printf.c
340 lines (317 loc) · 6.69 KB
/
printf.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
#include <kernel/printf.h>
#include <kernel/console.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
typedef void (*fmt_putchar_t)(int c, void *priv);
enum format_flags {
FMT_LONG = 1UL << 0,
FMT_LONG_LONG = 1UL << 1,
FMT_ZERO_PAD = 1UL << 2,
FMT_LEFT = 1UL << 3,
FMT_UNSIGNED = 1UL << 4,
FMT_PRECISION = 1UL << 5,
FMT_WIDTH = 1UL << 6,
};
struct format_state {
enum format_flags flags;
size_t width;
};
static const char number_chars_upcase[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
};
static const char number_chars[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
};
static char *format_number(char *buf, uint64_t n, size_t len, const char *chars, uint64_t base, bool sign)
{
bool negative = (int64_t)n < 0;
if (sign && negative) {
n = -n;
}
size_t pos = len;
buf[--pos] = 0;
while (n >= base) {
int digit = n % base;
n /= base;
buf[--pos] = chars[digit];
}
buf[--pos] = chars[n];
if (sign && negative) {
buf[--pos] = '-';
}
return &buf[pos];
}
static void format_str(const char *buf, size_t len, struct format_state *fmt, fmt_putchar_t putchar, void *priv)
{
size_t length = len;
size_t padding = 0;
if (fmt->flags & FMT_WIDTH) {
if (length > fmt->width) {
length = fmt->width;
}
padding = fmt->width - length;
}
if (fmt->flags & FMT_LEFT) {
while (length > 0) {
char c = *buf++;
if (!c) {
break;
}
putchar(c, priv);
length--;
}
while (padding > 0) {
putchar(' ', priv);
padding--;
}
} else {
char pad = fmt->flags & FMT_ZERO_PAD ? '0' : ' ';
while (padding > 0) {
putchar(pad, priv);
padding--;
}
while (length > 0) {
char c = *buf++;
if (!c) {
break;
}
putchar(c, priv);
length--;
}
}
}
static uint64_t pop_int(va_list ap, struct format_state *fmt)
{
if (fmt->flags & FMT_UNSIGNED) {
if (fmt->flags & FMT_LONG_LONG) {
return va_arg(ap, unsigned long long);
} else if (fmt->flags & FMT_LONG) {
return va_arg(ap, unsigned long);
} else {
return va_arg(ap, unsigned int);
}
} else {
if (fmt->flags & FMT_LONG_LONG) {
return va_arg(ap, long long);
}
if (fmt->flags & FMT_LONG) {
return va_arg(ap, long);
}
return va_arg(ap, int);
}
}
static const char *format(const char *fmt, va_list ap, fmt_putchar_t putchar, void *priv)
{
struct format_state fmt_state = {
.flags = 0,
.width = 0,
};
for (;;) {
int c = *fmt++;
switch (c) {
/* Field width and precision: */
case '0' ... '9': {
/* Precision is ignored */
if (!(fmt_state.flags & FMT_PRECISION)) {
fmt_state.flags |= FMT_WIDTH;
if (fmt_state.width == 0 && c == '0') {
fmt_state.flags |= FMT_ZERO_PAD;
}
fmt_state.width *= 10;
fmt_state.width += c - '0';
}
break;
}
case '.': {
fmt_state.flags |= FMT_PRECISION;
fmt_state.flags |= FMT_ZERO_PAD;
break;
}
/* Flags: */
case '-': {
fmt_state.flags |= FMT_LEFT;
break;
}
case 'l': {
if (fmt_state.flags & FMT_LONG) {
fmt_state.flags |= FMT_LONG_LONG;
}
fmt_state.flags |= FMT_LONG;
break;
}
/* Conversion specifiers: */
case 'd': {
char buffer[32];
uint64_t d = pop_int(ap, &fmt_state);
const char *s = format_number(buffer, d, ARRAY_SIZE(buffer), number_chars, 10, true);
format_str(s, strlen(s), &fmt_state, putchar, priv);
goto out;
}
case 'u': {
fmt_state.flags |= FMT_UNSIGNED;
char buffer[32];
uint64_t d = pop_int(ap, &fmt_state);
const char *s = format_number(buffer, d, ARRAY_SIZE(buffer), number_chars, 10, false);
format_str(s, strlen(s), &fmt_state, putchar, priv);
goto out;
}
case 'x': {
fmt_state.flags |= FMT_UNSIGNED;
char buffer[32];
uint64_t d = pop_int(ap, &fmt_state);
const char *s = format_number(buffer, d, ARRAY_SIZE(buffer), number_chars, 16, false);
format_str(s, strlen(s), &fmt_state, putchar, priv);
goto out;
}
case 'X': {
fmt_state.flags |= FMT_UNSIGNED;
char buffer[32];
uint64_t d = pop_int(ap, &fmt_state);
const char *s = format_number(buffer, d, ARRAY_SIZE(buffer), number_chars_upcase, 16, false);
format_str(s, strlen(s), &fmt_state, putchar, priv);
goto out;
}
case 'c': {
int ch = va_arg(ap, int);
putchar(ch, priv);
goto out;
}
case 's': {
const char *s = va_arg(ap, const char *);
if (!s) {
s = "<null>";
}
format_str(s, strlen(s), &fmt_state, putchar, priv);
goto out;
}
case 'p': {
putchar('0', priv);
putchar('x', priv);
fmt_state.flags |= FMT_UNSIGNED;
char buffer[32];
void *p = va_arg(ap, void *);
const char *s =
format_number(buffer, (unsigned long)p, ARRAY_SIZE(buffer), number_chars, 16, false);
format_str(s, strlen(s), &fmt_state, putchar, priv);
goto out;
}
case '%': {
putchar('%', priv);
goto out;
}
/* Unknown format specifier: */
default:
putchar('%', priv);
putchar(c, priv);
goto out;
}
}
out:
return fmt;
}
struct vprintf_state {
size_t pos;
};
static void vprintf_output(int c, void *priv)
{
struct vprintf_state *state = priv;
putchar(c);
state->pos++;
}
int vprintf(const char *fmt, va_list ap)
{
struct vprintf_state state = {
.pos = 0,
};
for (;;) {
char c = *fmt++;
if (c == '\0') {
break;
}
if (c != '%') {
putchar(c);
continue;
}
fmt = format(fmt, ap, vprintf_output, &state);
}
return state.pos;
}
int printf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int ret = vprintf(fmt, ap);
va_end(ap);
return ret;
}
int putchar(int c)
{
console_write_char(c);
return c;
}
int puts(const char *s)
{
console_write_str(s);
return 1;
}
struct vsprintf_state {
char *str;
size_t pos;
size_t size;
};
static void vsprintf_output(int c, void *priv)
{
struct vsprintf_state *state = priv;
if (state->pos < state->size) {
state->str[state->pos] = c;
}
state->pos++;
}
int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
{
struct vsprintf_state state = {
.str = str,
.pos = 0,
.size = size,
};
for (;;) {
char c = *fmt++;
if (c == '\0') {
break;
}
if (c != '%') {
vsprintf_output(c, &state);
continue;
}
fmt = format(fmt, ap, vsprintf_output, &state);
}
if (state.pos > state.size) {
str[state.size - 1] = '\0';
} else {
str[state.pos] = '\0';
}
return state.pos;
}
int vsprintf(char *str, const char *fmt, va_list ap)
{
return vsnprintf(str, SIZE_MAX, fmt, ap);
}
int sprintf(char *str, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int ret = vsprintf(str, fmt, ap);
va_end(ap);
return ret;
}
int snprintf(char *str, size_t size, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int ret = vsnprintf(str, size, fmt, ap);
va_end(ap);
return ret;
}