tree-wide: replace strv_split_full() with strv_split_extract() everywhere
Behaviour is not identical, as shown by the tests in test-strv. The combination of EXTRACT_UNQUOTE without EXTRACT_RELAX only appears in the test, so it doesn't seem particularly important. OTOH, the difference in handling of squished parameters could make a difference. New behaviour is what both bash and python do, so I think we can ignore this corner case. This change has the following advantages: - the duplication of code paths that do a very similar thing is removed - extract_one_word() / strv_split_extract() return a proper error code.
This commit is contained in:
@@ -256,44 +256,6 @@ int strv_extend_strv_concat(char ***a, char * const *b, const char *suffix) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
char **strv_split_full(const char *s, const char *separator, SplitFlags flags) {
|
||||
const char *word, *state;
|
||||
size_t l;
|
||||
size_t n, i;
|
||||
char **r;
|
||||
|
||||
assert(s);
|
||||
|
||||
if (!separator)
|
||||
separator = WHITESPACE;
|
||||
|
||||
s += strspn(s, separator);
|
||||
if (isempty(s))
|
||||
return new0(char*, 1);
|
||||
|
||||
n = 0;
|
||||
_FOREACH_WORD(word, l, s, separator, flags, state)
|
||||
n++;
|
||||
|
||||
r = new(char*, n+1);
|
||||
if (!r)
|
||||
return NULL;
|
||||
|
||||
i = 0;
|
||||
_FOREACH_WORD(word, l, s, separator, flags, state) {
|
||||
r[i] = strndup(word, l);
|
||||
if (!r[i]) {
|
||||
strv_free(r);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
r[i] = NULL;
|
||||
return r;
|
||||
}
|
||||
|
||||
char **strv_split_newlines(const char *s) {
|
||||
char **l;
|
||||
size_t n;
|
||||
|
||||
+10
-4
@@ -72,13 +72,19 @@ static inline bool strv_isempty(char * const *l) {
|
||||
return !l || !*l;
|
||||
}
|
||||
|
||||
char **strv_split_full(const char *s, const char *separator, SplitFlags flags);
|
||||
static inline char **strv_split(const char *s, const char *separator) {
|
||||
return strv_split_full(s, separator, 0);
|
||||
}
|
||||
char **strv_split_newlines(const char *s);
|
||||
|
||||
int strv_split_extract(char ***t, const char *s, const char *separators, ExtractFlags flags);
|
||||
static inline char **strv_split(const char *s, const char *separators) {
|
||||
char **ret;
|
||||
int r;
|
||||
|
||||
r = strv_split_extract(&ret, s, separators, 0);
|
||||
if (r < 0)
|
||||
return NULL;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Given a string containing white-space separated tuples of words themselves separated by ':',
|
||||
* returns a vector of strings. If the second element in a tuple is missing, the corresponding
|
||||
|
||||
Reference in New Issue
Block a user