diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/til_str.c | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/src/til_str.c b/src/til_str.c index 298e88d..4c8a0ad 100644 --- a/src/til_str.c +++ b/src/til_str.c @@ -18,7 +18,7 @@ struct til_str_t { struct { - size_t allocated, used; /* used is length, including '\0' terminator */ + size_t allocated, used, growby; /* used is length, including '\0' terminator */ } size; char *buf; }; @@ -38,6 +38,7 @@ static til_str_t * til_str_nulstr(size_t minsize) str->size.used = 1; str->size.allocated = MAX(minsize, TIL_STR_MIN_SIZE); + str->size.growby = TIL_STR_MIN_SIZE; str->buf = calloc(1, str->size.allocated); if (!str->buf) { @@ -88,7 +89,6 @@ til_str_t * til_str_newf(const char *format, ...) assert(format); - va_start(ap, format); str = til_str_nulstr(vsnprintf(NULL, 0, format, ap) + 1); va_end(ap); @@ -121,6 +121,9 @@ int til_str_appendf(til_str_t *str, const char *format, ...) if (str->size.used + len > str->size.allocated) { char *new; + str->size.growby += TIL_STR_MIN_SIZE; + len = MAX(str->size.growby, len); + new = realloc(str->buf, str->size.used + len); if (!new) return -ENOMEM; |