Common: Add utility function for case-insensitive string comparison.

This commit is contained in:
Admiral H. Curtiss 2022-05-31 02:06:42 +02:00
parent 7586fc8134
commit bdb19085c4
No known key found for this signature in database
GPG Key ID: F051B4C4044F33FB
3 changed files with 12 additions and 11 deletions

View File

@ -704,4 +704,12 @@ void ToUpper(std::string* str)
{ {
std::transform(str->begin(), str->end(), str->begin(), [](char c) { return Common::ToUpper(c); }); std::transform(str->begin(), str->end(), str->begin(), [](char c) { return Common::ToUpper(c); });
} }
bool CaseInsensitiveEquals(std::string_view a, std::string_view b)
{
if (a.size() != b.size())
return false;
return std::equal(a.begin(), a.end(), b.begin(),
[](char ca, char cb) { return Common::ToLower(ca) == Common::ToLower(cb); });
}
} // namespace Common } // namespace Common

View File

@ -264,4 +264,5 @@ inline char ToUpper(char ch)
} }
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);
} // namespace Common } // namespace Common

View File

@ -320,14 +320,6 @@ static void ApplyPatchToFile(const Patch& patch, const File& file_patch,
file_patch.m_fileoffset, file_patch.m_length, file_patch.m_resize); file_patch.m_fileoffset, file_patch.m_length, file_patch.m_resize);
} }
static bool CaseInsensitiveEquals(std::string_view a, std::string_view b)
{
if (a.size() != b.size())
return false;
return std::equal(a.begin(), a.end(), b.begin(),
[](char ca, char cb) { return Common::ToLower(ca) == Common::ToLower(cb); });
}
static FSTBuilderNode* FindFileNodeInFST(std::string_view path, std::vector<FSTBuilderNode>* fst, static FSTBuilderNode* FindFileNodeInFST(std::string_view path, std::vector<FSTBuilderNode>* fst,
bool create_if_not_exists) bool create_if_not_exists)
{ {
@ -335,7 +327,7 @@ static FSTBuilderNode* FindFileNodeInFST(std::string_view path, std::vector<FSTB
const bool is_file = path_separator == std::string_view::npos; const bool is_file = path_separator == std::string_view::npos;
const std::string_view name = is_file ? path : path.substr(0, path_separator); const std::string_view name = is_file ? path : path.substr(0, path_separator);
const auto it = std::find_if(fst->begin(), fst->end(), [&](const FSTBuilderNode& node) { const auto it = std::find_if(fst->begin(), fst->end(), [&](const FSTBuilderNode& node) {
return CaseInsensitiveEquals(node.m_filename, name); return Common::CaseInsensitiveEquals(node.m_filename, name);
}); });
if (it == fst->end()) if (it == fst->end())
@ -377,7 +369,7 @@ static DiscIO::FSTBuilderNode* FindFilenameNodeInFST(std::string_view filename,
if (result) if (result)
return result; return result;
} }
else if (CaseInsensitiveEquals(node.m_filename, filename)) else if (Common::CaseInsensitiveEquals(node.m_filename, filename))
{ {
return &node; return &node;
} }
@ -398,7 +390,7 @@ static void ApplyFilePatchToFST(const Patch& patch, const File& file,
if (node) if (node)
ApplyPatchToFile(patch, file, node); ApplyPatchToFile(patch, file, node);
} }
else if (dol_node && CaseInsensitiveEquals(file.m_disc, "main.dol")) else if (dol_node && Common::CaseInsensitiveEquals(file.m_disc, "main.dol"))
{ {
// Special case: If the filename is "main.dol", we want to patch the main executable. // Special case: If the filename is "main.dol", we want to patch the main executable.
ApplyPatchToFile(patch, file, dol_node); ApplyPatchToFile(patch, file, dol_node);