From 3861e8289ae40bac168275ce6f10b231e11baa55 Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Mon, 28 Nov 2022 22:19:12 +0000 Subject: Refactor string vector code. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/desktop_vec.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/desktop_vec.c') 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; } -- cgit v1.2.3