Skip to content

Commit

Permalink
StringUtil: Add CompareNoCase()
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Oct 12, 2024
1 parent dedc177 commit 2fc5856
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/common/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <charconv>
#include <cstddef>
#include <cstring>
#include <functional>
#include <iomanip>
#include <optional>
#include <span>
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit 2fc5856

Please sign in to comment.