Echo Writes Code

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
#ifndef CRUCIBLE_PARSING_BITS_HPP
#define CRUCIBLE_PARSING_BITS_HPP

#include "crucible/parsing/errors.inl"

#include "crucible/core/none.hpp"

#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>;

  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 ExactByteSequence final {
  public:
    using StateType = BitsState;

    using ValueType = core::none::None;

    using ResultType = errors::ParsingResult<StateType, ValueType>;

    explicit ExactByteSequence(std::span<std::byte const> byte_sequence);

    [[nodiscard]]
    auto operator()(StateType const &state) const -> ResultType;

  private:
    std::vector<std::byte> my_byte_sequence {};
  };

  class SizedByteSequence final {
  public:
    using StateType = BitsState;

    using ValueType = std::span<std::byte const>;

    using ResultType = errors::ParsingResult<StateType, ValueType>;

    explicit SizedByteSequence(std::size_t const size);

    [[nodiscard]]
    auto operator()(StateType const &state) const -> ResultType;

  private:
    std::size_t my_size { 0 };
  };

  template<FixedWidthUnsignedIntegerType T>
  struct LittleEndianUnsignedInteger final {
    using StateType = BitsState;

    using ValueType = T;

    using ResultType = errors::ParsingResult<StateType, ValueType>;

    [[nodiscard]]
    auto operator()(StateType const &state) const -> ResultType;
  };

  template<FixedWidthUnsignedIntegerType T>
  struct BigEndianUnsignedInteger final {
    using StateType = BitsState;

    using ValueType = T;

    using ResultType = errors::ParsingResult<StateType, ValueType>;

    [[nodiscard]]
    auto operator()(StateType const &state) const -> ResultType;
  };
}

#endif // CRUCIBLE_PARSING_BITS_HPP