NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
byte_cursor.hpp
浏览该文件的文档.
1#ifndef NEFORCE_NETWORK_HTTP_BYTE_CURSOR_HPP__
2#define NEFORCE_NETWORK_HTTP_BYTE_CURSOR_HPP__
3
10
14NEFORCE_BEGIN_NAMESPACE__
15NEFORCE_BEGIN_HTTP__
16
22
30private:
32 const byte_t* data_ = nullptr;
34 size_t size_ = 0;
36 size_t pos_ = 0;
38 uint64_t bit_buf_ = 0;
40 size_t bits_in_buf_ = 0;
41
42public:
44 byte_cursor() noexcept = default;
45
47 byte_cursor(const byte_t* data, size_t size) noexcept :
48 data_(data),
49 size_(size) {}
50
52 explicit byte_cursor(cbyte_view view) noexcept :
53 data_(view.data()),
54 size_(view.size()) {}
55
57 NEFORCE_NODISCARD size_t remaining() const noexcept { return size_ - pos_; }
59 NEFORCE_NODISCARD size_t consumed_bytes() const noexcept { return pos_; }
61 NEFORCE_NODISCARD bool exhausted() const noexcept { return pos_ >= size_; }
63 NEFORCE_NODISCARD const byte_t* data() const noexcept { return data_; }
65 NEFORCE_NODISCARD size_t size() const noexcept { return size_; }
66
71 NEFORCE_NODISCARD optional<byte_t> try_read_byte() noexcept {
72 if (pos_ >= size_) {
73 return none;
74 }
75 return data_[pos_++];
76 }
77
82 NEFORCE_NODISCARD optional<byte_t> peek_byte() const noexcept {
83 if (pos_ >= size_) {
84 return none;
85 }
86 return data_[pos_];
87 }
88
93 NEFORCE_NODISCARD optional<uint16_t> try_read_be16() noexcept {
94 if (pos_ + 2 > size_) {
95 return none;
96 }
97 const uint16_t val = endian::read_be16(data_ + pos_);
98 pos_ += 2;
99 return val;
100 }
101
106 NEFORCE_NODISCARD optional<uint32_t> try_read_be24() noexcept {
107 if (pos_ + 3 > size_) {
108 return none;
109 }
110 const uint32_t val = endian::read_be24(data_ + pos_);
111 pos_ += 3;
112 return val;
113 }
114
119 NEFORCE_NODISCARD optional<uint32_t> try_read_be32() noexcept {
120 if (pos_ + 4 > size_) {
121 return none;
122 }
123 const uint32_t val = endian::read_be32(data_ + pos_);
124 pos_ += 4;
125 return val;
126 }
127
132 NEFORCE_NODISCARD optional<uint64_t> try_read_be64() noexcept {
133 if (pos_ + 8 > size_) {
134 return none;
135 }
136 const uint64_t val = endian::read_be64(data_ + pos_);
137 pos_ += 8;
138 return val;
139 }
140
145 NEFORCE_NODISCARD optional<uint16_t> try_read_le16() noexcept {
146 if (pos_ + 2 > size_) {
147 return none;
148 }
149 const uint16_t val = endian::read_le16(data_ + pos_);
150 pos_ += 2;
151 return val;
152 }
153
158 NEFORCE_NODISCARD optional<uint32_t> try_read_le32() noexcept {
159 if (pos_ + 4 > size_) {
160 return none;
161 }
162 const uint32_t val = endian::read_le32(data_ + pos_);
163 pos_ += 4;
164 return val;
165 }
166
171 NEFORCE_NODISCARD optional<uint64_t> try_read_le64() noexcept {
172 if (pos_ + 8 > size_) {
173 return none;
174 }
175 const uint64_t val = endian::read_le64(data_ + pos_);
176 pos_ += 8;
177 return val;
178 }
179
185 NEFORCE_NODISCARD optional<cbyte_view> try_read_bytes(size_t count) noexcept {
186 if (pos_ + count > size_) {
187 return none;
188 }
189 cbyte_view view(data_ + pos_, count);
190 pos_ += count;
191 return view;
192 }
193
199 bool skip(size_t count) noexcept {
200 if (pos_ + count > size_) {
201 return false;
202 }
203 pos_ += count;
204 return true;
205 }
206
212 void reset(const byte_t* data, size_t size) noexcept {
213 data_ = data;
214 size_ = size;
215 pos_ = 0;
216 bit_buf_ = 0;
217 bits_in_buf_ = 0;
218 }
219
225 NEFORCE_NODISCARD optional<uint64_t> try_read_bits(uint8_t n) noexcept {
226 if (n > 64) {
227 return none;
228 }
229 while (bits_in_buf_ < n) {
230 auto b = try_read_byte();
231 if (!b) {
232 return none;
233 }
234 bit_buf_ = (bit_buf_ << 8) | *b;
235 bits_in_buf_ += 8;
236 }
237 bits_in_buf_ -= n;
238 const uint64_t mask = (n == 64) ? numeric_traits<uint64_t>::max() : ((1ULL << n) - 1);
239 return (bit_buf_ >> bits_in_buf_) & mask;
240 }
241
247 NEFORCE_NODISCARD optional<uint64_t> try_peek_bits(uint8_t n) const noexcept {
248 if (n > 64 || bits_in_buf_ < n) {
249 return none;
250 }
251 const uint64_t mask = (n == 64) ? numeric_traits<uint64_t>::max() : ((1ULL << n) - 1);
252 return (bit_buf_ >> (bits_in_buf_ - n)) & mask;
253 }
254
259 void skip_bits(uint8_t n) noexcept {
260 if (n <= bits_in_buf_) {
261 bits_in_buf_ -= n;
262 } else {
263 bits_in_buf_ = 0;
264 }
265 }
266
268 NEFORCE_NODISCARD size_t bits_remaining() const noexcept { return bits_in_buf_; }
269
274 bool refill_bits() noexcept {
275 if (exhausted()) {
276 return false;
277 }
278 bit_buf_ = (bit_buf_ << 8) | data_[pos_++];
279 bits_in_buf_ += 8;
280 return true;
281 }
282};
283 // ByteCursor
285
286NEFORCE_END_HTTP__
287NEFORCE_END_NAMESPACE__
288#endif // NEFORCE_NETWORK_HTTP_BYTE_CURSOR_HPP__
optional< uint32_t > try_read_be32() noexcept
尝试读取大端序 32 位无符号整数
optional< byte_t > peek_byte() const noexcept
查看当前字节而不消费
optional< uint32_t > try_read_le32() noexcept
尝试读取小端序 32 位无符号整数
void skip_bits(uint8_t n) noexcept
跳过指定位数
optional< uint64_t > try_peek_bits(uint8_t n) const noexcept
预读指定位数而不消费
optional< uint64_t > try_read_le64() noexcept
尝试读取小端序 64 位无符号整数
optional< uint32_t > try_read_be24() noexcept
尝试读取大端序 24 位无符号整数
optional< cbyte_view > try_read_bytes(size_t count) noexcept
尝试读取指定长度的字节块
byte_cursor() noexcept=default
构造空游标
optional< uint16_t > try_read_be16() noexcept
尝试读取大端序 16 位无符号整数
bool exhausted() const noexcept
是否已读完所有数据
void reset(const byte_t *data, size_t size) noexcept
重置游标到新的缓冲区
size_t bits_remaining() const noexcept
位缓冲区中剩余的可读位数
optional< uint16_t > try_read_le16() noexcept
尝试读取小端序 16 位无符号整数
size_t size() const noexcept
获取底层缓冲区总大小
optional< uint64_t > try_read_be64() noexcept
尝试读取大端序 64 位无符号整数
const byte_t * data() const noexcept
获取底层数据指针
byte_cursor(cbyte_view view) noexcept
从 cbyte_view 构造
size_t remaining() const noexcept
剩余可读字节数
size_t consumed_bytes() const noexcept
已消费的字节数
bool refill_bits() noexcept
从字节缓冲区中补充位缓冲区
optional< byte_t > try_read_byte() noexcept
尝试读取一个字节
bool skip(size_t count) noexcept
跳过指定数量的字节
optional< uint64_t > try_read_bits(uint8_t n) noexcept
尝试读取指定位数(用于 HPACK Huffman 解码)
static constexpr T max() noexcept
获取类型的最大值
端序转换工具
unsigned char byte_t
字节类型,定义为无符号字符
unsigned int uint32_t
32位无符号整数类型
unsigned long uint64_t
64位无符号整数类型
unsigned char uint8_t
8位无符号整数类型
unsigned short uint16_t
16位无符号整数类型
constexpr iter_difference_t< Iterator > count(Iterator first, Iterator last, const T &value)
统计范围内等于指定值的元素数量
memory_view< const byte_t > cbyte_view
常量字节视图类型别名
constexpr none_t none
默认空表示
内存视图容器
可选值类型
static constexpr uint64_t read_be64(const byte_t *data) noexcept
读取64位大端整数
static constexpr uint16_t read_le16(const byte_t *data) noexcept
读取16位小端整数
static constexpr uint32_t read_le32(const byte_t *data) noexcept
读取32位小端整数
static constexpr uint16_t read_be16(const byte_t *data) noexcept
读取16位大端整数
static constexpr uint32_t read_be32(const byte_t *data) noexcept
读取32位大端整数
static constexpr uint64_t read_le64(const byte_t *data) noexcept
读取64位小端整数
static constexpr uint32_t read_be24(const byte_t *data) noexcept
读取24位大端整数