diff options
author | Phil Jones <philj56@gmail.com> | 2022-11-28 22:19:12 +0000 |
---|---|---|
committer | Phil Jones <philj56@gmail.com> | 2022-11-28 22:19:12 +0000 |
commit | 3861e8289ae40bac168275ce6f10b231e11baa55 (patch) | |
tree | 30093bb562e9718c60d3d36c9942ce4714c43488 /src/desktop_vec.c | |
parent | 574b523ab5d9b63a114ac905e5ff8494f4b2f233 (diff) |
Refactor string vector code.
Previously, string vectors were built by reading input line-by line, and
multiple copies of string vectors were made when searching.
Now, input is read into one big buffer, and string vectors only contain
references to the strings in this buffer. This both speeds up reading of
input, and avoids unnecessary copying of strings in various places.
The main downside currently is that input read from stdin is no longer
UTF-8 normalised. This means, for example, that a search for `e` won't
necessarily match `é`. Normalisation is very slow relative to the rest
of tofi, however, and not needed for most use-cases. This could either
be solved by accepting the slowdown, or making this an option, such as
--unicode or --unicode-normalize.
Diffstat (limited to 'src/desktop_vec.c')
-rw-r--r-- | src/desktop_vec.c | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/src/desktop_vec.c b/src/desktop_vec.c index a9aa1f7..de7fcd7 100644 --- a/src/desktop_vec.c +++ b/src/desktop_vec.c @@ -145,12 +145,12 @@ struct desktop_entry *desktop_vec_find_sorted(struct desktop_vec *restrict vec, return bsearch(&tmp, vec->buf, vec->count, sizeof(vec->buf[0]), cmpdesktopp); } -struct string_vec desktop_vec_filter( +struct string_ref_vec desktop_vec_filter( const struct desktop_vec *restrict vec, const char *restrict substr, bool fuzzy) { - struct string_vec filt = string_vec_create(); + struct string_ref_vec filt = string_ref_vec_create(); for (size_t i = 0; i < vec->count; i++) { int32_t search_score; if (fuzzy) { @@ -159,7 +159,7 @@ struct string_vec desktop_vec_filter( search_score = fuzzy_match_simple_words(substr, vec->buf[i].name); } if (search_score != INT32_MIN) { - string_vec_add(&filt, vec->buf[i].name); + string_ref_vec_add(&filt, vec->buf[i].name); /* * Store the position of the match in the string as * its search_score, for later sorting. @@ -174,7 +174,7 @@ struct string_vec desktop_vec_filter( search_score = fuzzy_match_simple_words(substr, vec->buf[i].keywords); } if (search_score != INT32_MIN) { - string_vec_add(&filt, vec->buf[i].name); + string_ref_vec_add(&filt, vec->buf[i].name); /* * Arbitrary score addition to make name * matches preferred over keyword matches. @@ -254,7 +254,6 @@ bool match_current_desktop(char * const *desktop_list, gsize length) string_vec_add(&desktops, desktop); desktop = strtok_r(NULL, ":", &saveptr); } - free(tmp); string_vec_sort(&desktops); for (gsize i = 0; i < length; i++) { @@ -264,6 +263,7 @@ bool match_current_desktop(char * const *desktop_list, gsize length) } string_vec_destroy(&desktops); + free(tmp); return false; } |