Binary data parser.
This class exposes a read-only API that facilitates extracting different data types from a byte sequence. It is backed by std::span, hence it is cheap to create it from an existing data container, or copy it from an existing parser (which could be useful when writing custom parsers).
- Note
- The parser does not own the memory, hence it is not thread-safe.
template<std::floating_point TValue, Order Endian = Order::big>
TValue kouta::io::Parser::extract_floating_point |
( |
std::size_t |
offset | ) |
const |
|
inline |
Extract a floating point value from the view.
The offset
must be within bounds, considering the size of type
- Template Parameters
-
TValue. | Otherwise, an std::out_of_range exception will be thrown. |
It is recommended to check the size() before attempting to extract a value.
- Template Parameters
-
TValue | The numerical type to extract from the data view. |
Endian | Endian order of the value to extract. |
- Parameters
-
offset | The offset from which to start extracting data. |
- Returns
- Floating point value
- Exceptions
-
std::out_of_range | when there are not enough bytes in the data view. |
template<std::integral TValue, std::size_t N = sizeof(TValue), Order Endian = Order::big>
TValue kouta::io::Parser::extract_integral |
( |
std::size_t |
offset | ) |
const |
|
inline |
Extract an integral value from the view.
The offset
must be within bounds, considering the size
- Template Parameters
-
N. | Otherwise, an std::out_of_range exception will be thrown. |
It is recommended to check the size() before attempting to extract a value.
- Template Parameters
-
TValue | The numerical type to extract from the data view. |
N | Number of bytes to extract. |
Endian | Endian order of the value to extract. |
- Parameters
-
offset | The offset from which to start extracting data. |
- Returns
- Integral value
- Exceptions
-
std::out_of_range | when there are not enough bytes in the data view. |
virtual std::string kouta::io::Parser::extract_string |
( |
std::size_t |
offset, |
|
|
std::size_t |
count |
|
) |
| const |
|
inlinevirtual |
Extract a string value from the view.
The offset
must be within bounds, considering the character count
specified.
It is recommended to check the size() before attempting to extract a value.
This method is marked as virtual to allow overriding it in order to implement custmo behaviour such as interrupting the parsing whenever a null-character is found.
- Parameters
-
offset | The starting offset from which to start extracting data. |
count | The number of characters/bytes to extract from the view. |
- Returns
- String extracted from the view.
- Exceptions
-
std::out_of_range | when there are not enough bytes in the data view. |