mirror of
https://github.com/tailix/libkernaux.git
synced 2026-08-15 11:00:07 -04:00
Main: add example "printf_fmt"
This commit is contained in:
parent
841d6991e7
commit
f34b66479a
4 changed files with 72 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -96,6 +96,7 @@
|
||||||
/examples/panic_simple
|
/examples/panic_simple
|
||||||
/examples/pfa
|
/examples/pfa
|
||||||
/examples/printf
|
/examples/printf
|
||||||
|
/examples/printf_fmt
|
||||||
/examples/printf_va
|
/examples/printf_va
|
||||||
/examples/snprintf
|
/examples/snprintf
|
||||||
/examples/snprintf_va
|
/examples/snprintf_va
|
||||||
|
|
|
||||||
|
|
@ -140,6 +140,9 @@ endif
|
||||||
|
|
||||||
if WITH_PRINTF_FMT
|
if WITH_PRINTF_FMT
|
||||||
libkernaux_a_SOURCES += src/printf_fmt.c
|
libkernaux_a_SOURCES += src/printf_fmt.c
|
||||||
|
if ENABLE_TESTS
|
||||||
|
TESTS += examples/printf_fmt
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if WITH_UNITS
|
if WITH_UNITS
|
||||||
|
|
@ -179,6 +182,10 @@ examples_printf_SOURCES = \
|
||||||
$(libkernaux_a_SOURCES) \
|
$(libkernaux_a_SOURCES) \
|
||||||
examples/printf.c
|
examples/printf.c
|
||||||
|
|
||||||
|
examples_printf_fmt_SOURCES = \
|
||||||
|
$(libkernaux_a_SOURCES) \
|
||||||
|
examples/printf_fmt.c
|
||||||
|
|
||||||
examples_printf_va_SOURCES = \
|
examples_printf_va_SOURCES = \
|
||||||
$(libkernaux_a_SOURCES) \
|
$(libkernaux_a_SOURCES) \
|
||||||
examples/printf_va.c
|
examples/printf_va.c
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,7 @@ zero). Work-in-progress APIs can change at any time.
|
||||||
* [To human](/examples/units_human.c)
|
* [To human](/examples/units_human.c)
|
||||||
* [printf format parser](/include/kernaux/printf_fmt.h)
|
* [printf format parser](/include/kernaux/printf_fmt.h)
|
||||||
* Code from [https://github.com/mpaland/printf](https://github.com/mpaland/printf). Thank you!
|
* Code from [https://github.com/mpaland/printf](https://github.com/mpaland/printf). Thank you!
|
||||||
|
* [Example](/examples/printf_fmt.c)
|
||||||
* Usual functions
|
* Usual functions
|
||||||
* [libc replacement](/include/kernaux/libc.h) (*stable since* **0.1.0**)
|
* [libc replacement](/include/kernaux/libc.h) (*stable since* **0.1.0**)
|
||||||
* [itoa/ftoa replacement](/include/kernaux/ntoa.h) (*stable since* **0.1.0**)
|
* [itoa/ftoa replacement](/include/kernaux/ntoa.h) (*stable since* **0.1.0**)
|
||||||
|
|
|
||||||
63
examples/printf_fmt.c
Normal file
63
examples/printf_fmt.c
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
#include <kernaux/printf_fmt.h>
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
const char *format = "012.34f";
|
||||||
|
|
||||||
|
struct KernAux_PrintfFmt_Spec spec = KernAux_PrintfFmt_Spec_create();
|
||||||
|
KernAux_PrintfFmt_Spec_eval_flags(&spec, &format);
|
||||||
|
if (KernAux_PrintfFmt_Spec_eval_width1(&spec, &format)) {
|
||||||
|
KernAux_PrintfFmt_Spec_eval_width2(&spec, &format, 0);
|
||||||
|
}
|
||||||
|
if (KernAux_PrintfFmt_Spec_eval_precision1(&spec, &format)) {
|
||||||
|
KernAux_PrintfFmt_Spec_eval_precision2(&spec, &format, 0);
|
||||||
|
}
|
||||||
|
KernAux_PrintfFmt_Spec_eval_length(&spec, &format);
|
||||||
|
KernAux_PrintfFmt_Spec_eval_type(&spec, &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);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const char *format = " *.*ld";
|
||||||
|
|
||||||
|
struct KernAux_PrintfFmt_Spec spec = KernAux_PrintfFmt_Spec_create();
|
||||||
|
KernAux_PrintfFmt_Spec_eval_flags(&spec, &format);
|
||||||
|
if (KernAux_PrintfFmt_Spec_eval_width1(&spec, &format)) {
|
||||||
|
KernAux_PrintfFmt_Spec_eval_width2(&spec, &format, 12);
|
||||||
|
}
|
||||||
|
if (KernAux_PrintfFmt_Spec_eval_precision1(&spec, &format)) {
|
||||||
|
KernAux_PrintfFmt_Spec_eval_precision2(&spec, &format, 34);
|
||||||
|
}
|
||||||
|
KernAux_PrintfFmt_Spec_eval_length(&spec, &format);
|
||||||
|
KernAux_PrintfFmt_Spec_eval_type(&spec, &format);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue