]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
string-util: introduce strprepend_with_separator()
authorMike Yuan <me@yhndnzj.com>
Fri, 14 Feb 2025 19:27:55 +0000 (20:27 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 11 Jun 2025 03:50:51 +0000 (12:50 +0900)
src/basic/string-util.c
src/basic/string-util.h
src/core/mount.c
src/test/test-string-util.c

index d2c538cfc4b4c705cf485ad032d09fee36bde732..425f9742375a9511b8fd2c45af64b5b2ba05d7fc 100644 (file)
@@ -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);
index b715e85299db4de353f5b9e8ae6addd0d9a40357..70722d9983ca6a5cf87b91bac1918f17231b2f57 100644 (file)
@@ -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)                                                   \
index 94105d9db5e58005c5e084f9c72e63b7cf943eb8..aa6a41fc0f1e1315f7bb76a8f46c8cf86670ee43 100644 (file)
@@ -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);
index fb3d28ed535af64e340fa4315c0a4317de5d8b08..fa2de0f12be54747c4bb336cc3dece291c5b7f6a 100644 (file)
@@ -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) {