mirror of https://github.com/AxioDL/metaforce.git
hecl/hecl: Handle bounded strings within CaseInsensitiveCompare
std::string_view instances aren't guaranteed to be null-terminated, so we shouldn't be treating them as if they are in these functions, and should instead use a bounded comparison based off their sizes. This way we prevent an edge-case from ever becoming a problem and also remove an ifdef, making the code uniform across all implementations.
This commit is contained in:
parent
9dc1373201
commit
91ff474c44
|
@ -22,20 +22,22 @@ extern "C" int rep_closefrom(int lower);
|
||||||
#endif
|
#endif
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#include <cwchar>
|
#include <cwchar>
|
||||||
|
#include <cwctype>
|
||||||
#include <Shlwapi.h>
|
#include <Shlwapi.h>
|
||||||
#include "winsupport.hpp"
|
#include "winsupport.hpp"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cinttypes>
|
#include <cinttypes>
|
||||||
#include <ctime>
|
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <ctime>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <string>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <regex>
|
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <regex>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "logvisor/logvisor.hpp"
|
#include "logvisor/logvisor.hpp"
|
||||||
#include "athena/Global.hpp"
|
#include "athena/Global.hpp"
|
||||||
#include "../extern/boo/xxhash/xxhash.h"
|
#include "../extern/boo/xxhash/xxhash.h"
|
||||||
|
@ -469,20 +471,16 @@ public:
|
||||||
*/
|
*/
|
||||||
struct CaseInsensitiveCompare {
|
struct CaseInsensitiveCompare {
|
||||||
bool operator()(std::string_view lhs, std::string_view rhs) const {
|
bool operator()(std::string_view lhs, std::string_view rhs) const {
|
||||||
#if _WIN32
|
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), [](char lhs, char rhs) {
|
||||||
if (_stricmp(lhs.data(), rhs.data()) < 0)
|
return std::tolower(static_cast<unsigned char>(lhs)) < std::tolower(static_cast<unsigned char>(rhs));
|
||||||
#else
|
});
|
||||||
if (strcasecmp(lhs.data(), rhs.data()) < 0)
|
|
||||||
#endif
|
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if _WIN32
|
#if _WIN32
|
||||||
bool operator()(std::wstring_view lhs, std::wstring_view rhs) const {
|
bool operator()(std::wstring_view lhs, std::wstring_view rhs) const {
|
||||||
if (_wcsicmp(lhs.data(), rhs.data()) < 0)
|
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), [](wchar_t lhs, wchar_t rhs) {
|
||||||
return true;
|
return std::towlower(lhs) < std::towlower(rhs);
|
||||||
return false;
|
});
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue