IniFile: Migrate Common::CaseInsensitiveLess to StringUtil

Migrating `Common::CaseInsensitiveLess` to StringUtil.h will hopefully discourage rolling one's own solution in the future for case-insensitive associative containers when this (quite robust!) solution already exists.

`Common::CaseInsensitiveStringCompare::IsEqual` was removed in favor of using the `Common::CaseInsensitiveEquals` function.

The `a.size() != b.size()` condition in `Common::CaseInsensitiveEquals` can be removed, since `std::ranges::equal` already checks this condition (confirmed in libc++).
This commit is contained in:
mitaclaw
2024-09-21 22:32:24 -07:00
parent 067bdaa3dd
commit 508ccc2054
4 changed files with 19 additions and 30 deletions

View File

@ -15,29 +15,6 @@
namespace Common
{
struct CaseInsensitiveStringCompare
{
// Allow heterogenous lookup.
using is_transparent = void;
bool operator()(std::string_view a, std::string_view b) const
{
return std::lexicographical_compare(
a.begin(), a.end(), b.begin(), b.end(),
[](char lhs, char rhs) { return Common::ToLower(lhs) < Common::ToLower(rhs); });
}
static bool IsEqual(std::string_view a, std::string_view b)
{
if (a.size() != b.size())
return false;
return std::equal(a.begin(), a.end(), b.begin(), b.end(), [](char lhs, char rhs) {
return Common::ToLower(lhs) == Common::ToLower(rhs);
});
}
};
class IniFile
{
public:
@ -86,7 +63,7 @@ public:
bool GetLines(std::vector<std::string>* lines, const bool remove_comments = true) const;
bool operator<(const Section& other) const { return name < other.name; }
using SectionMap = std::map<std::string, std::string, CaseInsensitiveStringCompare>;
using SectionMap = std::map<std::string, std::string, CaseInsensitiveLess>;
const std::string& GetName() const { return name; }
const SectionMap& GetValues() const { return values; }