Wed, 02 May 2018 19:16:58 +0200
doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu
docs/src/header.html | file | annotate | diff | comparison | revisions | |
docs/src/modules.md | file | annotate | diff | comparison | revisions |
--- a/docs/src/header.html Wed May 02 18:47:22 2018 +0200 +++ b/docs/src/header.html Wed May 02 19:16:58 2018 +0200 @@ -21,17 +21,17 @@ <li><a href="modules.html">Modules</a> <ul> <li><a href="modules.html#allocator">Allocator</a></li> - <li><a href="modules.html#avl">AVL Tree</a></li> + <li><a href="modules.html#avl-tree">AVL Tree</a></li> <li><a href="modules.html#buffer">Buffer</a></li> <li><a href="modules.html#list">List</a></li> <li><a href="modules.html#logging">Logging</a></li> <li><a href="modules.html#map">Map</a></li> - <li><a href="modules.html#mempool">Memory Pool</a></li> + <li><a href="modules.html#memory-pool">Memory Pool</a></li> <li><a href="modules.html#properties">Properties</a></li> <li><a href="modules.html#stack">Stack</a></li> <li><a href="modules.html#string">String</a></li> - <li><a href="modules.html#test">Testing</a></li> - <li><a href="modules.html#utils">Utilities</a></li> + <li><a href="modules.html#testing">Testing</a></li> + <li><a href="modules.html#utilities">Utilities</a></li> </ul> </li> <li><a target="_blank" href="api/index.html">API Reference</a></li>
--- 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); +```