summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-03-03 16:46:11 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-03-03 16:46:11 -0500
commit74cae07c859e68876ee98b99e1c1761d4c205484 (patch)
tree05c992da6cc2da48d7a3635249cfcf46836aafdb
parent6f90a278bc04432195ae328126544752f0862b49 (diff)
tags_beginning_with (starting completion)
-rw-r--r--main.c2
-rw-r--r--tags.c24
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;