From 8a9d5f6a0033b1c8999e528c52047e1947f3d3c9 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sun, 10 Jan 2021 09:51:38 +0100 Subject: [PATCH] Add macro string_sets (like string_cats) --- README.md | 5 +++++ m-string.h | 4 ++++ tests/test-mstring.c | 2 ++ 3 files changed, 11 insertions(+) diff --git a/README.md b/README.md index 91eb1a72..b86283d3 100644 --- a/README.md +++ b/README.md @@ -5020,6 +5020,11 @@ NOTE: v2 can also be a 'const char *' in C11. Append all the strings 'v2' ... to the string 'v'. NOTE: v2 can also be a 'const char *' in C11. +##### void string\_sets(string\_t v, const string\_t v2[, ...] ) + +Set the string 'v' to the concatenation of all the strings 'v2' +NOTE: v2 can also be a 'const char *' in C11. + ##### int string\_cmp\_str(const string\_t v1, const char str[]) ##### int string\_cmp(const string\_t v1, const string\_t str) diff --git a/m-string.h b/m-string.h index 19ac40d5..6892d30b 100644 --- a/m-string.h +++ b/m-string.h @@ -1859,6 +1859,10 @@ namespace m_lib { #define string_cats(a, ...) \ M_MAP2_C(string_cat, a, __VA_ARGS__) +/* Set a string to a set strings (or const char * if C1)) */ +#define string_sets(a, ...) \ + (string_clean(a), M_MAP2_C(string_cat, a, __VA_ARGS__) ) + /* Macro encapsulation for C11 */ #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L diff --git a/tests/test-mstring.c b/tests/test-mstring.c index 27155c39..77d20112 100644 --- a/tests/test-mstring.c +++ b/tests/test-mstring.c @@ -302,6 +302,8 @@ static void test0(void) string_set_str(s3, " world"); string_cats(s1, s2, s3); assert (string_cmp_str (s1, "Hello world") == 0); + string_sets(s1, s2, s3); + assert (string_cmp_str (s1, "o world") == 0); } string_clear (s1);