22
33#include < array>
44#include < iomanip>
5- #include < regex>
65
76#include < unicode/locid.h>
87
4342#include " ../mwlua/luamanagerimp.hpp"
4443
4544#include " confirmationdialog.hpp"
45+ #include " weightedsearch.hpp"
4646
4747namespace
4848{
@@ -942,42 +942,6 @@ namespace MWGui
942942 mControlsBox ->setVisibleVScroll (true );
943943 }
944944
945- namespace
946- {
947- std::string escapeRegex (const std::string& str)
948- {
949- static const std::regex specialChars (R"r( [\^\.\[\$\(\)\|\*\+\?\{])r" , std::regex_constants::extended);
950- return std::regex_replace (str, specialChars, R"( \$&)" );
951- }
952-
953- std::regex wordSearch (const std::string& query)
954- {
955- static const std::regex wordsRegex (R"( [^[:space:]]+)" , std::regex_constants::extended);
956- auto wordsBegin = std::sregex_iterator (query.begin (), query.end (), wordsRegex);
957- auto wordsEnd = std::sregex_iterator ();
958- std::string searchRegex (" (" );
959- for (auto it = wordsBegin; it != wordsEnd; ++it)
960- {
961- if (it != wordsBegin)
962- searchRegex += ' |' ;
963- searchRegex += escapeRegex (query.substr (it->position (), it->length ()));
964- }
965- searchRegex += ' )' ;
966- // query had only whitespace characters
967- if (searchRegex == " ()" )
968- searchRegex = " ^(.*)$" ;
969- return std::regex (searchRegex, std::regex_constants::extended | std::regex_constants::icase);
970- }
971-
972- double weightedSearch (const std::regex& regex, const std::string& text)
973- {
974- std::smatch matches;
975- std::regex_search (text, matches, regex);
976- // need a signed value, so cast to double (not an integer type to guarantee no overflow)
977- return static_cast <double >(matches.size ());
978- }
979- }
980-
981945 void SettingsWindow::renderScriptSettings ()
982946 {
983947 mScriptAdapter ->detach ();
@@ -989,24 +953,29 @@ namespace MWGui
989953 {
990954 size_t mIndex ;
991955 std::string mName ;
992- double mNameWeight ;
993- double mHintWeight ;
994-
995- constexpr auto tie () const { return std::tie (mNameWeight , mHintWeight , mName ); }
956+ size_t mNameWeight ;
957+ size_t mHintWeight ;
996958
997- constexpr bool operator <(const WeightedPage& rhs) const { return tie () < rhs.tie (); }
959+ constexpr bool operator <(const WeightedPage& rhs) const
960+ {
961+ if (mNameWeight != rhs.mNameWeight )
962+ return mNameWeight > rhs.mNameWeight ;
963+ if (mHintWeight != rhs.mHintWeight )
964+ return mHintWeight > rhs.mHintWeight ;
965+ return mName < rhs.mName ;
966+ }
998967 };
999968
1000- std::regex searchRegex = wordSearch (mScriptFilter ->getCaption ());
969+ const std::vector<std::string> patternArray = generatePatternArray (mScriptFilter ->getCaption ());
1001970 std::vector<WeightedPage> weightedPages;
1002971 weightedPages.reserve (LuaUi::scriptSettingsPageCount ());
1003972 for (size_t i = 0 ; i < LuaUi::scriptSettingsPageCount (); ++i)
1004973 {
1005974 LuaUi::ScriptSettingsPage page = LuaUi::scriptSettingsPageAt (i);
1006- double nameWeight = weightedSearch (searchRegex, page.mName );
1007- double hintWeight = weightedSearch (searchRegex, page.mSearchHints );
975+ size_t nameWeight = weightedSearch (page.mName , patternArray );
976+ size_t hintWeight = weightedSearch (page.mSearchHints , patternArray );
1008977 if ((nameWeight + hintWeight) > 0 )
1009- weightedPages.push_back ({ i, page.mName , - nameWeight, - hintWeight });
978+ weightedPages.push_back ({ i, page.mName , nameWeight, hintWeight });
1010979 }
1011980 std::sort (weightedPages.begin (), weightedPages.end ());
1012981 for (const WeightedPage& weightedPage : weightedPages)
0 commit comments