bits.cpp
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
#include "crucible/parsing/bits.inl" #include <algorithm> #include <climits> #include <sstream> namespace crucible::parsing::bits { BitsState::BitsState(std::span<std::byte const> const &input) : my_input { input } { } BitsState::BitsState(std::span<std::byte const> const &input, std::size_t const byte_offset, std::size_t const bit_offset) : my_input { input }, my_byte_offset { byte_offset }, my_bit_offset { bit_offset } { } auto BitsState::is_finished() const -> bool { return my_byte_offset >= my_input.size(); } auto BitsState::get_remaining_bytes_in_input() const -> std::span<std::byte const> { return my_input.subspan(my_byte_offset); } auto BitsState::get_remaining_bits_in_byte() const -> std::byte { constexpr std::byte ALL_ONES { 0xff }; std::byte const next_byte { my_input[my_byte_offset] }; std::byte const mask { ALL_ONES >> my_bit_offset }; return next_byte & mask; } auto BitsState::advance_by_bytes(std::size_t const byte_count) const -> BitsState { return BitsState { my_input, my_byte_offset + byte_count, my_bit_offset }; } auto BitsState::advance_by_bits(std::size_t const bit_count) const -> BitsState { std::size_t const total_bit_offset { my_byte_offset * CHAR_BIT + my_bit_offset + bit_count }; std::size_t const new_byte_offset { total_bit_offset / CHAR_BIT }; std::size_t const new_bit_offset { total_bit_offset % CHAR_BIT }; return BitsState { my_input, new_byte_offset, new_bit_offset }; } auto BitsState::format_location() const -> std::string { std::ostringstream buffer {}; buffer << "{ \"source_type\" : \"bits\", \"byte\": " << my_byte_offset << ", \"bit\" : " << my_bit_offset << " }"; return buffer.str(); } ExactByteSequence::ExactByteSequence(std::span<std::byte const> byte_sequence) : my_byte_sequence { byte_sequence.begin(), byte_sequence.end() } { } auto ExactByteSequence::operator()(StateType const &state) const -> ResultType { auto const &remaining_bytes { state.get_remaining_bytes_in_input() }; if (remaining_bytes.size() < my_byte_sequence.size()) { return errors::make_reject(state, errors::UnexpectedEndOfInput {}); } auto const sequence_begin { my_byte_sequence.begin() }; auto const sequence_end { my_byte_sequence.end() }; auto const input_begin { remaining_bytes.begin() }; bool const is_equal { std::equal(sequence_begin, sequence_end, input_begin) }; if (is_equal) { auto const next_state { state.advance_by_bytes(my_byte_sequence.size()) }; return errors::make_accept(next_state, core::none::None {}); } else { return errors::make_reject(state, errors::UnexpectedInput {}); } } SizedByteSequence::SizedByteSequence(std::size_t const size) : my_size { size } { } auto SizedByteSequence::operator()(StateType const &state) const -> ResultType { auto const &remaining_bytes { state.get_remaining_bytes_in_input() }; if (remaining_bytes.size() < my_size) { return errors::make_reject(state, errors::UnexpectedEndOfInput {}); } auto const sequence = remaining_bytes.first(my_size); auto const next_state { state.advance_by_bytes(my_size) }; return errors::make_accept(next_state, sequence); } }