extract-word: move start block outside the for loop

This block runs once before all the other handling, so move it outside
the main loop and put it in its own loop until it's finished doing its
job.

Tested by confirming `make check` (and particularly test-extract-word)
still passes and by booting a system with binaries including this
commit.
This commit is contained in:
Filipe Brandenburger 2015-11-03 20:13:11 -08:00
parent a6bff4a742
commit b85e1c2534
1 changed files with 17 additions and 18 deletions

View File

@ -29,12 +29,12 @@
int extract_first_word(const char **p, char **ret, const char *separators, ExtractFlags flags) {
_cleanup_free_ char *s = NULL;
size_t allocated = 0, sz = 0;
char c;
int r;
char quote = 0; /* 0 or ' or " */
bool backslash = false; /* whether we've just seen a backslash */
bool separator = false; /* whether we've just seen a separator */
bool start = true; /* false means we're looking at a value */
assert(p);
assert(ret);
@ -51,31 +51,30 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
* (because of an uneven number of quotes or similar), leaves
* the pointer *p at the first invalid character. */
if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
if (!GREEDY_REALLOC(s, allocated, sz+1))
return -ENOMEM;
for (;;) {
char c = **p;
if (start) {
c = **p;
if (c == 0)
goto finish_force_terminate;
else if (strchr(separators, c)) {
(*p) ++;
if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
if (!GREEDY_REALLOC(s, allocated, sz+1))
return -ENOMEM;
if (c == 0)
goto finish_force_terminate;
else if (strchr(separators, c)) {
(*p) ++;
if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
goto finish_force_next;
continue;
}
goto finish_force_next;
} else {
/* We found a non-blank character, so we will always
* want to return a string (even if it is empty),
* allocate it here. */
if (!GREEDY_REALLOC(s, allocated, sz+1))
return -ENOMEM;
start = false;
break;
}
}
for (;;) {
c = **p;
if (backslash) {
if (!GREEDY_REALLOC(s, allocated, sz+7))