From b011b8085deee8efb768904b5e0cd89fd950edf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Wed, 5 Jul 2023 23:07:36 +0300 Subject: [PATCH] Move IEquals to String.cpp and cleanup the implementation --- src/openrct2/core/String.cpp | 93 ++++++++++++++---------------------- src/openrct2/core/String.hpp | 21 +++----- 2 files changed, 44 insertions(+), 70 deletions(-) diff --git a/src/openrct2/core/String.cpp b/src/openrct2/core/String.cpp index e9a27b68c3..978e3b4803 100644 --- a/src/openrct2/core/String.cpp +++ b/src/openrct2/core/String.cpp @@ -150,66 +150,28 @@ namespace String return strcmp(a, b); } - bool Equals(std::string_view a, std::string_view b, bool ignoreCase) + template static bool EqualsImpl(TString&& lhs, TString&& rhs, bool ignoreCase) { - if (ignoreCase) - { - if (a.size() == b.size()) + return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), [ignoreCase](auto a, auto b) { + const auto first = static_cast(a); + const auto second = static_cast(b); + if (((first | second) & 0x80) != 0) { - for (size_t i = 0; i < a.size(); i++) - { - if (tolower(static_cast(a[i])) != tolower(static_cast(b[i]))) - { - return false; - } - } - return true; + // Only do case insensitive comparison on ASCII characters + return first == second; } - - return false; - } - - return a == b; + return ignoreCase ? (tolower(first) == tolower(second)) : (first == second); + }); } - bool Equals(const std::string& a, const std::string& b, bool ignoreCase) + bool Equals(std::string_view a, std::string_view b) { - if (a.size() != b.size()) - return false; + return EqualsImpl(a, b, false); + } - if (ignoreCase) - { - for (size_t i = 0; i < a.size(); i++) - { - auto ai = a[i]; - auto bi = b[i]; - - // Only do case insensitive comparison on ASCII characters - if ((ai & 0x80) != 0 || (bi & 0x80) != 0) - { - if (a[i] != b[i]) - { - return false; - } - } - else if (tolower(static_cast(ai)) != tolower(static_cast(bi))) - { - return false; - } - } - } - else - { - for (size_t i = 0; i < a.size(); i++) - { - if (a[i] != b[i]) - { - return false; - } - } - } - - return true; + bool Equals(const std::string& a, const std::string& b) + { + return EqualsImpl(a, b, false); } bool Equals(const utf8* a, const utf8* b, bool ignoreCase) @@ -227,12 +189,31 @@ namespace String return strcmp(a, b) == 0; } + bool IEquals(std::string_view a, std::string_view b) + { + return EqualsImpl(a, b, true); + } + + bool IEquals(const std::string& a, const std::string& b) + { + return EqualsImpl(a, b, true); + } + + bool IEquals(const utf8* a, const utf8* b) + { + if (a == b) + return true; + if (a == nullptr || b == nullptr) + return false; + return _stricmp(a, b) == 0; + } + bool StartsWith(std::string_view str, std::string_view match, bool ignoreCase) { if (str.size() >= match.size()) { auto view = str.substr(0, match.size()); - return Equals(view, match, ignoreCase); + return EqualsImpl(view, match, ignoreCase); } return false; } @@ -242,7 +223,7 @@ namespace String if (str.size() >= match.size()) { auto view = str.substr(str.size() - match.size()); - return Equals(view, match, ignoreCase); + return EqualsImpl(view, match, ignoreCase); } return false; } @@ -259,7 +240,7 @@ namespace String for (size_t start = 0; start <= end; start++) { auto sub = haystack.substr(start, needle.size()); - if (Equals(sub, needle, ignoreCase)) + if (EqualsImpl(sub, needle, ignoreCase)) { return true; } diff --git a/src/openrct2/core/String.hpp b/src/openrct2/core/String.hpp index 5213823813..bc22a88f58 100644 --- a/src/openrct2/core/String.hpp +++ b/src/openrct2/core/String.hpp @@ -56,21 +56,14 @@ namespace String bool IsNullOrEmpty(const utf8* str); int32_t Compare(const std::string& a, const std::string& b, bool ignoreCase = false); int32_t Compare(const utf8* a, const utf8* b, bool ignoreCase = false); - bool Equals(std::string_view a, std::string_view b, bool ignoreCase = false); - inline bool IEquals(std::string_view a, std::string_view b) - { - return Equals(a, b, true); - } - bool Equals(const std::string& a, const std::string& b, bool ignoreCase = false); - inline bool IEquals(const std::string& a, const std::string& b) - { - return Equals(a, b, true); - } + + bool Equals(std::string_view a, std::string_view b); + bool Equals(const std::string& a, const std::string& b); bool Equals(const utf8* a, const utf8* b, bool ignoreCase = false); - inline bool IEquals(const utf8* a, const utf8* b) - { - return Equals(a, b, true); - } + bool IEquals(std::string_view a, std::string_view b); + bool IEquals(const std::string& a, const std::string& b); + bool IEquals(const utf8* a, const utf8* b); + bool StartsWith(std::string_view str, std::string_view match, bool ignoreCase = false); bool EndsWith(std::string_view str, std::string_view match, bool ignoreCase = false); bool Contains(std::string_view haystack, std::string_view needle, bool ignoreCase = false);