Use compare opertators from string_view

Change-Id: I9da78dd15a6696a7061342cc1b1f1571eba74f19
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2022-07-13 18:13:29 +02:00
parent 2860e033ad
commit 63243aabc1
2 changed files with 25 additions and 28 deletions

View File

@@ -34,14 +34,7 @@ namespace Sqlite {
constexpr int compare(Utils::SmallStringView first, Utils::SmallStringView second) noexcept constexpr int compare(Utils::SmallStringView first, Utils::SmallStringView second) noexcept
{ {
auto difference = std::char_traits<char>::compare(first.data(), return first.compare(second);
second.data(),
std::min(first.size(), second.size()));
if (difference == 0)
return int(first.size() - second.size());
return difference;
} }
enum class UpdateChange { No, Update }; enum class UpdateChange { No, Update };

View File

@@ -113,35 +113,39 @@ public:
} }
}; };
constexpr bool operator==(SmallStringView first, SmallStringView second) noexcept
{
return first.size() == second.size()
&& std::char_traits<char>::compare(first.data(), second.data(), first.size()) == 0;
}
constexpr bool operator!=(SmallStringView first, SmallStringView second) noexcept constexpr bool operator!=(SmallStringView first, SmallStringView second) noexcept
{ {
return !(first == second); return std::string_view{first} != std::string_view{second};
} }
constexpr int compare(SmallStringView first, SmallStringView second) noexcept constexpr bool operator==(SmallStringView first, SmallStringView second) noexcept
{ {
int sizeDifference = int(first.size() - second.size()); return std::string_view{first} == std::string_view{second};
if (sizeDifference == 0)
return std::char_traits<char>::compare(first.data(), second.data(), first.size());
return sizeDifference;
} }
constexpr bool operator<(SmallStringView first, SmallStringView second) noexcept constexpr bool operator<(SmallStringView first, SmallStringView second) noexcept
{ {
return compare(first, second) < 0; return std::string_view{first} < std::string_view{second};
} }
constexpr bool operator>(SmallStringView first, SmallStringView second) noexcept constexpr bool operator>(SmallStringView first, SmallStringView second) noexcept
{ {
return second < first; return std::string_view{first} > std::string_view{second};
}
constexpr bool operator<=(SmallStringView first, SmallStringView second) noexcept
{
return std::string_view{first} <= std::string_view{second};
}
constexpr bool operator>=(SmallStringView first, SmallStringView second) noexcept
{
return std::string_view{first} >= std::string_view{second};
}
constexpr int compare(SmallStringView first, SmallStringView second) noexcept
{
return first.compare(second);
} }
namespace Internal { namespace Internal {
@@ -168,12 +172,12 @@ constexpr int reverse_memcmp(const char *first, const char *second, size_t n)
constexpr int reverseCompare(SmallStringView first, SmallStringView second) noexcept constexpr int reverseCompare(SmallStringView first, SmallStringView second) noexcept
{ {
int sizeDifference = int(first.size() - second.size()); int difference = Internal::reverse_memcmp(first.data(), second.data(), first.size());
if (sizeDifference == 0) if (difference == 0)
return Internal::reverse_memcmp(first.data(), second.data(), first.size()); return int(first.size()) - int(second.size());
return sizeDifference; return difference;
} }
} // namespace Utils } // namespace Utils