From c7a9c8215ce68b40a2b8a02ae5382fef37e0c29e Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 28 Dec 2024 11:54:58 -0500 Subject: [PATCH] Reorganized functions, consolidated. --- src/dstr.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/dstr.c b/src/dstr.c index 29dafd9..48183d1 100644 --- a/src/dstr.c +++ b/src/dstr.c @@ -1,6 +1,7 @@ #include "include/dstr.h" #include "include/util.h" +#include #include #include @@ -20,16 +21,21 @@ void dstr_destroy(Dstr* dstr) { free(dstr); } -void dstr_append(Dstr* dest, char* src, size_t ln) { - while (dest->ln + ln + 1 > dest->bufsz) { +// Check whether the buffer is overflowing and resize it if necessary. +void check_resz(Dstr* dstr, size_t ln) { + while (dstr->ln + ln + 1 > dstr->bufsz) { // Double the buffer size when overflown. - dest->bufsz *= 2; - dest->buf = realloc(dest->buf, dest->bufsz); + dstr->bufsz *= 2; + dstr->buf = realloc(dstr->buf, dstr->bufsz); log_dbgf( - "dstr @ %p doubled from %ld to %ld", dest, dest->bufsz / 2, - dest->bufsz + "dstr @ %p doubled from %ld to %ld", dstr, dstr->bufsz / 2, + dstr->bufsz ); } +} + +void dstr_append(Dstr* dest, char* src, size_t ln) { + check_resz(dest, ln); // Overwrites the \0 at the end of the string, keeps the null from the given // string. @@ -38,15 +44,7 @@ void dstr_append(Dstr* dest, char* src, size_t ln) { } void dstr_appendch(Dstr* dest, char ch) { - if (dest->ln + 1 + 1 > dest->bufsz) { - // Double the buffer size when overflown. - dest->bufsz *= 2; - dest->buf = realloc(dest->buf, dest->bufsz); - log_dbgf( - "dstr @ %p doubled from %ld to %ld", dest, dest->bufsz / 2, - dest->bufsz - ); - } + check_resz(dest, 1); // Overwrites the preexisting null terminator, and adds one of its own. dest->buf[dest->ln] = ch;