dtstr
Heap-allocated string formatting and concatenation helpers.
This group of functions provides formatted string construction for heap-allocated buffers. I provides printf-style formatting with explicit ownership. It also supports appending formatted text to existing allocations with optional separators. The implementation favors explicit allocation tracking via a ledger.
Mini-guide
- Allocate formatted strings by calling the formatting helpers and disposing them explicitly.
- Append formatted text to an existing allocation by using the concat helpers.
- Preserve ownership on failure by checking for NULL and retaining the original pointer.
- Pair all successful allocations with exactly one dispose call to keep the ledger accurate and avoid memory leaks.
Example
char* s = dtstr_format("value=%d", 42);
s = dtstr_concat_format(s, ", ", "status=%s", "ok");
dtstr_dispose(s);
Functions
dtstr_concat_format
Appends formatted text to an existing allocation with an optional separator.
Params:
char* existingExisting heap-allocated string to extend.
const char* separatorOptional separator inserted before the formatted suffix.
const char* formatprintf-style format string.
Return: char* Pointer to the resized allocation on success, or NULL on failure.
dtstr_concat_format_va
Appends variadic formatted text to an existing allocation with an optional separator.
Params:
char* existingExisting heap-allocated string to extend.
const char* separatorOptional separator inserted before the formatted suffix.
const char* formatprintf-style format string.
va_list argsVariadic argument list for formatting.
Return: char* Pointer to the resized allocation on success, or NULL on failure.
dtstr_dispose
Releases a string allocation created by this module and updates the ledger.
Params:
char* strHeap-allocated string to release.
Return: void No return value.
dtstr_dup
Duplicates a C string into a tracked heap allocation.
Params:
const char* strSource string to duplicate.
Return: char* Newly allocated duplicate, or NULL on failure.
dtstr_format
Formats a string into a newly allocated buffer.
Params:
const char* formatprintf-style format string.
Return: char* Newly allocated formatted string, or NULL on failure.
dtstr_format_va
Formats a string into a newly allocated buffer using a variadic list.
Params:
const char* formatprintf-style format string.
va_list argsVariadic argument list for formatting.
Return: char* Newly allocated formatted string, or NULL on failure.