StringUtil: Add CompareNoCase()

This commit is contained in:
Stenzek 2024-10-12 16:21:34 +10:00
parent dedc177300
commit 2fc5856c44
No known key found for this signature in database
1 changed files with 12 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#include <charconv>
#include <cstddef>
#include <cstring>
#include <functional>
#include <iomanip>
#include <optional>
#include <span>
@ -68,6 +69,17 @@ static inline bool EqualNoCase(std::string_view s1, std::string_view s2)
return (Strncasecmp(s1.data(), s2.data(), s1.length()) == 0);
}
static inline int CompareNoCase(std::string_view s1, std::string_view s2)
{
const size_t s1_len = s1.length();
const size_t s2_len = s2.length();
if (s1_len != s2_len)
return false;
const size_t compare_len = std::min(s1_len, s2_len);
const int compare_res = Strncasecmp(s1.data(), s2.data(), compare_len);
return (compare_len != 0) ? compare_res : ((s1_len < s2_len) ? -1 : ((s1_len > s2_len) ? 1 : 0));
}
/// Wrapper around std::from_chars
template<typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>