diff --git a/Source/Core/Common/IniFile.cpp b/Source/Core/Common/IniFile.cpp index 65deaa1573..74f43dc165 100644 --- a/Source/Core/Common/IniFile.cpp +++ b/Source/Core/Common/IniFile.cpp @@ -5,16 +5,13 @@ #include "Common/IniFile.h" #include -#include #include -#include #include #include #include #include #include -#include "Common/CommonTypes.h" #include "Common/FileUtil.h" #include "Common/StringUtil.h" @@ -128,16 +125,22 @@ IniFile::~IniFile() = default; const IniFile::Section* IniFile::GetSection(const std::string& sectionName) const { for (const Section& sect : sections) - if (!strcasecmp(sect.name.c_str(), sectionName.c_str())) - return (&(sect)); + { + if (CaseInsensitiveStringCompare::IsEqual(sect.name, sectionName)) + return § + } + return nullptr; } IniFile::Section* IniFile::GetSection(const std::string& sectionName) { for (Section& sect : sections) - if (!strcasecmp(sect.name.c_str(), sectionName.c_str())) - return (&(sect)); + { + if (CaseInsensitiveStringCompare::IsEqual(sect.name, sectionName)) + return § + } + return nullptr; } diff --git a/Source/Core/Common/IniFile.h b/Source/Core/Common/IniFile.h index f23a52fcb0..3b87501d29 100644 --- a/Source/Core/Common/IniFile.h +++ b/Source/Core/Common/IniFile.h @@ -4,21 +4,38 @@ #pragma once -#include +#include +#include #include #include #include +#include #include -#include "Common/CommonFuncs.h" #include "Common/CommonTypes.h" #include "Common/StringUtil.h" struct CaseInsensitiveStringCompare { - bool operator()(const std::string& a, const std::string& b) const + // Allow heterogenous lookup. + using is_transparent = void; + + bool operator()(std::string_view a, std::string_view b) const { - return strcasecmp(a.c_str(), b.c_str()) < 0; + return std::lexicographical_compare( + a.begin(), a.end(), b.begin(), b.end(), [](char lhs, char rhs) { + return std::tolower(static_cast(lhs)) < std::tolower(static_cast(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 std::tolower(static_cast(lhs)) == std::tolower(static_cast(rhs)); + }); } };