string-util: add strextendf() helper, that allows extending some allocated string via a format string

It's not going to be efficient if called in inner loops, but it's oh so
handy, and we have some code that does this:

   asprintf(&p, "%s…", b, …);
   free(b);
   b = TAKE_PTR(p);

which can now be replaced by the quicker and easier to read:

   strextendf(&p, "…", …);
This commit is contained in:
Lennart Poettering
2021-05-07 09:10:59 +02:00
committed by Zbigniew Jędrzejewski-Szmek
parent db1ce3ea1a
commit e9b88a6d4e
4 changed files with 113 additions and 24 deletions
+2
View File
@@ -152,6 +152,8 @@ char *strextend_with_separator_internal(char **x, const char *separator, ...) _s
#define strextend_with_separator(x, separator, ...) strextend_with_separator_internal(x, separator, __VA_ARGS__, NULL)
#define strextend(x, ...) strextend_with_separator_internal(x, NULL, __VA_ARGS__, NULL)
int strextendf(char **x, const char *format, ...) _printf_(2,3);
char *strrep(const char *s, unsigned n);
int split_pair(const char *s, const char *sep, char **l, char **r);