--- a/docs/src/modules.md Wed May 02 18:47:22 2018 +0200 +++ b/docs/src/modules.md Wed May 02 19:16:58 2018 +0200 @@ -294,4 +294,29 @@ } ``` +### Automatic allocation for formatted strings +The UCX utility function `ucx_asprintf()` and it's convenient shortcut +`ucx_sprintf` allow easy formatting of strings, without ever having to worry +about the required space. +```C +sstr_t mystring = ucx_sprintf("The answer is: %d!", 42); +``` +Still, you have to pass `mystring.ptr` to `free()` (or the free function of +your allocator, if you use `ucx_asprintf`). +If you don't have all the information ready to build your string, you can even +use a [UcxBuffer](#buffer) as a target with the utility function +`ucx_bprintf()`. +```C +UcxBuffer* strbuffer = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND); + +for (unsigned int i = 2 ; i < 100 ; i++) { + ucx_bprintf(strbuffer, "Integer %d is %s\n", + i, prime(i) ? "prime" : "not prime"); +} + +// print the result to stdout +printf("%s", (char*)strbuffer->space); + +ucx_buffer_free(strbuffer); +```