From 1057a5cffc687688f8b55a6d764584fb56578b21 Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Sat, 18 Jun 2022 20:02:09 +0200 Subject: [PATCH] Small fix --- src/client/component/gui_asset_list.cpp | 8 +++----- src/common/utils/string.cpp | 20 ++++++++++++++++++++ src/common/utils/string.hpp | 2 ++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/client/component/gui_asset_list.cpp b/src/client/component/gui_asset_list.cpp index c4989191..a5ece325 100644 --- a/src/client/component/gui_asset_list.cpp +++ b/src/client/component/gui_asset_list.cpp @@ -38,7 +38,7 @@ namespace asset_list const auto name = game::g_assetNames[i]; const auto type = static_cast(i); - if (utils::string::find_lower(name, asset_type_filter)) + if (utils::string::strstr_lower(name, asset_type_filter.data())) { ImGui::Checkbox(name, &shown_assets[type]); } @@ -63,14 +63,12 @@ namespace asset_list ImGui::InputText("asset name", &assets_name_filter[type]); ImGui::BeginChild("assets list"); - const auto lowercase_filter = utils::string::to_lower(assets_name_filter[type]); - - fastfiles::enum_assets(type, [&lowercase_filter, type](const game::XAssetHeader header) + fastfiles::enum_assets(type, [type](const game::XAssetHeader header) { const auto asset = game::XAsset{type, header}; const auto* const asset_name = game::DB_GetXAssetName(&asset); - if (strstr(asset_name, lowercase_filter.data()) && ImGui::Button(asset_name)) + if (utils::string::strstr_lower(asset_name, assets_name_filter[type].data()) && ImGui::Button(asset_name)) { gui::copy_to_clipboard(asset_name); } diff --git a/src/common/utils/string.cpp b/src/common/utils/string.cpp index f47703ce..735c56ac 100644 --- a/src/common/utils/string.cpp +++ b/src/common/utils/string.cpp @@ -204,4 +204,24 @@ namespace utils::string ? text : text.substr(0, length - end.size()) + end; } + + bool strstr_lower(const char* a, const char* b) + { + size_t index{}; + while (*a != '\0' && b[index] != '\0') + { + if (std::tolower(*a) == std::tolower(b[index])) + { + index++; + } + else if (index != 0) + { + return false; + } + + a++; + } + + return b[index] == '\0'; + } } diff --git a/src/common/utils/string.hpp b/src/common/utils/string.hpp index 2b91102b..970e1617 100644 --- a/src/common/utils/string.hpp +++ b/src/common/utils/string.hpp @@ -102,4 +102,6 @@ namespace utils::string bool find_lower(const std::string& a, const std::string& b); std::string truncate(const std::string& text, const size_t length, const std::string& end); + + bool strstr_lower(const char* a, const char* b); }