Fix CStringExtras::CompareCaseInsensitive, `std::lexicographical_compare` does not have the intended behavior

This commit is contained in:
Phillip Stephens 2019-09-28 08:00:49 -07:00
parent 4b25d58caf
commit b60ad339d5
Signed by: Antidote
GPG Key ID: F8BEE4C83DACA60D
1 changed files with 4 additions and 5 deletions

View File

@ -9,21 +9,20 @@ namespace urde {
class CStringExtras { class CStringExtras {
public: public:
// Checks if the provided views into string data can be considered equal or not based on // Checks if the provided views into string data can be considered equal or not based on
// whether or not all their characters are lexicographically equal to one another in // whether or not all their characters are equal to one another in a character insensitive manner.
// a character insensitive manner.
// //
// NOTE: This differs slightly from the actual version of this function within the game executable // NOTE: This differs slightly from the actual version of this function within the game executable
// in order to better accomodate string views and potentially non-null-terminated string data. // in order to better accomodate string views and potentially non-null-terminated string data.
// //
// In the game executable, the function essentially behaves like strcasecmp in that it returns // In the game executable, the function essentially behaves like strcasecmp in that it returns
// an int indicating whether or not the first argument is lexicographically less than, equal to, // an int indicating whether or not the first argument is less than, equal to,
// or greater than the second argument. Given no usages in the code depend on the less than or // or greater than the second argument. Given no usages in the code depend on the less than or
// greater than cases, but rather just care about whether or not the strings are equal to one // greater than cases, but rather just care about whether or not the strings are equal to one
// another, this is a safe change to make. // another, this is a safe change to make.
// //
static bool CompareCaseInsensitive(std::string_view a, std::string_view b) { static bool CompareCaseInsensitive(std::string_view a, std::string_view b) {
return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), [](char lhs, char rhs) { return std::equal(a.begin(), a.end(), b.begin(), b.end(), [](char lhs, char rhs) {
return std::tolower(static_cast<unsigned char>(lhs)) < std::tolower(static_cast<unsigned char>(rhs)); return std::tolower(lhs) == std::tolower(rhs);
}); });
} }