From a1815db9732fc0e8972f925fb6161e0ccb8e6952 Mon Sep 17 00:00:00 2001 From: James-Mart Date: Thu, 5 Sep 2024 16:03:34 +0000 Subject: [PATCH 1/2] add some concepts to blob compare to increase readability --- .../psibase/common/include/psibase/blob.hpp | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/libraries/psibase/common/include/psibase/blob.hpp b/libraries/psibase/common/include/psibase/blob.hpp index 7137d61b2..1d977f036 100644 --- a/libraries/psibase/common/include/psibase/blob.hpp +++ b/libraries/psibase/common/include/psibase/blob.hpp @@ -4,16 +4,29 @@ #include #include +template +concept IsByteType = + std::is_same_v, char> || std::is_same_v, unsigned char>; + +template +concept IsContainerLike = requires(T t) { + { + t.data() + } -> std::convertible_to; + { + t.size() + } -> std::convertible_to; +}; + namespace psibase { // Compare vector, string, string_view, shared_string (unsigned) template + requires IsContainerLike && IsContainerLike && + IsByteType().data())> && + IsByteType().data())> int compare_blob(const A& a, const B& b) { - static_assert(std::is_same_v, char> || - std::is_same_v, unsigned char>); - static_assert(std::is_same_v, char> || - std::is_same_v, unsigned char>); auto r = memcmp(a.data(), b.data(), std::min(a.size(), b.size())); if (r) return r; From 8903dcc9b6078f7b30b8ab673847355091ae7c76 Mon Sep 17 00:00:00 2001 From: James-Mart Date: Tue, 10 Sep 2024 19:35:38 +0000 Subject: [PATCH 2/2] pr feedback - improve blob compare concepts --- .../psibase/common/include/psibase/blob.hpp | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/libraries/psibase/common/include/psibase/blob.hpp b/libraries/psibase/common/include/psibase/blob.hpp index 1d977f036..14afb3042 100644 --- a/libraries/psibase/common/include/psibase/blob.hpp +++ b/libraries/psibase/common/include/psibase/blob.hpp @@ -2,37 +2,30 @@ #include #include +#include #include template -concept IsByteType = - std::is_same_v, char> || std::is_same_v, unsigned char>; +concept IsByteType = std::is_same_v || std::is_same_v; template -concept IsContainerLike = requires(T t) { - { - t.data() - } -> std::convertible_to; - { - t.size() - } -> std::convertible_to; -}; +concept IsByteRange = std::ranges::contiguous_range && std::ranges::sized_range && + IsByteType>>; namespace psibase { // Compare vector, string, string_view, shared_string (unsigned) template - requires IsContainerLike && IsContainerLike && - IsByteType().data())> && - IsByteType().data())> + requires IsByteRange && IsByteRange int compare_blob(const A& a, const B& b) { - auto r = memcmp(a.data(), b.data(), std::min(a.size(), b.size())); + auto r = memcmp(std::ranges::data(a), std::ranges::data(b), + std::min(std::ranges::size(a), std::ranges::size(b))); if (r) return r; - if (a.size() < b.size()) + if (std::ranges::size(a) < std::ranges::size(b)) return -1; - if (a.size() > b.size()) + if (std::ranges::size(a) > std::ranges::size(b)) return 1; return 0; }