From 98eb89be5dd08f3b38f34e7881bae37c3b13e8bb Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Wed, 9 Sep 2020 13:44:39 +0200 Subject: UI: improve search results by using fuzzy and prefix matching Blender does string based searching in many different places. This patch updates four of these places to use the new string search api in `BLI_string_search.h`. In the future we probably want to update the other searches as well. Reviewers: Severin, pablovazquez Differential Revision: https://developer.blender.org/D8825 --- .../interface/interface_template_search_menu.c | 26 +++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'source/blender/editors/interface/interface_template_search_menu.c') diff --git a/source/blender/editors/interface/interface_template_search_menu.c b/source/blender/editors/interface/interface_template_search_menu.c index 667dcfd935d..5bde51846a8 100644 --- a/source/blender/editors/interface/interface_template_search_menu.c +++ b/source/blender/editors/interface/interface_template_search_menu.c @@ -41,6 +41,7 @@ #include "BLI_math_matrix.h" #include "BLI_memarena.h" #include "BLI_string.h" +#include "BLI_string_search.h" #include "BLI_string_utils.h" #include "BLI_utildefines.h" @@ -993,19 +994,24 @@ static void menu_search_update_fn(const bContext *UNUSED(C), { struct MenuSearch_Data *data = arg; - /* Prepare BLI_string_all_words_matched. */ - const size_t str_len = strlen(str); - const int words_max = BLI_string_max_possible_word_count(str_len); - int(*words)[2] = BLI_array_alloca(words, words_max); - const int words_len = BLI_string_find_split_words(str, str_len, ' ', words, words_max); + StringSearch *search = BLI_string_search_new(); - for (struct MenuSearch_Item *item = data->items.first; item; item = item->next) { - if (BLI_string_all_words_matched(item->drawwstr_full, str, words, words_len)) { - if (!UI_search_item_add(items, item->drawwstr_full, item, item->icon, item->state, 0)) { - break; - } + LISTBASE_FOREACH (struct MenuSearch_Item *, item, &data->items) { + BLI_string_search_add(search, item->drawwstr_full, item); + } + + struct MenuSearch_Item **filtered_items; + int filtered_amount = BLI_string_search_query(search, str, (void ***)&filtered_items); + + for (int i = 0; i < filtered_amount; i++) { + struct MenuSearch_Item *item = filtered_items[i]; + if (!UI_search_item_add(items, item->drawwstr_full, item, item->icon, item->state, 0)) { + break; } } + + MEM_freeN(filtered_items); + BLI_string_search_free(search); } /** \} */ -- cgit v1.2.3