diff --git a/include/ucore/dstr.h b/include/ucore/dstr.h index 77e462d..a003983 100644 --- a/include/ucore/dstr.h +++ b/include/ucore/dstr.h @@ -45,12 +45,16 @@ void uc_dstr_destroy(struct DStr *str); */ size_t uc_dstr_available(const struct DStr *str); -/** Ensure the DStr is of at least 'len' bytes, +/** Ensure the DStr have room for least 'len' bytes, * allocating room if needed. * + * This includes the length of the existing string, + * to ensure e.g an additional 10 bytes can be appended, + * one would do uc_dstr_ensure(str, str->len + 10) + * * @return 0 on success (!= 0 if an allocation fail) */ -int uc_dstr_ensure(struct DStr *str, size_t len); +int uc_dstr_ensure(struct DStr *str, size_t total); /** Reset the DStr, setting its length to 0 */ diff --git a/src/dstr.c b/src/dstr.c index 59d4cbc..d0ea6a6 100644 --- a/src/dstr.c +++ b/src/dstr.c @@ -22,13 +22,13 @@ size_t uc_dstr_available(const struct DStr *str) return str->allocated - str->len; } -int uc_dstr_ensure(struct DStr *str, size_t len) +int uc_dstr_ensure(struct DStr *str, size_t total) { - if (str->allocated < len) { + if (str->allocated < total) { //allocate a minimum no. of bytes, and one //additional for the common case of nul terminating //the string - size_t new_len = UC_MAX(7U, len) + 1; + size_t new_len = UC_MAX(7U, total) + 1; char *new_str = realloc(str->str, new_len); if (new_str == NULL) {