From 74cae07c859e68876ee98b99e1c1761d4c205484 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Wed, 3 Mar 2021 16:46:11 -0500 Subject: tags_beginning_with (starting completion) --- tags.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'tags.c') diff --git a/tags.c b/tags.c index efe70f8..a7b87e8 100644 --- a/tags.c +++ b/tags.c @@ -70,15 +70,27 @@ size_t tags_beginning_with(Ted *ted, char const *prefix, char **out, size_t out_ } } char line[1024]; - if (!exact) + fseek(file, (long)mid, SEEK_SET); + if (!exact && mid > 0) fgets(line, sizeof line, file); // go to next line size_t nmatches = 0; - while (fgets(line, sizeof line, file)) { - if (str_is_prefix(line, prefix)) { - out[nmatches++] = strn_dup(line, strcspn(line, "\t")); - if (nmatches >= out_size) break; - } else break; + size_t prefix_len = strlen(prefix); + bool done = false; + while (!done && fgets(line, sizeof line, file)) { + switch (strncmp(line, prefix, prefix_len)) { + case 0: { + char *tag = strn_dup(line, strcspn(line, "\t")); + if (nmatches == 0 || !streq(tag, out[nmatches - 1])) // don't include duplicate tags + out[nmatches++] = tag; + else + free(tag); + if (nmatches >= out_size) done = true; + } break; + case +1: + done = true; + break; + } } fclose(file); return nmatches; -- cgit v1.2.3