1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2024-11-13 11:04:27 -05:00
libkernaux/examples/printf_fmt.c

93 lines
2.7 KiB
C
Raw Normal View History

2022-05-26 19:17:29 -04:00
#include <kernaux/printf_fmt.h>
#include <assert.h>
void example_main()
2022-05-26 19:17:29 -04:00
{
{
const char *format = "s";
struct KernAux_PrintfFmt_Spec spec = KernAux_PrintfFmt_Spec_create();
format = KernAux_PrintfFmt_Spec_parse(&spec, 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.flags == 0);
assert(spec.width == 0);
assert(spec.precision == 0);
assert(spec.type == KERNAUX_PRINTF_FMT_TYPE_STR);
assert(spec.base == 0);
}
2022-05-26 19:17:29 -04:00
{
const char *format = "012.34f";
struct KernAux_PrintfFmt_Spec spec = KernAux_PrintfFmt_Spec_create();
// Parsing of each part may be done separately.
KernAux_PrintfFmt_Spec_parse_flags(&spec, &format);
KernAux_PrintfFmt_Spec_parse_width(&spec, &format);
KernAux_PrintfFmt_Spec_parse_precision(&spec, &format);
KernAux_PrintfFmt_Spec_parse_length(&spec, &format);
KernAux_PrintfFmt_Spec_parse_type(&spec, &format);
if (spec.set_width) {
// Actually this line won't be executed.
KernAux_PrintfFmt_Spec_set_width(&spec, 0);
2022-05-26 19:17:29 -04:00
}
if (spec.set_precision) {
// Actually this line won't be executed.
KernAux_PrintfFmt_Spec_set_precision(&spec, 0);
2022-05-26 19:17:29 -04:00
}
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);
}
{
const char *const format = " *.*ld";
2022-05-26 19:17:29 -04:00
struct KernAux_PrintfFmt_Spec spec = KernAux_PrintfFmt_Spec_create();
// Returning value may be ignored.
KernAux_PrintfFmt_Spec_parse(&spec, format);
if (spec.set_width) {
KernAux_PrintfFmt_Spec_set_width(&spec, 12);
2022-05-26 19:17:29 -04:00
}
if (spec.set_precision) {
KernAux_PrintfFmt_Spec_set_precision(&spec, 34);
2022-05-26 19:17:29 -04:00
}
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);
}
}