Merge pull request #13068 from mitaclaw/redundant-case-insensitive

IniFile: Migrate `Common::CaseInsensitiveLess` to StringUtil
This commit is contained in:
JMC47 2024-09-26 18:24:36 -04:00 committed by GitHub
commit b1cd4a6690
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 30 deletions

View File

@ -130,7 +130,7 @@ const IniFile::Section* IniFile::GetSection(std::string_view section_name) const
{ {
for (const Section& sect : sections) for (const Section& sect : sections)
{ {
if (CaseInsensitiveStringCompare::IsEqual(sect.name, section_name)) if (CaseInsensitiveEquals(sect.name, section_name))
return § return §
} }
@ -141,7 +141,7 @@ IniFile::Section* IniFile::GetSection(std::string_view section_name)
{ {
for (Section& sect : sections) for (Section& sect : sections)
{ {
if (CaseInsensitiveStringCompare::IsEqual(sect.name, section_name)) if (CaseInsensitiveEquals(sect.name, section_name))
return § return §
} }

View File

@ -15,29 +15,6 @@
namespace Common 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 class IniFile
{ {
public: public:
@ -86,7 +63,7 @@ public:
bool GetLines(std::vector<std::string>* lines, const bool remove_comments = true) const; bool GetLines(std::vector<std::string>* lines, const bool remove_comments = true) const;
bool operator<(const Section& other) const { return name < other.name; } 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 std::string& GetName() const { return name; }
const SectionMap& GetValues() const { return values; } const SectionMap& GetValues() const { return values; }

View File

@ -666,10 +666,14 @@ void ToUpper(std::string* str)
bool CaseInsensitiveEquals(std::string_view a, std::string_view b) bool CaseInsensitiveEquals(std::string_view a, std::string_view b)
{ {
if (a.size() != b.size()) return std::ranges::equal(
return false; a, b, [](char ca, char cb) { return Common::ToLower(ca) == Common::ToLower(cb); });
return std::equal(a.begin(), a.end(), b.begin(), }
[](char ca, char cb) { return Common::ToLower(ca) == Common::ToLower(cb); });
bool CaseInsensitiveLess::operator()(std::string_view a, std::string_view b) const
{
return std::ranges::lexicographical_compare(
a, b, [](char ca, char cb) { return Common::ToLower(ca) < Common::ToLower(cb); });
} }
std::string BytesToHexString(std::span<const u8> bytes) std::string BytesToHexString(std::span<const u8> bytes)

View File

@ -313,5 +313,13 @@ std::string GetEscapedHtml(std::string html);
void ToLower(std::string* str); void ToLower(std::string* str);
void ToUpper(std::string* str); void ToUpper(std::string* str);
bool CaseInsensitiveEquals(std::string_view a, std::string_view b); bool CaseInsensitiveEquals(std::string_view a, std::string_view b);
// 'std::less'-like comparison function object type for case-insensitive strings.
struct CaseInsensitiveLess
{
using is_transparent = void; // Allow heterogenous lookup.
bool operator()(std::string_view a, std::string_view b) const;
};
std::string BytesToHexString(std::span<const u8> bytes); std::string BytesToHexString(std::span<const u8> bytes);
} // namespace Common } // namespace Common