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