From: Olivier Matz Date: Thu, 21 Sep 2017 14:22:34 +0000 (+0200) Subject: remove smallest start from completed struct X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=e138615ec0e881047d8ffb6dd75e9c5f5a44c93e;p=protos%2Flibecoli.git remove smallest start from completed struct --- diff --git a/lib/ecoli_completed.c b/lib/ecoli_completed.c index c9aff48..8f8dc22 100644 --- a/lib/ecoli_completed.c +++ b/lib/ecoli_completed.c @@ -243,7 +243,6 @@ __ec_completed_add_match(enum ec_completed_type type, struct ec_completed_node *compnode = NULL; struct ec_completed_match *match = NULL; int ret = -ENOMEM; - size_t n; /* find the compnode entry corresponding to this node */ TAILQ_FOREACH(compnode, &completed->nodes, next) { @@ -257,18 +256,8 @@ __ec_completed_add_match(enum ec_completed_type type, if (match == NULL) goto fail; - if (match->add != NULL) { - if (completed->smallest_start == NULL) { - completed->smallest_start = ec_strdup(match->add); - if (completed->smallest_start == NULL) - goto fail; - } else { - n = strcmp_count(match->add, - completed->smallest_start); - completed->smallest_start[n] = '\0'; - } + if (match->add != NULL) completed->count_match++; - } TAILQ_INSERT_TAIL(&compnode->matches, match, next); completed->count++; @@ -366,7 +355,6 @@ void ec_completed_free(struct ec_completed *completed) } ec_free(compnode); } - ec_free(completed->smallest_start); ec_free(completed); } @@ -380,9 +368,8 @@ void ec_completed_dump(FILE *out, const struct ec_completed *completed) return; } - fprintf(out, "completion: count=%u match=%u smallest_start=<%s>\n", - completed->count, completed->count_match, - completed->smallest_start); + fprintf(out, "completion: count=%u match=%u\n", + completed->count, completed->count_match); TAILQ_FOREACH(compnode, &completed->nodes, next) { fprintf(out, "node=%p, node_type=%s\n", @@ -402,13 +389,36 @@ void ec_completed_dump(FILE *out, const struct ec_completed *completed) } } -const char *ec_completed_smallest_start( - const struct ec_completed *completed) +char *ec_completed_smallest_start(const struct ec_completed *completed) { - if (completed == NULL || completed->smallest_start == NULL) - return ""; + struct ec_completed_node *compnode; + struct ec_completed_match *item; + char *smallest_start = NULL; + size_t n; + + if (completed == NULL) + goto fail; - return completed->smallest_start; + TAILQ_FOREACH(compnode, &completed->nodes, next) { + TAILQ_FOREACH(item, &compnode->matches, next) { + if (item->add == NULL) + continue; + if (smallest_start == NULL) { + smallest_start = ec_strdup(item->add); + if (smallest_start == NULL) + goto fail; + } else { + n = strcmp_count(item->add, smallest_start); + smallest_start[n] = '\0'; + } + } + } + + return smallest_start; + +fail: + ec_free(smallest_start); + return NULL; } unsigned int ec_completed_count( diff --git a/lib/ecoli_completed.h b/lib/ecoli_completed.h index c81544a..d801c3e 100644 --- a/lib/ecoli_completed.h +++ b/lib/ecoli_completed.h @@ -64,7 +64,6 @@ TAILQ_HEAD(ec_completed_node_list, ec_completed_node); struct ec_completed { unsigned count; unsigned count_match; - char *smallest_start; struct ec_completed_node_list nodes; }; @@ -108,9 +107,8 @@ ec_node_default_complete(const struct ec_node *gen_node, struct ec_parsed *state, const struct ec_strvec *strvec); -/* cannot return NULL */ -const char *ec_completed_smallest_start( - const struct ec_completed *completed); +/* return the smallest string start, or NULL on error */ +char *ec_completed_smallest_start(const struct ec_completed *completed); unsigned int ec_completed_count( const struct ec_completed *completed, diff --git a/lib/ecoli_test.c b/lib/ecoli_test.c index 1e98c65..a50529d 100644 --- a/lib/ecoli_test.c +++ b/lib/ecoli_test.c @@ -102,7 +102,8 @@ int ec_test_check_complete(struct ec_node *tk, ...) { struct ec_completed *c = NULL; struct ec_strvec *vec = NULL; - const char *s, *expected; + const char *expected, *s; + char *smallest_start = NULL; int ret = 0; unsigned int count = 0; va_list ap; @@ -171,8 +172,10 @@ int ec_test_check_complete(struct ec_node *tk, ...) /* check the expected smallest start */ expected = va_arg(ap, const char *); - s = ec_completed_smallest_start(c); - if (strcmp(s, expected)) { + smallest_start = ec_completed_smallest_start(c); + if (smallest_start == NULL) + goto out; + if (strcmp(smallest_start, expected)) { ret = -1; ec_log(EC_LOG_ERR, "should complete with <%s> but completes with <%s>\n", @@ -182,6 +185,7 @@ int ec_test_check_complete(struct ec_node *tk, ...) out: ec_strvec_free(vec); ec_completed_free(c); + ec_free(smallest_start); va_end(ap); return ret; }