diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2021-03-03 16:46:11 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2021-03-03 16:46:11 -0500 |
commit | 74cae07c859e68876ee98b99e1c1761d4c205484 (patch) | |
tree | 05c992da6cc2da48d7a3635249cfcf46836aafdb | |
parent | 6f90a278bc04432195ae328126544752f0862b49 (diff) |
tags_beginning_with (starting completion)
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | tags.c | 24 |
2 files changed, 18 insertions, 8 deletions
@@ -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) { @@ -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; |