Skip to content

Commit

Permalink
Merge pull request #837 from gofractally/add-blob-compare-concepts
Browse files Browse the repository at this point in the history
add some concepts to blob compare
  • Loading branch information
James-Mart authored Sep 10, 2024
2 parents abf75dc + 8903dcc commit 395c63b
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions libraries/psibase/common/include/psibase/blob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@

#include <string.h>
#include <algorithm>
#include <ranges>
#include <type_traits>

template <typename T>
concept IsByteType = std::is_same_v<T, char> || std::is_same_v<T, unsigned char>;

template <typename T>
concept IsByteRange = std::ranges::contiguous_range<T> && std::ranges::sized_range<T> &&
IsByteType<std::remove_cvref_t<std::ranges::range_value_t<T>>>;

namespace psibase
{
// Compare vector, string, string_view, shared_string (unsigned)
template <typename A, typename B>
requires IsByteRange<A> && IsByteRange<B>
int compare_blob(const A& a, const B& b)
{
static_assert(std::is_same_v<std::decay_t<decltype(*a.data())>, char> ||
std::is_same_v<std::decay_t<decltype(*a.data())>, unsigned char>);
static_assert(std::is_same_v<std::decay_t<decltype(*b.data())>, char> ||
std::is_same_v<std::decay_t<decltype(*b.data())>, unsigned char>);
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;
}
Expand Down

0 comments on commit 395c63b

Please sign in to comment.