return r;
}
-char *strv_join_quoted(char **l) {
- char *buf = NULL;
- char **s;
- size_t allocated = 0, len = 0;
-
- STRV_FOREACH(s, l) {
- /* assuming here that escaped string cannot be more
- * than twice as long, and reserving space for the
- * separator and quotes.
- */
- _cleanup_free_ char *esc = NULL;
- size_t needed;
-
- if (!GREEDY_REALLOC(buf, allocated,
- len + strlen(*s) * 2 + 3))
- goto oom;
-
- esc = cescape(*s);
- if (!esc)
- goto oom;
-
- needed = snprintf(buf + len, allocated - len, "%s\"%s\"",
- len > 0 ? " " : "", esc);
- assert(needed < allocated - len);
- len += needed;
- }
-
- if (!buf)
- buf = malloc0(1);
-
- return buf;
-
- oom:
- return mfree(buf);
-}
-
int strv_push(char ***l, char *value) {
char **c;
unsigned n, m;
int strv_split_extract(char ***t, const char *s, const char *separators, ExtractFlags flags);
char *strv_join(char **l, const char *separator);
-char *strv_join_quoted(char **l);
char **strv_parse_nulstr(const char *s, size_t l);
char **strv_split_nulstr(const char *s);
#include <string.h>
#include "alloc-util.h"
+#include "escape.h"
#include "specifier.h"
#include "string-util.h"
#include "strv.h"
};
-static const char* const input_table_quotes[] = {
- "\"",
- "'",
- "\"\"",
- "\\",
- "\\\\",
- NULL,
-};
-#define QUOTES_STRING \
- "\"\\\"\" " \
- "\"\\\'\" " \
- "\"\\\"\\\"\" " \
- "\"\\\\\" " \
- "\"\\\\\\\\\""
-
-static const char * const input_table_spaces[] = {
- " ",
- "' '",
- "\" ",
- " \"",
- " \\\\ ",
- NULL,
-};
-#define SPACES_STRING \
- "\" \" " \
- "\"\\' \\'\" " \
- "\"\\\" \" " \
- "\" \\\"\" " \
- "\" \\\\\\\\ \""
-
static void test_strv_find(void) {
assert_se(strv_find((char **)input_table_multiple, "three"));
assert_se(!strv_find((char **)input_table_multiple, "four"));
assert_se(streq(w, ""));
}
-static void test_strv_quote_unquote(const char* const *split, const char *quoted) {
- _cleanup_free_ char *p;
- _cleanup_strv_free_ char **s = NULL;
- char **t;
- int r;
-
- p = strv_join_quoted((char **)split);
- assert_se(p);
- printf("-%s- --- -%s-\n", p, quoted); /* fprintf deals with NULL, puts does not */
- assert_se(p);
- assert_se(streq(p, quoted));
-
- r = strv_split_extract(&s, quoted, WHITESPACE, EXTRACT_QUOTES);
- assert_se(r == (int) strv_length(s));
- assert_se(s);
- STRV_FOREACH(t, s) {
- assert_se(*t);
- assert_se(streq(*t, *split));
- split++;
- }
-}
-
static void test_strv_unquote(const char *quoted, char **list) {
_cleanup_strv_free_ char **s;
_cleanup_free_ char *j;
test_strv_find_startswith();
test_strv_join();
- test_strv_quote_unquote(input_table_multiple, "\"one\" \"two\" \"three\"");
- test_strv_quote_unquote(input_table_one, "\"one\"");
- test_strv_quote_unquote(input_table_none, "");
- test_strv_quote_unquote(input_table_one_empty, "\"\"");
- test_strv_quote_unquote(input_table_two_empties, "\"\" \"\"");
- test_strv_quote_unquote(input_table_quotes, QUOTES_STRING);
- test_strv_quote_unquote(input_table_spaces, SPACES_STRING);
-
test_strv_unquote(" foo=bar \"waldo\" zzz ", STRV_MAKE("foo=bar", "waldo", "zzz"));
test_strv_unquote("", STRV_MAKE_EMPTY);
test_strv_unquote(" ", STRV_MAKE_EMPTY);