1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
base / containers / span_reader.h [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_CONTAINERS_SPAN_READER_H_
#define BASE_CONTAINERS_SPAN_READER_H_
#include <concepts>
#include <optional>
#include "base/containers/span.h"
#include "base/memory/stack_allocated.h"
#include "base/numerics/byte_conversions.h"
#include "base/numerics/safe_conversions.h"
namespace base {
// A Reader to consume elements from the front of a span dynamically.
//
// SpanReader is used to split off prefix spans from a larger span, reporting
// errors if there's not enough room left (instead of crashing, as would happen
// with span directly).
template <class T>
class SpanReader {
STACK_ALLOCATED();
public:
// Construct SpanReader from a span.
explicit SpanReader(span<T> buf) : buf_(buf), original_size_(buf_.size()) {}
// Returns a span over the next `n` objects, if there are enough objects left.
// Otherwise, it returns nullopt and does nothing.
std::optional<span<T>> Read(StrictNumeric<size_t> n) {
if (n > remaining()) {
return std::nullopt;
}
auto [lhs, rhs] = buf_.split_at(n);
buf_ = rhs;
return lhs;
}
// Returns a fixed-size span over the next `N` objects, if there are enough
// objects left. Otherwise, it returns nullopt and does nothing.
template <size_t N>
std::optional<span<T, N>> Read() {
if (N > remaining()) {
return std::nullopt;
}
auto [lhs, rhs] = buf_.template split_at<N>();
buf_ = rhs;
return lhs;
}
// Returns true and writes a span over the next `n` objects into `out`, if
// there are enough objects left. Otherwise, it returns false and does
// nothing.
bool ReadInto(StrictNumeric<size_t> n, span<T>& out) {
if (n > remaining()) {
return false;
}
auto [lhs, rhs] = buf_.split_at(n);
out = lhs;
buf_ = rhs;
return true;
}
// Returns true and copies objects into `out`, if there are enough objects
// left to fill `out`. Otherwise, it returns false and does nothing.
bool ReadCopy(span<std::remove_const_t<T>> out) {
if (out.size() > remaining()) {
return false;
}
auto [lhs, rhs] = buf_.split_at(out.size());
out.copy_from(lhs);
buf_ = rhs;
return true;
}
// Returns true and skips over the next `n` objects, if there are enough
// objects left. Otherwise, it returns false and does nothing.
std::optional<span<T>> Skip(StrictNumeric<size_t> n) {
if (n > remaining()) {
return std::nullopt;
}
auto [lhs, rhs] = buf_.split_at(n);
buf_ = rhs;
return lhs;
}
// For a SpanReader over bytes, we can read integer values directly from those
// bytes as a memcpy. Returns true if there was room remaining and the bytes
// were read.
//
// These treat the bytes from the buffer as being in big endian order.
bool ReadU8BigEndian(uint8_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<1>([&](auto buf) { value = U8FromBigEndian(buf); });
}
bool ReadU16BigEndian(uint16_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<2>([&](auto buf) { value = U16FromBigEndian(buf); });
}
bool ReadU32BigEndian(uint32_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<4>([&](auto buf) { value = U32FromBigEndian(buf); });
}
bool ReadU64BigEndian(uint64_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<8>([&](auto buf) { value = U64FromBigEndian(buf); });
}
// For a SpanReader over bytes, we can read integer values directly from those
// bytes as a memcpy. Returns true if there was room remaining and the bytes
// were read.
//
// These treat the bytes from the buffer as being in little endian order.
bool ReadU8LittleEndian(uint8_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<1>([&](auto buf) { value = U8FromLittleEndian(buf); });
}
bool ReadU16LittleEndian(uint16_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<2>([&](auto buf) { value = U16FromLittleEndian(buf); });
}
bool ReadU32LittleEndian(uint32_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<4>([&](auto buf) { value = U32FromLittleEndian(buf); });
}
bool ReadU64LittleEndian(uint64_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<8>([&](auto buf) { value = U64FromLittleEndian(buf); });
}
// For a SpanReader over bytes, we can read integer values directly from those
// bytes as a memcpy. Returns true if there was room remaining and the bytes
// were read.
//
// These treat the bytes from the buffer as being in native endian order. Note
// that this is almost never what you want to do. Native ordering only makes
// sense for byte buffers that are only meant to stay in memory and never be
// written to the disk or network.
bool ReadU8NativeEndian(uint8_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<1>([&](auto buf) { value = U8FromNativeEndian(buf); });
}
bool ReadU16NativeEndian(uint16_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<2>([&](auto buf) { value = U16FromNativeEndian(buf); });
}
bool ReadU32NativeEndian(uint32_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<4>([&](auto buf) { value = U32FromNativeEndian(buf); });
}
bool ReadU64NativeEndian(uint64_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<8>([&](auto buf) { value = U64FromNativeEndian(buf); });
}
// For a SpanReader over bytes, we can read integer values directly from those
// bytes as a memcpy. Returns true if there was room remaining and the bytes
// were read.
//
// These treat the bytes from the buffer as being in big endian order.
bool ReadI8BigEndian(int8_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<1>([&](auto buf) { value = I8FromBigEndian(buf); });
}
bool ReadI16BigEndian(int16_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<2>([&](auto buf) { value = I16FromBigEndian(buf); });
}
bool ReadI32BigEndian(int32_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<4>([&](auto buf) { value = I32FromBigEndian(buf); });
}
bool ReadI64BigEndian(int64_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<8>([&](auto buf) { value = I64FromBigEndian(buf); });
}
// For a SpanReader over bytes, we can read integer values directly from those
// bytes as a memcpy. Returns true if there was room remaining and the bytes
// were read.
//
// These treat the bytes from the buffer as being in little endian order.
bool ReadI8LittleEndian(int8_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<1>([&](auto buf) { value = I8FromLittleEndian(buf); });
}
bool ReadI16LittleEndian(int16_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<2>([&](auto buf) { value = I16FromLittleEndian(buf); });
}
bool ReadI32LittleEndian(int32_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<4>([&](auto buf) { value = I32FromLittleEndian(buf); });
}
bool ReadI64LittleEndian(int64_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<8>([&](auto buf) { value = I64FromLittleEndian(buf); });
}
// For a SpanReader over bytes, we can read integer values directly from those
// bytes as a memcpy. Returns true if there was room remaining and the bytes
// were read.
//
// These treat the bytes from the buffer as being in native endian order. Note
// that this is almost never what you want to do. Native ordering only makes
// sense for byte buffers that are only meant to stay in memory and never be
// written to the disk or network.
bool ReadI8NativeEndian(int8_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<1>([&](auto buf) { value = I8FromNativeEndian(buf); });
}
bool ReadI16NativeEndian(int16_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<2>([&](auto buf) { value = I16FromNativeEndian(buf); });
}
bool ReadI32NativeEndian(int32_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<4>([&](auto buf) { value = I32FromNativeEndian(buf); });
}
bool ReadI64NativeEndian(int64_t& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<8>([&](auto buf) { value = I64FromNativeEndian(buf); });
}
// For a SpanReader over bytes, reads one byte and returns it as a `char`,
// which may be signed or unsigned depending on the platform. Returns true if
// there was room remaining and the byte was read.
bool ReadChar(char& value)
requires(std::same_as<std::remove_const_t<T>, uint8_t>)
{
return ReadAnd<1>([&](auto buf) { value = static_cast<char>(buf[0u]); });
}
// Returns the number of objects remaining to be read from the original span.
size_t remaining() const { return buf_.size(); }
// Returns the objects that have not yet been read, as a span.
span<T> remaining_span() const { return buf_; }
// Returns the number of objects read (or skipped) in the original span.
size_t num_read() const { return original_size_ - buf_.size(); }
private:
template <size_t N, class F>
requires(std::invocable<F, span<T, N>>)
bool ReadAnd(F f) {
auto buf = Read<N>();
if (buf.has_value()) {
f(*buf);
}
return buf.has_value();
}
span<T> buf_;
size_t original_size_;
};
template <typename ElementType, size_t Extent, typename InternalPtrType>
SpanReader(span<ElementType, Extent, InternalPtrType>)
-> SpanReader<ElementType>;
} // namespace base
#endif // BASE_CONTAINERS_SPAN_READER_H_