diff -r 0f70bb04f7ba -r b0300de92b72 docs/Writerside/topics/printf.h.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/docs/Writerside/topics/printf.h.md Tue Feb 11 19:55:32 2025 +0100 @@ -0,0 +1,87 @@ +# Formatting + +In the `printf.h` header you can find various useful `printf()`-like functions that can write the formatted output +directly to an arbitrary stream or [buffer](buffer.h.md), or to memory allocated by an [allocator](allocator.h.md). + +With the help of these convenience functions, you do not need the libc `snprintf` to print your string to a temporary buffer anymore, +plus you do not need to worry about too small buffer sizes, because the functions will automatically allocate enough memory to contain the entire formatted string. + +## Print to Streams and Buffers + +```C +#include + +int cx_fprintf(void *stream, cx_write_func wfc, + const char *fmt, ...); + +int cx_vfprintf(void *stream, cx_write_func wfc, + const char *fmt, va_list ap); + +int cx_bprintf(CxBuffer *buf, const char *fmt, ...); +``` + +> TODO: document +{style="warning"} + +The `cx_vfprintf()` function is equivalent to `cx_fprintf()`, +except that instead of being called with a variable number of arguments, +they are called with an argument list as defined by ``. + +## Print to Freshly Allocated Memory + +```C +#include + +cxmutstr cx_asprintf(const char *fmt, ...); + +cxmutstr cx_asprintf_a(const CxAllocator *allocator, + const char *fmt, ...); + +cxmutstr cx_vasprintf(const char *fmt, va_list ap); + +cxmutstr cx_vasprintf_a(const CxAllocator *allocator, + const char *fmt, va_list ap); +``` + +> TODO: document +{style="warning"} + +The `cx_vasprintf()` and `cx_vasprintf_a()` functions are equivalent to `cx_asprintf()` and `cx_asprintf_a()`, +except that instead of being called with a variable number of arguments, +they are called with an argument list as defined by ``. + +## Print to Existing Memory + +```C +#include + +cx_sprintf +cx_sprintf_a +cx_sprintf_s +cx_sprintf_sa +cx_vsprintf +cx_vsprintf_a +cx_vsprintf_s +cx_vsprintf_sa +``` + +> TODO: document +{style="warning"} + +The `cx_vsprintf()`, `cx_vsprintf_a()`, `cx_vsprintf_s()`, and `cx_vsprintf_sa()` functions are equivalent +to `cx_sprintf()`, `cx_sprintf_a()`, `cx_sprintf_s()`, and `cx_sprintf_sa()`, +except that instead of being called with a variable number of arguments, +they are called with an argument list as defined by ``. + +## Small Buffer Optimization + +All functions that allocate internal temporary memory use small buffer optimization to avoid a heap allocation +if the expected string length is smaller than `cx_printf_sbo_size`. +This size cannot be changed at runtime, but modified by defining the `CX_PRINTF_SBO_SIZE` macro when [building](install.md#small-buffer-optimizations) the library. + + + +printf.h + + +