diff options
| author | Christian Hopps <chopps@labn.net> | 2025-03-13 20:32:59 +0000 |
|---|---|---|
| committer | Christian Hopps <chopps@labn.net> | 2025-03-14 08:37:46 +0000 |
| commit | d58a8f473b50bb4f8166c1eaf929c63fae62db0a (patch) | |
| tree | 9793cb96b5ff8b31fbd3bdeb8521ba242f510812 /lib/darr.h | |
| parent | 2ab8cce2e189a4f222a21e3d59c4b49854919ee4 (diff) | |
lib: add `darr_strlen_fixup()` to update len based on NUL term
Signed-off-by: Christian Hopps <chopps@labn.net>
Diffstat (limited to 'lib/darr.h')
| -rw-r--r-- | lib/darr.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/darr.h b/lib/darr.h index 084c2a103a..4638b904d1 100644 --- a/lib/darr.h +++ b/lib/darr.h @@ -62,6 +62,7 @@ * - darr_strdup * - darr_strdup_cap * - darr_strlen + * - darr_strlen_fixup * - darr_strnul * - darr_sprintf, darr_vsprintf */ @@ -753,6 +754,22 @@ void *__darr_resize(void *a, uint count, size_t esize, struct memtype *mt); }) /** + * Fixup darr_len (and thus darr_strlen) for `S` based on its strlen(S) + * (i.e., scan for NUL byte). The dynamic array length will be set to strlen(S) + 1. + * + * Args: + * S: The dynamic array with a NUL terminated string, cannot be NULL. + * + * Return: + * The calculated strlen() value. + */ +#define darr_strlen_fixup(S) \ + ({ \ + _darr_len(S) = strlen(S) + 1; \ + darr_strlen(S); \ + }) + +/** * darr_vsprintf() - vsprintf into a new dynamic array. * * Args: |
