Add str_format_v with va_list argument

So the string formatting can be reused in other places. Passing variadic arguments directly to another function is not possible.

The function must have a different name, as overloading `str_format` would cause the wrong function to be called when passing the `va_list`. The same naming with suffix `_v` is used for the logging functions.
This commit is contained in:
Robert Müller 2023-03-19 11:00:50 +01:00
parent 3ab0fa7653
commit d0fe1087db
2 changed files with 35 additions and 14 deletions

View file

@ -2690,26 +2690,27 @@ int str_length(const char *str)
return (int)strlen(str);
}
int str_format(char *buffer, int buffer_size, const char *format, ...)
int str_format_v(char *buffer, int buffer_size, const char *format, va_list args)
{
#if defined(CONF_FAMILY_WINDOWS)
va_list ap;
va_start(ap, format);
_vsprintf_p(buffer, buffer_size, format, ap);
va_end(ap);
_vsprintf_p(buffer, buffer_size, format, args);
buffer[buffer_size - 1] = 0; /* assure null termination */
#else
va_list ap;
va_start(ap, format);
vsnprintf(buffer, buffer_size, format, ap);
va_end(ap);
vsnprintf(buffer, buffer_size, format, args);
/* null termination is assured by definition of vsnprintf */
#endif
return str_utf8_fix_truncation(buffer);
}
int str_format(char *buffer, int buffer_size, const char *format, ...)
{
va_list args;
va_start(args, format);
int length = str_format_v(buffer, buffer_size, format, args);
va_end(args);
return length;
}
const char *str_trim_words(const char *str, int words)
{
while(*str && str_isspace(*str))

View file

@ -15,6 +15,7 @@
#endif
#include <cinttypes>
#include <cstdarg>
#include <cstdint>
#include <ctime>
@ -1248,13 +1249,32 @@ int str_length(const char *str);
* @param buffer Pointer to the buffer to receive the formatted string.
* @param buffer_size Size of the buffer.
* @param format printf formatting string.
* @param ... Parameters for the formatting.
* @param args The variable argument list.
*
* @return Length of written string, even if it has been truncated
* @return Length of written string, even if it has been truncated.
*
* @remark See the C manual for syntax for the printf formatting string.
* @remark The strings are treated as zero-terminated strings.
* @remark Guarantees that dst string will contain zero-termination.
* @remark Guarantees that buffer string will contain zero-termination.
*/
int str_format_v(char *buffer, int buffer_size, const char *format, va_list args)
GNUC_ATTRIBUTE((format(printf, 3, 0)));
/**
* Performs printf formatting into a buffer.
*
* @ingroup Strings
*
* @param buffer Pointer to the buffer to receive the formatted string.
* @param buffer_size Size of the buffer.
* @param format printf formatting string.
* @param ... Parameters for the formatting.
*
* @return Length of written string, even if it has been truncated.
*
* @remark See the C manual for syntax for the printf formatting string.
* @remark The strings are treated as zero-terminated strings.
* @remark Guarantees that buffer string will contain zero-termination.
*/
int str_format(char *buffer, int buffer_size, const char *format, ...)
GNUC_ATTRIBUTE((format(printf, 3, 4)));