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) --- main.c | 2 -- tags.c | 24 ++++++++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/main.c b/main.c index 6a9e46a..0a78fe2 100644 --- a/main.c +++ b/main.c @@ -290,8 +290,6 @@ int main(int argc, char **argv) { setlocale(LC_ALL, ""); // allow unicode - - // read command-line arguments char const *starting_filename = NULL; switch (argc) { 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