From d9bf81509305fef3b2f561770118d4b47f5f63c7 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Fri, 14 Feb 2025 20:27:55 +0100 Subject: [PATCH] string-util: introduce strprepend_with_separator() --- src/basic/string-util.c | 14 -------------- src/basic/string-util.h | 13 ++++++++++++- src/core/mount.c | 9 ++------- src/test/test-string-util.c | 11 ++++++++++- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/basic/string-util.c b/src/basic/string-util.c index d2c538cfc4b..425f9742375 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -40,20 +40,6 @@ char* first_word(const char *s, const char *word) { return (char*) nw; } -char* strprepend(char **x, const char *s) { - assert(x); - - if (isempty(s) && *x) - return *x; - - char *p = strjoin(strempty(s), *x); - if (!p) - return NULL; - - free_and_replace(*x, p); - return *x; -} - char* strextendn(char **x, const char *s, size_t l) { assert(x); assert(s || l == 0); diff --git a/src/basic/string-util.h b/src/basic/string-util.h index b715e85299d..70722d9983c 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -86,7 +86,6 @@ static inline const char* empty_or_dash_to_null(const char *p) { char* first_word(const char *s, const char *word) _pure_; -char* strprepend(char **x, const char *s); char* strextendn(char **x, const char *s, size_t l) _nonnull_if_nonzero_(2, 3); #define strjoin(a, ...) strextend_with_separator_internal(NULL, NULL, a, __VA_ARGS__, NULL) @@ -177,6 +176,18 @@ char* strextend_with_separator_internal(char **x, const char *separator, ...) _s int strextendf_with_separator(char **x, const char *separator, const char *format, ...) _printf_(3,4); #define strextendf(x, ...) strextendf_with_separator(x, NULL, __VA_ARGS__) +#define strprepend_with_separator(x, separator, ...) \ + ({ \ + char **_p_ = ASSERT_PTR(x), *_s_; \ + _s_ = strextend_with_separator_internal(NULL, (separator), __VA_ARGS__, empty_to_null(*_p_), NULL); \ + if (_s_) { \ + free(*_p_); \ + *_p_ = _s_; \ + } \ + _s_; \ + }) +#define strprepend(x, ...) strprepend_with_separator(x, NULL, __VA_ARGS__) + char* strrep(const char *s, unsigned n); #define strrepa(s, n) \ diff --git a/src/core/mount.c b/src/core/mount.c index 94105d9db5e..aa6a41fc0f1 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -1169,14 +1169,9 @@ static int mount_set_mount_command(Mount *m, ExecCommand *c, const MountParamete if (r < 0) return r; - if (remount) { - if (isempty(opts)) { - opts = strdup("remount"); - if (!opts) - return -ENOMEM; - } else if (!strprepend(&opts, "remount,")) + if (remount) + if (!strprepend_with_separator(&opts, ",", "remount")) return -ENOMEM; - } if (!isempty(opts)) { r = exec_command_append(c, "-o", opts, NULL); diff --git a/src/test/test-string-util.c b/src/test/test-string-util.c index fb3d28ed535..fa2de0f12be 100644 --- a/src/test/test-string-util.c +++ b/src/test/test-string-util.c @@ -1355,7 +1355,16 @@ TEST(strprepend) { ASSERT_STREQ(strprepend(&x, "xxx"), "xxx"); ASSERT_STREQ(strprepend(&x, "bar"), "barxxx"); - ASSERT_STREQ(strprepend(&x, "foo"), "foobarxxx"); + ASSERT_STREQ(strprepend(&x, "foo", "4711"), "foo4711barxxx"); + x = mfree(x); + + ASSERT_STREQ(strprepend_with_separator(&x, "...", NULL), ""); + + ASSERT_STREQ(strprepend_with_separator(&x, "xyz", "a", "bb", "ccc"), "axyzbbxyzccc"); + x = mfree(x); + + ASSERT_STREQ(strprepend_with_separator(&x, ",", "start", "", "1", "234"), "start,,1,234"); + ASSERT_STREQ(strprepend_with_separator(&x, ";", "more", "5", "678"), "more;5;678;start,,1,234"); } TEST(strlevenshtein) { -- 2.47.3