bits.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
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
#ifndef CRUCIBLE_PARSING_BITS_HPP #define CRUCIBLE_PARSING_BITS_HPP #include "crucible/core/constant_error.inl" #include "crucible/core/error_chain.inl" #include "crucible/core/none.hpp" #include "crucible/parsing/result.inl" #include <concepts> #include <cstddef> #include <cstdint> #include <span> namespace crucible::parsing::bits { template<typename T> concept FixedWidthUnsignedIntegerType = std::same_as<T, std::uint8_t> || std::same_as<T, std::uint16_t> || std::same_as<T, std::uint32_t> || std::same_as<T, std::uint64_t>; struct ByteSequenceMismatch final : core::constant_error::ConstantError<ByteSequenceMismatch> { static constexpr char const message[] = "Byte sequence mismatch"; }; struct UnexpectedEndOfInput final : core::constant_error::ConstantError<UnexpectedEndOfInput> { static constexpr char const message[] = "Unexpected end of input"; }; struct BitsError final : core::error_chain::ErrorChain<BitsError, ByteSequenceMismatch, UnexpectedEndOfInput> { using ErrorChain::ErrorChain; static constexpr char const message[] = "Parse error in bits primitive"; }; class BitsState final { public: explicit BitsState(std::span<std::byte const> const &input); BitsState(std::span<std::byte const> const &input, std::size_t const byte_offset, std::size_t const bit_offset); [[nodiscard]] auto is_finished() const -> bool; [[nodiscard]] auto get_remaining_bytes_in_input() const -> std::span<std::byte const>; [[nodiscard]] auto get_remaining_bits_in_byte() const -> std::byte; [[nodiscard]] auto advance_by_bytes(std::size_t const byte_count) const -> BitsState; [[nodiscard]] auto advance_by_bits(std::size_t const bit_count) const -> BitsState; [[nodiscard]] auto format_location() const -> std::string; private: std::span<std::byte const> my_input; std::size_t my_byte_offset = 0; std::size_t my_bit_offset = 0; }; class ByteSequence final { public: using StateType = BitsState; using ErrorType = BitsError; using ValueType = core::none::None; using RejectType = result::Reject<StateType, ErrorType>; using AcceptType = result::Accept<StateType, ValueType>; using ResultType = core::result::Result<RejectType, AcceptType>; explicit ByteSequence(std::span<std::byte const> byte_sequence); [[nodiscard]] auto operator()(StateType const &state) const -> ResultType; private: std::span<std::byte const> my_byte_sequence; }; template<FixedWidthUnsignedIntegerType T> struct LittleEndianUnsignedInteger final { using StateType = BitsState; using ErrorType = BitsError; using ValueType = T; using RejectType = result::Reject<StateType, ErrorType>; using AcceptType = result::Accept<StateType, ValueType>; using ResultType = core::result::Result<RejectType, AcceptType>; [[nodiscard]] auto operator()(StateType const &state) const -> ResultType; }; template<FixedWidthUnsignedIntegerType T> struct BigEndianUnsignedInteger final { using StateType = BitsState; using ErrorType = BitsError; using ValueType = T; using RejectType = result::Reject<StateType, ErrorType>; using AcceptType = result::Accept<StateType, ValueType>; using ResultType = core::result::Result<RejectType, AcceptType>; [[nodiscard]] auto operator()(StateType const &state) const -> ResultType; }; } #endif // CRUCIBLE_PARSING_BITS_HPP