Optimize StringView comparision operators

This commit is contained in:
Wojciech Figat
2021-12-03 15:36:04 +01:00
parent e17b6d1625
commit faaddccee4
2 changed files with 24 additions and 8 deletions

View File

@@ -277,7 +277,7 @@ public:
/// <returns>True if this string is lexicographically equivalent to the other, otherwise false.</returns>
FORCE_INLINE bool operator==(const StringView& other) const
{
return this->Compare(other) == 0;
return _length == other._length && (_length == 0 || StringUtils::Compare(_data, other._data, _length) == 0);
}
/// <summary>
@@ -287,7 +287,7 @@ public:
/// <returns>True if this string is lexicographically is not equivalent to the other, otherwise false.</returns>
FORCE_INLINE bool operator!=(const StringView& other) const
{
return this->Compare(other) != 0;
return !(*this == other);
}
/// <summary>
@@ -297,7 +297,7 @@ public:
/// <returns>True if this string is lexicographically equivalent to the other, otherwise false.</returns>
FORCE_INLINE bool operator==(const Char* other) const
{
return this->Compare(StringView(other)) == 0;
return *this == StringView(other);
}
/// <summary>
@@ -307,7 +307,7 @@ public:
/// <returns>True if this string is lexicographically is not equivalent to the other, otherwise false.</returns>
FORCE_INLINE bool operator!=(const Char* other) const
{
return this->Compare(StringView(other)) != 0;
return !(*this == StringView(other));
}
/// <summary>
@@ -466,7 +466,7 @@ public:
/// <returns>True if this string is lexicographically equivalent to the other, otherwise false.</returns>
FORCE_INLINE bool operator==(const StringAnsiView& other) const
{
return this->Compare(other) == 0;
return _length == other._length && (_length == 0 || StringUtils::Compare(_data, other._data, _length) == 0);
}
/// <summary>
@@ -476,7 +476,7 @@ public:
/// <returns>True if this string is lexicographically is not equivalent to the other, otherwise false.</returns>
FORCE_INLINE bool operator!=(const StringAnsiView& other) const
{
return this->Compare(other) != 0;
return !(*this == other);
}
/// <summary>
@@ -486,7 +486,7 @@ public:
/// <returns>True if this string is lexicographically equivalent to the other, otherwise false.</returns>
FORCE_INLINE bool operator==(const char* other) const
{
return this->Compare(StringAnsiView(other)) == 0;
return *this == StringAnsiView(other);
}
/// <summary>
@@ -496,7 +496,7 @@ public:
/// <returns>True if this string is lexicographically is not equivalent to the other, otherwise false.</returns>
FORCE_INLINE bool operator!=(const char* other) const
{
return this->Compare(StringAnsiView(other)) != 0;
return !(*this == StringAnsiView(other));
}
/// <summary>

View File

@@ -224,6 +224,14 @@ TEST_CASE("String Compare works") {
// Case differences
CHECK(String("a").Compare(String(TEXT("A")), StringSearchCase::CaseSensitive) > 0);
CHECK(String("A").Compare(String(TEXT("a")), StringSearchCase::CaseSensitive) < 0);
// Operators
CHECK(String(TEXT("")) == String(TEXT("")));
CHECK(String(TEXT("xx")) != String(TEXT("")));
CHECK(!(String(TEXT("abcx")) == String(TEXT("xxx"))));
CHECK(String(TEXT("abcx")) != String(TEXT("xxx")));
CHECK(String(TEXT("xxx")) == String(TEXT("xxx")));
CHECK(!(String(TEXT("xxx")) != String(TEXT("xxx"))));
}
SECTION("ignore case") {
@@ -291,6 +299,14 @@ TEST_CASE("String Compare works") {
// Case differences
CHECK(StringView(TEXT("a")).Compare(StringView(TEXT("A")), StringSearchCase::CaseSensitive) > 0);
CHECK(StringView(TEXT("A")).Compare(StringView(TEXT("a")), StringSearchCase::CaseSensitive) < 0);
// Operators
CHECK(StringView(TEXT("")) == StringView(TEXT("")));
CHECK(StringView(TEXT("xx")) != StringView(TEXT("")));
CHECK(!(StringView(TEXT("abcx")) == StringView(TEXT("xxx"))));
CHECK(StringView(TEXT("abcx")) != StringView(TEXT("xxx")));
CHECK(StringView(TEXT("xxx")) == StringView(TEXT("xxx")));
CHECK(!(StringView(TEXT("xxx")) != StringView(TEXT("xxx"))));
}
SECTION("ignore case") {