[dstring] Return the string instead of printed size

Other than consistency with printf(), I'm not sure why we went with the
printed size as the return value; returning the resultant strings makes
much more sense as dsprintf() (etc) can then be used as a safe va()
This commit is contained in:
Bill Currie 2020-02-26 19:00:19 +09:00
parent 36bc139b27
commit ddd007e2d5
2 changed files with 12 additions and 12 deletions

View file

@ -164,15 +164,15 @@ void dstring_clearstr (dstring_t *dstr);
/** Formatted printing to dstrings. Existing data is replaced by the formatted
string.
*/
int dvsprintf (dstring_t *dstr, const char *fmt, va_list args) __attribute__((format(printf,2,0)));
int dsprintf (dstring_t *dstr, const char *fmt, ...) __attribute__((format(printf,2,3)));
char *dvsprintf (dstring_t *dstr, const char *fmt, va_list args) __attribute__((format(printf,2,0)));
char *dsprintf (dstring_t *dstr, const char *fmt, ...) __attribute__((format(printf,2,3)));
///@}
///@{
/** Formatted printing to dstrings. Formatted string is appened to the dstring.
Embedded nulls in the dstring are ignored.
*/
int davsprintf (dstring_t *dstr, const char *fmt, va_list args) __attribute__((format(printf,2,0)));
int dasprintf (dstring_t *dstr, const char *fmt, ...) __attribute__((format(printf,2,3)));
char *davsprintf (dstring_t *dstr, const char *fmt, va_list args) __attribute__((format(printf,2,0)));
char *dasprintf (dstring_t *dstr, const char *fmt, ...) __attribute__((format(printf,2,3)));
///@}
///@}

View file

@ -299,7 +299,7 @@ dstring_clearstr (dstring_t *dstr)
dstr->str[0] = 0;
}
static __attribute__((format(printf, 3, 0))) int
static __attribute__((format(printf, 3, 0))) char *
_dvsprintf (dstring_t *dstr, int offs, const char *fmt, va_list args)
{
int size;
@ -325,20 +325,20 @@ _dvsprintf (dstring_t *dstr, int offs, const char *fmt, va_list args)
}
dstr->size = size + offs + 1;
dstr->str[dstr->size - 1] = 0;
return size;
return dstr->str;
}
VISIBLE int
VISIBLE char *
dvsprintf (dstring_t *dstr, const char *fmt, va_list args)
{
return _dvsprintf (dstr, 0, fmt, args);
}
VISIBLE int
VISIBLE char *
dsprintf (dstring_t *dstr, const char *fmt, ...)
{
va_list args;
int ret;
char *ret;
va_start (args, fmt);
ret = _dvsprintf (dstr, 0, fmt, args);
@ -347,7 +347,7 @@ dsprintf (dstring_t *dstr, const char *fmt, ...)
return ret;
}
VISIBLE int
VISIBLE char *
davsprintf (dstring_t *dstr, const char *fmt, va_list args)
{
int offs = 0;
@ -357,11 +357,11 @@ davsprintf (dstring_t *dstr, const char *fmt, va_list args)
return _dvsprintf (dstr, offs, fmt, args);
}
VISIBLE int
VISIBLE char *
dasprintf (dstring_t *dstr, const char *fmt, ...)
{
va_list args;
int ret;
char *ret;
int offs = 0;
if (dstr->size)