From 68b41f9b449568c35ba09ad7ad91c5dda85a253f Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 15 Oct 2002 19:52:46 +0000 Subject: [PATCH] make dstring_adjust `grow' in increments of 1024. this should speed up multiple appends nicely. also create davsprintf and dasprintf to /append/ the printed strings to the dstring rather than overwriting it as is done with dvsprintf and dsprintf. --- include/QF/dstring.h | 2 ++ libs/util/dstring.c | 52 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/include/QF/dstring.h b/include/QF/dstring.h index e249c55c9..b3f0ab369 100644 --- a/include/QF/dstring.h +++ b/include/QF/dstring.h @@ -62,5 +62,7 @@ void dstring_clearstr (dstring_t *dstr); int dvsprintf (dstring_t *dstr, const char *fmt, va_list args); int dsprintf (dstring_t *dstr, const char *fmt, ...) __attribute__((format(printf,2,3))); +int davsprintf (dstring_t *dstr, const char *fmt, va_list args); +int dasprintf (dstring_t *dstr, const char *fmt, ...) __attribute__((format(printf,2,3))); #endif // __dstring_h diff --git a/libs/util/dstring.c b/libs/util/dstring.c index 7e16f55c2..99103a2d1 100644 --- a/libs/util/dstring.c +++ b/libs/util/dstring.c @@ -59,10 +59,10 @@ inline void dstring_adjust (dstring_t *dstr) { if (dstr->size > dstr->truesize) { - dstr->str = realloc (dstr->str, dstr->size); + dstr->truesize = (dstr->size + 1023) & ~1023; + dstr->str = realloc (dstr->str, dstr->truesize); if (!dstr->str) Sys_Error ("dstring_adjust: Failed to reallocate memory."); - dstr->truesize = dstr->size; } } @@ -191,8 +191,8 @@ dstring_clearstr (dstring_t *dstr) # define VA_COPY memcpy (a, b, sizeof (a)) #endif -int -dvsprintf (dstring_t *dstr, const char *fmt, va_list args) +static int +_dvsprintf (dstring_t *dstr, int offs, const char *fmt, va_list args) { int size; @@ -205,22 +205,30 @@ dvsprintf (dstring_t *dstr, const char *fmt, va_list args) dstr->size = 1024; dstring_adjust (dstr); } - size = vsnprintf (dstr->str, dstr->truesize, fmt, args) + 1; // +1 for nul + // +1 for nul + size = vsnprintf (dstr->str + offs, dstr->truesize - offs, fmt, args) + 1; while (size <= 0 || size > dstr->truesize) { if (size > 0) - dstr->size = (size + 1023) & ~1023; // 1k multiples + dstr->size = (size + offs + 1023) & ~1023; // 1k multiples else dstr->size = dstr->truesize + 1024; dstring_adjust (dstr); #ifdef VA_LIST_IS_ARRAY VA_COPY (args, tmp_args); #endif - size = vsnprintf (dstr->str, dstr->truesize, fmt, args) + 1; + size = vsnprintf (dstr->str + offs, dstr->truesize - offs, + fmt, args) + 1; } - dstr->size = size; + dstr->size = size + offs; return size - 1; } +int +dvsprintf (dstring_t *dstr, const char *fmt, va_list args) +{ + return _dvsprintf (dstr, 0, fmt, args); +} + int dsprintf (dstring_t *dstr, const char *fmt, ...) { @@ -228,7 +236,33 @@ dsprintf (dstring_t *dstr, const char *fmt, ...) int ret; va_start (args, fmt); - ret = dvsprintf (dstr, fmt, args); + ret = _dvsprintf (dstr, 0, fmt, args); + va_end (args); + + return ret; +} + +int +davsprintf (dstring_t *dstr, const char *fmt, va_list args) +{ + int offs = 0; + + if (dstr->size) + offs = dstr->size - 1; + return _dvsprintf (dstr, offs, fmt, args); +} + +int +dasprintf (dstring_t *dstr, const char *fmt, ...) +{ + va_list args; + int ret; + int offs = 0; + + if (dstr->size) + offs = dstr->size - 1; + va_start (args, fmt); + ret = _dvsprintf (dstr, offs, fmt, args); va_end (args); return ret;