Optimize StringView comparision operators
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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") {
|
||||
|
||||
Reference in New Issue
Block a user