From 39832a3e7198a99d27da392eb2f68045c09edb17 Mon Sep 17 00:00:00 2001 From: ddbb07 <31517774-ddbb07@users.noreply.gitlab.com> Date: Sat, 15 Nov 2025 23:59:31 +0100 Subject: [PATCH] Reimplement weightedSearch --- apps/openmw/mwgui/settingswindow.cpp | 50 +++++++++++----------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/apps/openmw/mwgui/settingswindow.cpp b/apps/openmw/mwgui/settingswindow.cpp index 0e13e4df49..8b7a6385be 100644 --- a/apps/openmw/mwgui/settingswindow.cpp +++ b/apps/openmw/mwgui/settingswindow.cpp @@ -2,7 +2,6 @@ #include #include -#include #include @@ -944,37 +943,26 @@ namespace MWGui namespace { - std::string escapeRegex(const std::string& str) + std::vector splitString(const std::string& inputString) { - static const std::regex specialChars(R"r([\^\.\[\$\(\)\|\*\+\?\{])r", std::regex_constants::extended); - return std::regex_replace(str, specialChars, R"(\$&)"); + std::istringstream stringStream(inputString); + return { std::istream_iterator(stringStream), std::istream_iterator() }; } - - std::regex wordSearch(const std::string& query) + double weightedSearch(std::string corpus, std::string patternString) { - static const std::regex wordsRegex(R"([^[:space:]]+)", std::regex_constants::extended); - auto wordsBegin = std::sregex_iterator(query.begin(), query.end(), wordsRegex); - auto wordsEnd = std::sregex_iterator(); - std::string searchRegex("("); - for (auto it = wordsBegin; it != wordsEnd; ++it) - { - if (it != wordsBegin) - searchRegex += '|'; - searchRegex += escapeRegex(query.substr(it->position(), it->length())); - } - searchRegex += ')'; - // query had only whitespace characters - if (searchRegex == "()") - searchRegex = "^(.*)$"; - return std::regex(searchRegex, std::regex_constants::extended | std::regex_constants::icase); - } + if (patternString.empty() || patternString.find_first_not_of(" ") == std::string::npos) + return 1.0; - double weightedSearch(const std::regex& regex, const std::string& text) - { - std::smatch matches; - std::regex_search(text, matches, regex); - // need a signed value, so cast to double (not an integer type to guarantee no overflow) - return static_cast(matches.size()); + Misc::StringUtils::lowerCaseInPlace(corpus); + Misc::StringUtils::lowerCaseInPlace(patternString); + + const std::vector patternArray = splitString(patternString); + + double numberOfMatches = 0.0; + for (const std::string& word : patternArray) + numberOfMatches += corpus.find(word) != std::string::npos ? 1.0 : 0.0; + + return numberOfMatches; } } @@ -997,14 +985,14 @@ namespace MWGui constexpr bool operator<(const WeightedPage& rhs) const { return tie() < rhs.tie(); } }; - std::regex searchRegex = wordSearch(mScriptFilter->getCaption()); + std::string searchPattern = mScriptFilter->getCaption(); std::vector weightedPages; weightedPages.reserve(LuaUi::scriptSettingsPageCount()); for (size_t i = 0; i < LuaUi::scriptSettingsPageCount(); ++i) { LuaUi::ScriptSettingsPage page = LuaUi::scriptSettingsPageAt(i); - double nameWeight = weightedSearch(searchRegex, page.mName); - double hintWeight = weightedSearch(searchRegex, page.mSearchHints); + double nameWeight = weightedSearch(page.mName, searchPattern); + double hintWeight = weightedSearch(page.mSearchHints, searchPattern); if ((nameWeight + hintWeight) > 0) weightedPages.push_back({ i, page.mName, -nameWeight, -hintWeight }); }