9
9
#ifndef {{"_".join(config.protocol.namespace)}}_encoding_h
10
10
#define {{"_".join(config.protocol.namespace)}}_encoding_h
11
11
12
+ #include <algorithm>
12
13
#include <cstddef>
13
14
#include <cstdint>
14
15
#include <cstring>
@@ -23,6 +24,19 @@ namespace {{namespace}} {
23
24
24
25
// ===== encoding/encoding.h =====
25
26
27
+ // This library is designed to be portable. The only allowed dependency
28
+ // are the C/C++ standard libraries, up to C++11. We support both 32 bit
29
+ // and 64 architectures.
30
+ //
31
+ // Types used below:
32
+ // uint8_t: a byte, e.g. for raw bytes or UTF8 characters
33
+ // uint16_t: two bytes, e.g. for UTF16 characters
34
+ // For input parameters:
35
+ // span<uint8_t>: pointer to bytes and length
36
+ // span<uint16_t>: pointer to UTF16 chars and length
37
+ // For output parameters:
38
+ // std::vector<uint8_t> - Owned segment of bytes / utf8 characters and length.
39
+ // std::string - Same, for compatibility, even though char is signed.
26
40
27
41
// =============================================================================
28
42
// span - sequence of bytes
@@ -81,6 +95,22 @@ inline span<uint8_t> SpanFrom(const std::string& v) {
81
95
return span<uint8_t>(reinterpret_cast<const uint8_t*>(v.data()), v.size());
82
96
}
83
97
98
+ // Less than / equality comparison functions for sorting / searching for byte
99
+ // spans. These are similar to absl::string_view's < and == operators.
100
+ inline bool SpanLessThan(span<uint8_t> x, span<uint8_t> y) noexcept {
101
+ auto min_size = std::min(x.size(), y.size());
102
+ const int r = min_size == 0 ? 0 : memcmp(x.data(), y.data(), min_size);
103
+ return (r < 0) || (r == 0 && x.size() < y.size());
104
+ }
105
+
106
+ inline bool SpanEquals(span<uint8_t> x, span<uint8_t> y) noexcept {
107
+ auto len = x.size();
108
+ if (len != y.size())
109
+ return false;
110
+ return x.data() == y.data() || len == 0 ||
111
+ std::memcmp(x.data(), y.data(), len) == 0;
112
+ }
113
+
84
114
// =============================================================================
85
115
// Status and Error codes
86
116
// =============================================================================
0 commit comments