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 <fmt/ranges.h>
10#include <small_vector/SmallVector.h>
11
12namespace ccf
13{
14 using ByteVector = llvm_vecsmall::SmallVector<uint8_t, 8>;
15}
16
17namespace std
18{
19 template <typename T, unsigned N>
20 struct hash<llvm_vecsmall::SmallVector<T, N>>
21 {
22 size_t operator()(const llvm_vecsmall::SmallVector<T, N>& v) const
23 {
24 static constexpr ccf::siphash::SipKey k{
25 0x7720796f726c694b, 0x2165726568207361};
26 return ccf::siphash::siphash<2, 4>(v.data(), v.size(), k);
27 }
28 };
29}
30
31FMT_BEGIN_NAMESPACE
32template <>
33struct formatter<ccf::ByteVector>
34{
35 template <typename ParseContext>
36 constexpr auto parse(ParseContext& ctx)
37 {
38 return ctx.begin();
39 }
40
41 template <typename FormatContext>
42 auto format(const ccf::ByteVector& e, FormatContext& ctx) const
43 {
44 // This is the same as std::isprint, but independent of the current locale.
45 auto printable = [](uint8_t b) { return b >= 0x20 && b <= 0x7e; };
46 if (std::all_of(e.begin(), e.end(), printable))
47 {
48 return format_to(
49 ctx.out(),
50 "<uint8[{}]: ascii={}>",
51 e.size(),
52 std::string(e.begin(), e.end()));
53 }
54 else
55 {
56 return format_to(
57 ctx.out(), "<uint8[{}]: hex={:02x}>", e.size(), fmt::join(e, " "));
58 }
59 }
60};
61FMT_END_NAMESPACE
uint64_t[2] SipKey
Definition siphash.h:13
Definition app_interface.h:14
llvm_vecsmall::SmallVector< uint8_t, 8 > ByteVector
Definition byte_vector.h:14
STL namespace.
constexpr auto parse(ParseContext &ctx)
Definition byte_vector.h:36
auto format(const ccf::ByteVector &e, FormatContext &ctx) const
Definition byte_vector.h:42
size_t operator()(const llvm_vecsmall::SmallVector< T, N > &v) const
Definition byte_vector.h:22