CCF
Loading...
Searching...
No Matches
byte_vector.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "ccf/ds/siphash.h"
6
7#define FMT_HEADER_ONLY
8#include <fmt/format.h>
9#include <small_vector/SmallVector.h>
10
11namespace ccf
12{
13 using ByteVector = llvm_vecsmall::SmallVector<uint8_t, 8>;
14}
15
16namespace std
17{
18 template <typename T, unsigned N>
19 struct hash<llvm_vecsmall::SmallVector<T, N>>
20 {
21 size_t operator()(const llvm_vecsmall::SmallVector<T, N>& v) const
22 {
23 static constexpr ccf::siphash::SipKey k{
24 0x7720796f726c694b, 0x2165726568207361};
25 return ccf::siphash::siphash<2, 4>(v.data(), v.size(), k);
26 }
27 };
28}
29
30FMT_BEGIN_NAMESPACE
31template <>
32struct formatter<ccf::ByteVector>
33{
34 template <typename ParseContext>
35 constexpr auto parse(ParseContext& ctx)
36 {
37 return ctx.begin();
38 }
39
40 template <typename FormatContext>
41 auto format(const ccf::ByteVector& e, FormatContext& ctx) const
42 {
43 // This is the same as std::isprint, but independent of the current locale.
44 auto printable = [](uint8_t b) { return b >= 0x20 && b <= 0x7e; };
45 if (std::all_of(e.begin(), e.end(), printable))
46 {
47 return format_to(
48 ctx.out(),
49 "<uint8[{}]: ascii={}>",
50 e.size(),
51 std::string(e.begin(), e.end()));
52 }
53 else
54 {
55 return format_to(
56 ctx.out(), "<uint8[{}]: hex={:02x}>", e.size(), fmt::join(e, " "));
57 }
58 }
59};
60FMT_END_NAMESPACE
uint64_t[2] SipKey
Definition siphash.h:13
Definition app_interface.h:15
llvm_vecsmall::SmallVector< uint8_t, 8 > ByteVector
Definition byte_vector.h:13
STL namespace.
constexpr auto parse(ParseContext &ctx)
Definition byte_vector.h:35
auto format(const ccf::ByteVector &e, FormatContext &ctx) const
Definition byte_vector.h:41
size_t operator()(const llvm_vecsmall::SmallVector< T, N > &v) const
Definition byte_vector.h:21