diff options
Diffstat (limited to 'src/Body.cpp')
-rw-r--r-- | src/Body.cpp | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/src/Body.cpp b/src/Body.cpp index 5c40c96..6833134 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -1254,14 +1254,35 @@ namespace QuickMedia { return spacing_y; } + // Returns std::string::npos if not found + static size_t find_next_non_whitespace_character(const std::string &str, size_t start_index) { + for(size_t i = start_index; i < str.size(); ++i) { + char c = str[i]; + if(c != ' ' && c != '\n' && c != '\t' && c != '\v') + return i; + } + return std::string::npos; + } + // TODO: Support utf-8 case insensitive find - //static - bool Body::string_find_case_insensitive(const std::string &str, const std::string &substr) { - auto it = std::search(str.begin(), str.end(), substr.begin(), substr.end(), - [](char c1, char c2) { - return std::toupper(c1) == std::toupper(c2); - }); - return it != str.end(); + static bool string_find_fuzzy_case_insensitive(const std::string &str, const std::string &substr) { + size_t substr_index = find_next_non_whitespace_character(substr, 0); + if(substr_index == std::string::npos) + return true; + + char substr_c = std::toupper(substr[substr_index]); + for(size_t i = 0; i < str.size(); ++i) { + char str_c = std::toupper(str[i]); + if(str_c == substr_c) { + substr_index = find_next_non_whitespace_character(substr, substr_index + 1); + if(substr_index == std::string::npos || substr_index == substr.size()) + return true; + else + substr_c = std::toupper(substr[substr_index]); + } + } + + return false; } void Body::filter_search_fuzzy(const std::string &text) { @@ -1280,11 +1301,11 @@ namespace QuickMedia { } void Body::filter_search_fuzzy_item(const std::string &text, BodyItem *body_item) { - body_item->visible = string_find_case_insensitive(body_item->get_title(), text); + body_item->visible = string_find_fuzzy_case_insensitive(body_item->get_title(), text); if(!body_item->visible && !body_item->get_description().empty()) - body_item->visible = string_find_case_insensitive(body_item->get_description(), text); + body_item->visible = string_find_fuzzy_case_insensitive(body_item->get_description(), text); if(!body_item->visible && !body_item->get_author().empty()) - body_item->visible = string_find_case_insensitive(body_item->get_author(), text); + body_item->visible = string_find_fuzzy_case_insensitive(body_item->get_author(), text); } bool Body::no_items_visible() const { |