string-util: add a mechanism for strextend_with_separator() for specifying "ignore" arguments

in strv_new() we have STRV_IGNORE for skipping over an argument in the
argument list. Let's add the same to strextend_with_separator():

strextend_with_separator(&x, "foo", POINTER_MAX, "bar");

will result in "foobar" being appended to "x". (POINTER_MAX Which is
different from NULL, which terminates the argument list).

This is useful for ternary op situations.

(We should probably get rid of STRV_IGNORE and just use POINTER_MAX
everywhere directly, but that's for another time.)
This commit is contained in:
Lennart Poettering
2025-01-15 09:32:44 +01:00
parent 34467ffa3c
commit fd3b7cf772
2 changed files with 17 additions and 0 deletions

View File

@@ -832,6 +832,8 @@ char* strextend_with_separator_internal(char **x, const char *separator, ...) {
t = va_arg(ap, const char *);
if (!t)
break;
if (t == POINTER_MAX)
continue;
n = strlen(t);
@@ -864,6 +866,8 @@ char* strextend_with_separator_internal(char **x, const char *separator, ...) {
t = va_arg(ap, const char *);
if (!t)
break;
if (t == POINTER_MAX)
continue;
if (need_separator && separator)
p = stpcpy(p, separator);