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);
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)
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) \
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);
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) {