Skip to content

Commit

Permalink
add caseInsensitiveCompare util, use it for local mod ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Oct 2, 2024
1 parent 6d2557b commit f3d38a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
7 changes: 7 additions & 0 deletions loader/include/Geode/utils/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <functional>
#include <string>
#include <vector>
#include <compare>

namespace geode::utils::string {
/**
Expand Down Expand Up @@ -64,4 +65,10 @@ namespace geode::utils::string {

GEODE_DLL bool startsWith(std::string const& str, std::string const& prefix);
GEODE_DLL bool endsWith(std::string const& str, std::string const& suffix);

/**
* Similar to strcmp, but case insensitive.
* Uses std::tolower, but could change in the future for better locale support
*/
GEODE_DLL std::strong_ordering caseInsensitiveCompare(std::string_view a, std::string_view b);
}
6 changes: 5 additions & 1 deletion loader/src/ui/mods/sources/ModListSource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ void filterModsWithLocalQuery(ModListSource::ProvidedMods& mods, Query const& qu
std::vector<std::pair<ModSource, double>> filtered;

// Filter installed mods based on query
// TODO: maybe skip fuzzy matching altogether if query is empty?
for (auto& src : mods.mods) {
double weighted = 0;
bool addToList = true;
Expand Down Expand Up @@ -226,7 +227,10 @@ void filterModsWithLocalQuery(ModListSource::ProvidedMods& mods, Query const& qu
return a.second > b.second;
}
// Sort secondarily alphabetically
return a.first.getMetadata().getName() < b.first.getMetadata().getName();
return utils::string::caseInsensitiveCompare(
a.first.getMetadata().getName(),
b.first.getMetadata().getName()
) == std::strong_ordering::less;
});

mods.mods.clear();
Expand Down
17 changes: 17 additions & 0 deletions loader/src/utils/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,20 @@ std::string utils::string::normalize(std::string const& str) {
auto ret = str;
return utils::string::normalizeIP(ret);
}

std::strong_ordering utils::string::caseInsensitiveCompare(std::string_view str1, std::string_view str2) {
for (size_t i = 0; i < str1.size() && i < str2.size(); i++) {
auto const a = std::tolower(str1[i]);
auto const b = std::tolower(str2[i]);
if (a < b) {
return std::strong_ordering::less;
} else if (a > b) {
return std::strong_ordering::greater;
}
}
if (str1.size() < str2.size())
return std::strong_ordering::less;
else if (str1.size() > str2.size())
return std::strong_ordering::greater;
return std::strong_ordering::equal;
}

0 comments on commit f3d38a7

Please sign in to comment.