-
Notifications
You must be signed in to change notification settings - Fork 2
/
printf_fmt.c
101 lines (82 loc) · 2.8 KB
/
printf_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
#include <kernaux/printf_fmt.h>
#include <assert.h>
#include <stddef.h>
void example_main()
{
{
const char *const format = "s";
struct KernAux_PrintfFmt_Spec spec = KernAux_PrintfFmt_Spec_create(format);
if (spec.set_width) {
// Actually this line won't be executed.
KernAux_PrintfFmt_Spec_set_width(&spec, 0);
}
if (spec.set_precision) {
// Actually this line won't be executed.
KernAux_PrintfFmt_Spec_set_precision(&spec, 0);
}
assert(spec.format_start == format);
assert(spec.format_limit == &format[1]);
assert(spec.flags == 0);
assert(spec.width == 0);
assert(spec.precision == 0);
assert(spec.type == KERNAUX_PRINTF_FMT_TYPE_STR);
assert(spec.base == 0);
assert(!spec.set_width);
assert(!spec.set_precision);
}
{
const char *format = "012.34f";
struct KernAux_PrintfFmt_Spec spec = KernAux_PrintfFmt_Spec_create_out(&format);
if (spec.set_width) {
// Actually this line won't be executed.
KernAux_PrintfFmt_Spec_set_width(&spec, 0);
}
if (spec.set_precision) {
// Actually this line won't be executed.
KernAux_PrintfFmt_Spec_set_precision(&spec, 0);
}
assert(spec.format_start == &format[-7]);
assert(spec.format_limit == format);
assert(
spec.flags ==
(
KERNAUX_PRINTF_FMT_FLAGS_ZEROPAD |
KERNAUX_PRINTF_FMT_FLAGS_PRECISION
)
);
assert(spec.width == 12);
assert(spec.precision == 34);
assert(spec.type == KERNAUX_PRINTF_FMT_TYPE_FLOAT);
assert(spec.base == 0);
assert(!spec.set_width);
assert(!spec.set_precision);
}
{
const char *const format = " *.*ld";
const char *new_format = NULL;
struct KernAux_PrintfFmt_Spec spec = KernAux_PrintfFmt_Spec_create_out_new(format, &new_format);
if (spec.set_width) {
KernAux_PrintfFmt_Spec_set_width(&spec, 12);
}
if (spec.set_precision) {
KernAux_PrintfFmt_Spec_set_precision(&spec, 34);
}
assert(spec.format_start == format);
assert(spec.format_limit == &format[6]);
assert(new_format == &format[6]);
assert(
spec.flags ==
(
KERNAUX_PRINTF_FMT_FLAGS_SPACE |
KERNAUX_PRINTF_FMT_FLAGS_LONG |
KERNAUX_PRINTF_FMT_FLAGS_PRECISION
)
);
assert(spec.width == 12);
assert(spec.precision == 34);
assert(spec.type == KERNAUX_PRINTF_FMT_TYPE_INT);
assert(spec.base == 10);
assert(spec.set_width);
assert(spec.set_precision);
}
}