aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-04-16 21:45:52 +0200
committerdec05eba <dec05eba@protonmail.com>2021-04-16 21:45:52 +0200
commit1376a92ef6d6a6418efefbcde5e01d1958e52fd5 (patch)
treef5bdea6ff68eeef109ab186a1cdeb6810422f067
parent88d3dbbd12d17056323f40effe00ce8ab8180691 (diff)
Improve fuzzy search
-rw-r--r--src/Body.cpp48
-rw-r--r--src/QuickMedia.cpp7
2 files changed, 31 insertions, 24 deletions
diff --git a/src/Body.cpp b/src/Body.cpp
index 6833134..c174188 100644
--- a/src/Body.cpp
+++ b/src/Body.cpp
@@ -4,6 +4,7 @@
#include "../include/ResourceLoader.hpp"
#include "../include/AsyncImageLoader.hpp"
#include "../include/Utils.hpp"
+#include "../include/StringUtils.hpp"
#include "../plugins/Plugin.hpp"
#include <SFML/Graphics/CircleShape.hpp>
#include <SFML/Window/Event.hpp>
@@ -1254,35 +1255,38 @@ 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;
+ static size_t str_find_case_insensitive(const std::string &str, size_t start_index, const char *substr, size_t substr_len) {
+ auto it = std::search(str.begin() + start_index, str.end(), substr, substr + substr_len,
+ [](char c1, char c2) {
+ return std::toupper(c1) == std::toupper(c2);
+ });
+ if(it == str.end())
+ return std::string::npos;
+ return it - str.begin();
}
// TODO: Support utf-8 case insensitive find
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;
+ if(str.empty()) return false;
+ if(substr.empty()) 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]);
+ size_t str_index = 0;
+ bool full_match = true;
+
+ string_split(substr, ' ', [&str, &str_index, &full_match](const char *str_part, size_t size) {
+ if(size == 0) return true;
+
+ size_t found_index = str_find_case_insensitive(str, str_index, str_part, size);
+ if(found_index == std::string::npos) {
+ full_match = false;
+ return false;
}
- }
- return false;
+ str_index = found_index + size;
+ return true;
+ });
+
+ return full_match;
}
void Body::filter_search_fuzzy(const std::string &text) {
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp
index 955a189..5fcddba 100644
--- a/src/QuickMedia.cpp
+++ b/src/QuickMedia.cpp
@@ -694,7 +694,8 @@ namespace QuickMedia {
string_split(urls_combined, ',', [&urls](const char *str, size_t size) {
std::string url(str, size);
url = strip(url);
- urls.push_back(std::move(url));
+ if(!url.empty())
+ urls.push_back(std::move(url));
return true;
});
})
@@ -736,7 +737,9 @@ namespace QuickMedia {
string_split(urls_str, ',', [&urls](const char *str, size_t size) {
std::string url(str, size);
- urls.push_back(std::move(url));
+ url = strip(url);
+ if(!url.empty())
+ urls.push_back(std::move(url));
return true;
});
return urls;