extract.be.hpp
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 | #pragma once
#include <felspar/parse/concepts.hpp>
#include <felspar/parse/endian.hpp>
#include <felspar/parse/exceptions.hpp>
#include <felspar/parse/extract.detail.hpp>
#include <cstdint>
namespace felspar::parse::binary::be {
template<concepts::numeric T>
T unchecked_extract(std::span<std::byte const, sizeof(T)> const s) noexcept {
if constexpr (sizeof(T) == 1u) {
return static_cast<T>(s[0]);
} else if constexpr (endian::native == endian::big) {
return detail::native_extract<T>(s);
} else {
return detail::non_native_extract<T>(s);
}
}
template<concepts::numeric T>
T unchecked_extract(
std::span<std::uint8_t const, sizeof(T)> const s) noexcept {
return unchecked_extract<T>(std::as_bytes(s));
}
template<concepts::numeric T>
T unchecked_extract(std::span<char const, sizeof(T)> const s) noexcept {
return unchecked_extract<T>(std::as_bytes(s));
}
|
Extract from a variable length data span
37
38
39
40
41
42
43
44
45
46
47
48
49
50 | template<typename T>
T extract(
std::span<std::byte const> &s,
felspar::source_location const &loc =
felspar::source_location::current()) {
buffer_too_small::check(sizeof(T), s.size(), loc);
auto const v = unchecked_extract<T>(
std::span<std::byte const, sizeof(T)>{s.data(), sizeof(T)});
s = s.subspan(sizeof(T));
return v;
}
}
|