string-util: some minor coding style updates

This commit is contained in:
Lennart Poettering 2020-04-02 16:00:55 +02:00
parent 77b19caf6b
commit 5fed82c642
1 changed files with 39 additions and 26 deletions

View File

@ -127,45 +127,58 @@ static size_t strcspn_escaped(const char *s, const char *reject) {
} }
/* Split a string into words. */ /* Split a string into words. */
const char* split(const char **state, size_t *l, const char *separator, SplitFlags flags) { const char* split(
const char **state,
size_t *l,
const char *separator,
SplitFlags flags) {
const char *current; const char *current;
assert(state);
assert(l);
if (!separator)
separator = WHITESPACE;
current = *state; current = *state;
if (!*current) { if (*current == '\0') /* already at the end? */
assert(**state == '\0');
return NULL; return NULL;
}
current += strspn(current, separator); current += strspn(current, separator); /* skip leading separators */
if (!*current) { if (*current == '\0') { /* at the end now? */
*state = current; *state = current;
return NULL; return NULL;
} }
if (flags & SPLIT_QUOTES && strchr("\'\"", *current)) { if (FLAGS_SET(flags, SPLIT_QUOTES)) {
char quotechars[2] = {*current, '\0'};
*l = strcspn_escaped(current + 1, quotechars); if (strchr(QUOTES, *current)) {
if (current[*l + 1] == '\0' || current[*l + 1] != quotechars[0] || /* We are looking at a quote */
(current[*l + 2] && !strchr(separator, current[*l + 2]))) { *l = strcspn_escaped(current + 1, CHAR_TO_STR(*current));
/* right quote missing or garbage at the end */ if (current[*l + 1] != *current ||
if (flags & SPLIT_RELAX) { (current[*l + 2] != 0 && !strchr(separator, current[*l + 2]))) {
*state = current + *l + 1 + (current[*l + 1] != '\0'); /* right quote missing or garbage at the end */
return current + 1; if (FLAGS_SET(flags, SPLIT_RELAX)) {
*state = current + *l + 1 + (current[*l + 1] != '\0');
return current + 1;
}
*state = current;
return NULL;
} }
*state = current; *state = current++ + *l + 2;
return NULL;
} else {
/* We are looking at a something that is not a quote */
*l = strcspn_escaped(current, separator);
if (current[*l] && !strchr(separator, current[*l]) && !FLAGS_SET(flags, SPLIT_RELAX)) {
/* unfinished escape */
*state = current;
return NULL;
}
*state = current + *l;
} }
*state = current++ + *l + 2;
} else if (flags & SPLIT_QUOTES) {
*l = strcspn_escaped(current, separator);
if (current[*l] && !strchr(separator, current[*l]) && !(flags & SPLIT_RELAX)) {
/* unfinished escape */
*state = current;
return NULL;
}
*state = current + *l;
} else { } else {
*l = strcspn(current, separator); *l = strcspn(current, separator);
*state = current + *l; *state = current + *l;