# HG changeset patch # User Mike Becker # Date 1525281418 -7200 # Node ID e8146a561e73165b3803d6f7e887efa1b0ecb567 # Parent 6e3c4036a80c35e3287f6428fb53e1d8deb08303 doc: adds ucx_sprintf() and ucx_bprintf() samples + fixes leftmenu diff -r 6e3c4036a80c -r e8146a561e73 docs/src/header.html --- 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 @@
  • Modules
  • API Reference
  • diff -r 6e3c4036a80c -r e8146a561e73 docs/src/modules.md --- 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); +```