From 4ad724391daeff5b86ad420f1e1a8b35d65fd7e0 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 29 Jun 2023 13:52:39 -0400 Subject: dstring: Allow dstreserve(NULL, n) --- src/dstring.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/dstring.c') diff --git a/src/dstring.c b/src/dstring.c index 7ca74d0..f019ba9 100644 --- a/src/dstring.c +++ b/src/dstring.c @@ -3,6 +3,7 @@ #include "dstring.h" #include "alloc.h" +#include "bit.h" #include "diag.h" #include #include @@ -67,10 +68,15 @@ size_t dstrlen(const char *dstr) { } int dstreserve(char **dstr, size_t capacity) { + if (!*dstr) { + *dstr = dstralloc(capacity); + return *dstr ? 0 : -1; + } + struct dstring *header = dstrheader(*dstr); if (capacity > header->capacity) { - capacity *= 2; + capacity = bit_ceil(capacity + 1) - 1; header = realloc(header, dstrsize(capacity)); if (!header) { -- cgit v1.2.3