CCF/include/ccf/byte_vector.h

59 строки
1.3 KiB
C++

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache 2.0 License.
#pragma once
#include "ccf/ds/siphash.h"
#define FMT_HEADER_ONLY
#include <fmt/format.h>
#include <small_vector/SmallVector.h>
namespace ccf
{
using ByteVector = llvm_vecsmall::SmallVector<uint8_t, 8>;
}
namespace std
{
template <typename T, unsigned N>
struct hash<llvm_vecsmall::SmallVector<T, N>>
{
size_t operator()(const llvm_vecsmall::SmallVector<T, N>& v) const
{
static constexpr siphash::SipKey k{
0x7720796f726c694b, 0x2165726568207361};
return siphash::siphash<2, 4>(v.data(), v.size(), k);
}
};
}
FMT_BEGIN_NAMESPACE
template <>
struct formatter<ccf::ByteVector>
{
template <typename ParseContext>
constexpr auto parse(ParseContext& ctx)
{
return ctx.begin();
}
template <typename FormatContext>
auto format(const ccf::ByteVector& e, FormatContext& ctx) const
{
if (std::find(e.begin(), e.end(), '\0') != e.end())
{
return format_to(
ctx.out(), "<uint8[{}]: hex={:02x}>", e.size(), fmt::join(e, " "));
}
else
{
return format_to(
ctx.out(),
"<uint8[{}]: ascii={}>",
e.size(),
std::string(e.begin(), e.end()));
}
}
};
FMT_END_NAMESPACE