[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 /** Formatted printing to dstrings. Existing data is replaced by the formatted
string. string.
*/ */
int dvsprintf (dstring_t *dstr, const char *fmt, va_list args) __attribute__((format(printf,2,0))); char *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 *dsprintf (dstring_t *dstr, const char *fmt, ...) __attribute__((format(printf,2,3)));
///@} ///@}
///@{ ///@{
/** Formatted printing to dstrings. Formatted string is appened to the dstring. /** Formatted printing to dstrings. Formatted string is appened to the dstring.
Embedded nulls in the dstring are ignored. Embedded nulls in the dstring are ignored.
*/ */
int davsprintf (dstring_t *dstr, const char *fmt, va_list args) __attribute__((format(printf,2,0))); char *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 *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; 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) _dvsprintf (dstring_t *dstr, int offs, const char *fmt, va_list args)
{ {
int size; int size;
@ -325,20 +325,20 @@ _dvsprintf (dstring_t *dstr, int offs, const char *fmt, va_list args)
} }
dstr->size = size + offs + 1; dstr->size = size + offs + 1;
dstr->str[dstr->size - 1] = 0; 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) dvsprintf (dstring_t *dstr, const char *fmt, va_list args)
{ {
return _dvsprintf (dstr, 0, fmt, args); return _dvsprintf (dstr, 0, fmt, args);
} }
VISIBLE int VISIBLE char *
dsprintf (dstring_t *dstr, const char *fmt, ...) dsprintf (dstring_t *dstr, const char *fmt, ...)
{ {
va_list args; va_list args;
int ret; char *ret;
va_start (args, fmt); va_start (args, fmt);
ret = _dvsprintf (dstr, 0, fmt, args); ret = _dvsprintf (dstr, 0, fmt, args);
@ -347,7 +347,7 @@ dsprintf (dstring_t *dstr, const char *fmt, ...)
return ret; return ret;
} }
VISIBLE int VISIBLE char *
davsprintf (dstring_t *dstr, const char *fmt, va_list args) davsprintf (dstring_t *dstr, const char *fmt, va_list args)
{ {
int offs = 0; int offs = 0;
@ -357,11 +357,11 @@ davsprintf (dstring_t *dstr, const char *fmt, va_list args)
return _dvsprintf (dstr, offs, fmt, args); return _dvsprintf (dstr, offs, fmt, args);
} }
VISIBLE int VISIBLE char *
dasprintf (dstring_t *dstr, const char *fmt, ...) dasprintf (dstring_t *dstr, const char *fmt, ...)
{ {
va_list args; va_list args;
int ret; char *ret;
int offs = 0; int offs = 0;
if (dstr->size) if (dstr->size)