From 992f21133f83f6e0a2a99136906cb2e07d6c0842 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Thu, 12 May 2022 10:40:31 +0200 Subject: Add caseSensitiveCompareStrings and manual <=> and == operator for CacheKey MacOS clang compiler does not implement a default <=> operator for string --- zencore/include/zencore/string.h | 23 +++++++++++++++++++++++ zenutil/include/zenutil/cache/cachekey.h | 14 ++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/zencore/include/zencore/string.h b/zencore/include/zencore/string.h index 012ee73ee..fe838ac19 100644 --- a/zencore/include/zencore/string.h +++ b/zencore/include/zencore/string.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -795,6 +796,28 @@ StrCaseCompare(const char* Lhs, const char* Rhs, int64_t Length = -1) #endif } +/** + * @brief + * Helper function to implement case sensitive spaceship operator for strings. + * MacOS clang version we use does not implement <=> for std::string + * @param Lhs string + * @param Rhs string + * @return std::strong_ordering indicating relationship between Lhs and Rhs + */ +inline auto +caseSensitiveCompareStrings(const std::string& Lhs, const std::string& Rhs) +{ + if (Lhs == Rhs) + { + return std::strong_ordering::equal; + } + if (Lhs < Rhs) + { + return std::strong_ordering::less; + } + return std::strong_ordering::greater; +} + ////////////////////////////////////////////////////////////////////////// /** diff --git a/zenutil/include/zenutil/cache/cachekey.h b/zenutil/include/zenutil/cache/cachekey.h index 569a31441..427c99435 100644 --- a/zenutil/include/zenutil/cache/cachekey.h +++ b/zenutil/include/zenutil/cache/cachekey.h @@ -23,13 +23,19 @@ struct CacheKey auto operator<=>(const CacheKey& that) const { - if (auto cmp = Namespace <=> that.Namespace; cmp != 0) - return cmp; - if (auto cmp = Bucket <=> that.Bucket; cmp != 0) - return cmp; + if (auto n = caseSensitiveCompareStrings(Namespace, that.Namespace); n != std::strong_ordering::equal) + { + return n; + } + if (auto b = caseSensitiveCompareStrings(Bucket, that.Bucket); b != std::strong_ordering::equal) + { + return b; + } return Hash <=> that.Hash; } + auto operator==(const CacheKey& that) const { return (*this <=> that) == std::strong_ordering::equal; } + static const CacheKey Empty; }; -- cgit v1.2.3