mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2026-10-23 20:18:10 +00:00
fix: make StringEqualsIgnoreCase constexpr (#1021)
* fix: make StringEqualsIgnoreCase constexpr Move utils::StringEqualsIgnoreCase into the header as a constexpr function using an ASCII-only lowercase helper, so it can be used from constexpr contexts such as InfoString's comparators with newer MSVC toolsets. * test: add StringEqualsIgnoreCase tests
This commit is contained in:
1 parent
bc0a786444
commit
28b14ea0ee
3 files changed
+38
-14
No files matched your search
@@ -4,14 +4,6 @@
|
||||
#include <cctype>
|
||||
#include <sstream>
|
||||
|
||||
namespace
|
||||
{
|
||||
bool ichar_equals(const char a, const char b)
|
||||
{
|
||||
return std::tolower(static_cast<unsigned char>(a)) == std::tolower(static_cast<unsigned char>(b));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace utils
|
||||
{
|
||||
std::string EscapeStringForQuotationMarks(const std::string_view& str)
|
||||
@@ -123,11 +115,6 @@ namespace utils
|
||||
c = static_cast<char>(toupper(static_cast<unsigned char>(c)));
|
||||
}
|
||||
|
||||
bool StringEqualsIgnoreCase(std::string_view lhs, std::string_view rhs)
|
||||
{
|
||||
return std::ranges::equal(lhs, rhs, ichar_equals);
|
||||
}
|
||||
|
||||
void StringTrimL(std::string& str)
|
||||
{
|
||||
str.erase(str.begin(),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
@@ -20,7 +21,20 @@ namespace utils
|
||||
void MakeStringUpperCase(char* str);
|
||||
void MakeStringUpperCase(std::string& str);
|
||||
|
||||
bool StringEqualsIgnoreCase(std::string_view lhs, std::string_view rhs);
|
||||
constexpr char CharToLowerAscii(const char c)
|
||||
{
|
||||
return c >= 'A' && c <= 'Z' ? static_cast<char>(c - 'A' + 'a') : c;
|
||||
}
|
||||
|
||||
constexpr bool StringEqualsIgnoreCase(const std::string_view lhs, const std::string_view rhs)
|
||||
{
|
||||
return std::ranges::equal(lhs,
|
||||
rhs,
|
||||
[](const char a, const char b)
|
||||
{
|
||||
return CharToLowerAscii(a) == CharToLowerAscii(b);
|
||||
});
|
||||
}
|
||||
|
||||
void StringTrimL(std::string& str);
|
||||
void StringTrimR(std::string& str);
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#include "Utils/StringUtils.h"
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
static_assert(utils::StringEqualsIgnoreCase("", ""));
|
||||
static_assert(utils::StringEqualsIgnoreCase("Hello World", "hELLO wORLD"));
|
||||
static_assert(!utils::StringEqualsIgnoreCase("Hello", "Hello World"));
|
||||
static_assert(!utils::StringEqualsIgnoreCase("[", "{"));
|
||||
|
||||
TEST_CASE("StringUtils: StringEqualsIgnoreCase", "[stringutils]")
|
||||
{
|
||||
SECTION("matches strings that only differ in case")
|
||||
{
|
||||
REQUIRE(utils::StringEqualsIgnoreCase("mp_rust", "MP_Rust"));
|
||||
REQUIRE(utils::StringEqualsIgnoreCase("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"));
|
||||
}
|
||||
|
||||
SECTION("does not match different strings")
|
||||
{
|
||||
REQUIRE_FALSE(utils::StringEqualsIgnoreCase("mp_rust", "mp_rust2"));
|
||||
REQUIRE_FALSE(utils::StringEqualsIgnoreCase("@", "`"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user