Merge pull request

Add m_string_cat_vprintf
This commit is contained in:
PpHd authored and GitHub committed 2023-05-08 23:35:54 +02:00
commit 408272c810
2 files changed
+39 -7

No files matched your search

+19 -7
View File
@@ -1106,25 +1106,23 @@ m_string_printf (m_string_t v, const char format[], ...)
/* Append to the string the formatted string of the given printf format */
static inline int
m_string_cat_printf (m_string_t v, const char format[], ...)
m_string_cat_vprintf (m_string_t v, const char format[], va_list args)
{
M_STR1NG_CONTRACT (v);
M_ASSERT (format != NULL);
va_list args;
va_list args_org;
va_copy(args_org, args);
int size;
size_t old_size = m_string_size(v);
char *ptr = m_str1ng_get_cstr(v);
size_t alloc = m_string_capacity(v);
va_start (args, format);
size = vsnprintf (&ptr[old_size], alloc - old_size, format, args);
if (size > 0 && (old_size+(size_t)size+1 >= alloc) ) {
// We have to realloc our string to fit the needed size
ptr = m_str1ng_fit2size (v, old_size + (size_t) size + 1);
alloc = m_string_capacity(v);
// and redo the parsing.
va_end (args);
va_start (args, format);
size = vsnprintf (&ptr[old_size], alloc - old_size, format, args);
size = vsnprintf (&ptr[old_size], alloc - old_size, format, args_org);
M_ASSERT (size >= 0);
}
if (size >= 0) {
@@ -1134,11 +1132,24 @@ m_string_cat_printf (m_string_t v, const char format[], ...)
// Undo this to have a clean state
ptr[old_size] = 0;
}
va_end (args);
va_end (args_org);
M_STR1NG_CONTRACT (v);
return size;
}
/* Append to the string the formatted string of the given printf format */
static inline int
m_string_cat_printf (m_string_t v, const char format[], ...)
{
M_STR1NG_CONTRACT (v);
M_ASSERT (format != NULL);
va_list args;
va_start (args, format);
int ret = m_string_cat_vprintf(v, format, args);
va_end (args);
return ret;
}
#if M_USE_FAST_STRING_CONV == 0
static inline void
m_string_set_ui(m_string_t v, unsigned int n)
@@ -2635,6 +2646,7 @@ namespace m_lib {
#define string_replace_all m_string_replace_all
#define string_vprintf m_string_vprintf
#define string_printf m_string_printf
#define string_cat_vprintf m_string_cat_vprintf
#define string_cat_printf m_string_cat_printf
#define string_fgets m_string_fgets
#define string_fget_word m_string_fget_word
+20
View File
@@ -143,6 +143,14 @@ static void test_utf8_it(void)
string_clear(s);
}
static void call_string_cat_vprintf(string_t s, const char format[], ...)
{
va_list args;
va_start (args, format);
string_cat_vprintf(s, format, args);
va_end (args);
}
static void test0(void)
{
string_t s1;
@@ -376,15 +384,27 @@ static void test0(void)
string_set_str(s2, "Hello, world! 10 little suns.");
assert(string_equal_p(s1, s2) == true);
string_set_str(s1, "Hello, world!");
call_string_cat_vprintf(s1, " %d little %s.", 10, "suns");
assert(string_equal_p(s1, s2) == true);
string_set_str(s1, "X:");
string_cat_printf(s1, "");
assert(string_equal_str_p(s1, "X:") == true);
string_set_str(s1, "X:");
call_string_cat_vprintf(s1, "");
assert(string_equal_str_p(s1, "X:") == true);
// Illegal format char
string_set_str(s1, "X:");
string_cat_printf(s1, "%#");
assert(string_equal_str_p(s1, "X:") == true);
string_set_str(s1, "X:");
call_string_cat_vprintf(s1, "%#");
assert(string_equal_str_p(s1, "X:") == true);
string_set_str(s1, " \r\n\t HELLO \n\r\t");
string_strim(s1);
assert (string_cmp_str (s1, "HELLO") == 0);