While a neat theoretical construct, state machines are hard to read. In this instance, it does not even make a whole lot of sense because we are more interested in flags, anyway: has the section been seen? Has the key been seen? Does the current section match the key we are looking for? Besides, the state `SECTION_SEEN` was named in a misleading way: it did not indicate that we saw the section matching the key we are looking for, but it instead indicated that we are *currently* in that section. Let's just replace the state machine logic by clear and obvious flags. This will also make it easier to review the upcoming patches to use the newly-introduced `event_fn` callback of the config parser. Signed-off-by: Johannes Schindelin <johannes.schindelin@xxxxxx> --- config.c | 59 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/config.c b/config.c index b73b48b5650..84e8f7ffeb8 100644 --- a/config.c +++ b/config.c @@ -2305,7 +2305,7 @@ struct config_set_store { int multi_replace; size_t *seen; unsigned int seen_nr, seen_alloc; - enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state; + unsigned int key_seen:1, section_seen:1, is_keys_section:1; }; static int matches(const char *key, const char *value, @@ -2328,8 +2328,7 @@ static int store_aux(const char *key, const char *value, void *cb) size_t section_len; struct config_set_store *store = cb; - switch (store->state) { - case KEY_SEEN: + if (store->key_seen) { if (matches(key, value, store)) { if (store->seen_nr == 1 && store->multi_replace == 0) { warning(_("%s has multiple values"), key); @@ -2341,8 +2340,8 @@ static int store_aux(const char *key, const char *value, void *cb) store->seen[store->seen_nr] = cf->do_ftell(cf); store->seen_nr++; } - break; - case SECTION_SEEN: + return 0; + } else if (store->is_keys_section) { /* * What we are looking for is in store->key (both * section and var), and its section part is baselen @@ -2357,10 +2356,9 @@ static int store_aux(const char *key, const char *value, void *cb) if ((section_len != store->baselen) || memcmp(key, store->key, section_len+1)) { - store->state = SECTION_END_SEEN; - break; + store->is_keys_section = 0; + return 0; } - /* * Do not increment matches: this is no match, but we * just made sure we are in the desired section. @@ -2368,27 +2366,29 @@ static int store_aux(const char *key, const char *value, void *cb) ALLOC_GROW(store->seen, store->seen_nr + 1, store->seen_alloc); store->seen[store->seen_nr] = cf->do_ftell(cf); - /* fallthru */ - case SECTION_END_SEEN: - case START: - if (matches(key, value, store)) { - ALLOC_GROW(store->seen, store->seen_nr + 1, - store->seen_alloc); - store->seen[store->seen_nr] = cf->do_ftell(cf); - store->state = KEY_SEEN; - store->seen_nr++; - } else { - if (strrchr(key, '.') - key == store->baselen && - !strncmp(key, store->key, store->baselen)) { - store->state = SECTION_SEEN; - ALLOC_GROW(store->seen, - store->seen_nr + 1, - store->seen_alloc); - store->seen[store->seen_nr] = - cf->do_ftell(cf); - } + } + + if (matches(key, value, store)) { + ALLOC_GROW(store->seen, store->seen_nr + 1, + store->seen_alloc); + store->seen[store->seen_nr] = cf->do_ftell(cf); + store->seen_nr++; + store->key_seen = 1; + store->section_seen = 1; + store->is_keys_section = 1; + } else { + if (strrchr(key, '.') - key == store->baselen && + !strncmp(key, store->key, store->baselen)) { + store->section_seen = 1; + store->is_keys_section = 1; + ALLOC_GROW(store->seen, + store->seen_nr + 1, + store->seen_alloc); + store->seen[store->seen_nr] = + cf->do_ftell(cf); } } + return 0; } @@ -2646,7 +2646,6 @@ int git_config_set_multivar_in_file_gently(const char *config_filename, ALLOC_GROW(store.seen, 1, store.seen_alloc); store.seen[0] = 0; - store.state = START; store.seen_nr = 0; /* @@ -2714,7 +2713,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename, new_line = 0; if (store.seen[i] == 0) { store.seen[i] = copy_end = contents_sz; - } else if (store.state != KEY_SEEN) { + } else if (!store.key_seen) { copy_end = store.seen[i]; } else copy_end = find_beginning_of_line( @@ -2738,7 +2737,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename, /* write the pair (value == NULL means unset) */ if (value != NULL) { - if (store.state == START) { + if (!store.section_seen) { if (write_section(fd, key, &store) < 0) goto write_err_out; } -- 2.16.2.windows.1.26.g2cc3565eb4b