BLI_string: add BLI_vsprintfN

Avoid having to use BLI_dynstr to format a string using a va_list.
This commit is contained in:
Campbell Barton
2023-07-03 13:14:06 +10:00
parent 22b98a1a55
commit 10bbf29d49
3 changed files with 23 additions and 15 deletions

View File

@@ -15,6 +15,7 @@
#include "BLI_blenlib.h"
#include "BLI_dynstr.h"
#include "BLI_string_utils.h"
#include "BLI_utildefines.h"
#include "BLT_translation.h"
@@ -109,7 +110,6 @@ void BKE_report(ReportList *reports, eReportType type, const char *_message)
void BKE_reportf(ReportList *reports, eReportType type, const char *_format, ...)
{
DynStr *ds;
Report *report;
va_list args;
const char *format = TIP_(_format);
@@ -126,15 +126,11 @@ void BKE_reportf(ReportList *reports, eReportType type, const char *_format, ...
if (reports && (reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
report = MEM_callocN(sizeof(Report), "Report");
ds = BLI_dynstr_new();
va_start(args, _format);
BLI_dynstr_vappendf(ds, format, args);
report->message = BLI_vsprintfN(format, args);
va_end(args);
report->message = BLI_dynstr_get_cstring(ds);
report->len = BLI_dynstr_get_len(ds);
BLI_dynstr_free(ds);
report->len = strlen(report->message);
report->type = type;
report->typestr = BKE_report_type_str(type);

View File

@@ -242,6 +242,9 @@ size_t BLI_vsnprintf_rlen(char *__restrict dst,
*/
char *BLI_sprintfN(const char *__restrict format, ...) ATTR_WARN_UNUSED_RESULT
ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1, 2);
/** A version of #BLI_sprintfN that takes a #va_list. */
char *BLI_vsprintfN(const char *__restrict format, va_list args) ATTR_NONNULL(1, 2)
ATTR_PRINTF_FORMAT(1, 0);
/**
* This roughly matches C and Python's string escaping with double quotes - `"`.

View File

@@ -228,20 +228,29 @@ size_t BLI_snprintf_rlen(char *__restrict dst,
char *BLI_sprintfN(const char *__restrict format, ...)
{
DynStr *ds;
DynStr *ds = BLI_dynstr_new();
va_list arg;
char *n;
va_start(arg, format);
ds = BLI_dynstr_new();
BLI_dynstr_vappendf(ds, format, arg);
n = BLI_dynstr_get_cstring(ds);
BLI_dynstr_free(ds);
va_end(arg);
return n;
char *result = BLI_dynstr_get_cstring(ds);
BLI_dynstr_free(ds);
return result;
}
char *BLI_vsprintfN(const char *__restrict format, va_list args)
{
DynStr *ds = BLI_dynstr_new();
BLI_dynstr_vappendf(ds, format, args);
char *result = BLI_dynstr_get_cstring(ds);
BLI_dynstr_free(ds);
return result;
}
/** \} */