Simple buffer around a string that allows for easy reading and writing of values.
More...
#include <string_buffer.hpp>
|
| | StringBuffer ()=default |
| | StringBuffer (std::string data) |
| | Constructs a string_buffer with the given data.
|
| BufferView< std::string::value_type > | prepare (const size_t size) |
| | Prepares space in the buffer for writing.
|
| void | commit (const size_t size) |
| | Commits characters to the buffer, by moving the write position ahead.
|
| size_t | remaining () const |
| bool | exhausted () const |
| std::string_view | data () const |
| void | consume (const size_t size) |
| | Consumes characters from the buffer, by moving the read position ahead.
|
| std::string_view | read (size_t max_size=std::numeric_limits< size_t >::max()) |
| | Reads data from the buffer, returning a view to the readable portion.
|
| std::optional< std::string_view > | read_until_newline () |
| | Reads until a newline is found.
|
| bool | starts_with (const std::string_view prefix) const |
| | Tests if the next available data starts with the given prefix, without consuming any data.
|
| void | write (const std::string_view &data) |
| | Writes and commits data to the buffer.
|
| void | clear () |
| | Clears the data and sets the read and write positions to zero.
|
Simple buffer around a string that allows for easy reading and writing of values.
Definition at line 38 of file string_buffer.hpp.
◆ StringBuffer() [1/2]
| rav::StringBuffer::StringBuffer |
( |
| ) |
|
|
default |
◆ StringBuffer() [2/2]
| rav::StringBuffer::StringBuffer |
( |
std::string | data | ) |
|
|
inlineexplicit |
Constructs a string_buffer with the given data.
- Parameters
-
Definition at line 46 of file string_buffer.hpp.
◆ clear()
| void rav::StringBuffer::clear |
( |
| ) |
|
|
inline |
Clears the data and sets the read and write positions to zero.
Definition at line 165 of file string_buffer.hpp.
◆ commit()
| void rav::StringBuffer::commit |
( |
const size_t | size | ) |
|
|
inline |
Commits characters to the buffer, by moving the write position ahead.
- Parameters
-
| size | The amount of characters to commit. |
Definition at line 64 of file string_buffer.hpp.
◆ consume()
| void rav::StringBuffer::consume |
( |
const size_t | size | ) |
|
|
inline |
Consumes characters from the buffer, by moving the read position ahead.
- Parameters
-
| size | The amount of characters to consume. |
Definition at line 95 of file string_buffer.hpp.
◆ data()
| std::string_view rav::StringBuffer::data |
( |
| ) |
const |
|
inlinenodiscard |
- Returns
- A string_view to the data available to read.
Definition at line 87 of file string_buffer.hpp.
◆ exhausted()
| bool rav::StringBuffer::exhausted |
( |
| ) |
const |
|
inlinenodiscard |
- Returns
- True if there is no data available to read.
Definition at line 80 of file string_buffer.hpp.
◆ prepare()
| BufferView< std::string::value_type > rav::StringBuffer::prepare |
( |
const size_t | size | ) |
|
|
inline |
Prepares space in the buffer for writing.
The returned buffer_view is valid until the next call to a non-const method. The buffer will be resized if necessary to accommodate the requested size. After writing to the prepared space, call commit to finalize the operation.
- Parameters
-
| size | The number of characters to reserve in the buffer. |
- Returns
- A string_view representing the writable portion of the buffer.
Definition at line 55 of file string_buffer.hpp.
◆ read()
| std::string_view rav::StringBuffer::read |
( |
size_t | max_size = std::numeric_limits<size_t>::max() | ) |
|
|
inlinenodiscard |
Reads data from the buffer, returning a view to the readable portion.
The size of the returned view will be either max_size or the amount of data available, whichever is smaller. The data is marked as consumed after this operation. The returned view remains valid until either reset() is called or the buffer is reallocated.
- Parameters
-
| max_size | The maximum number of characters to read. |
- Returns
- A string_view of the available data, up to max_size.
Definition at line 111 of file string_buffer.hpp.
◆ read_until_newline()
| std::optional< std::string_view > rav::StringBuffer::read_until_newline |
( |
| ) |
|
|
inlinenodiscard |
Reads until a newline is found.
Newline can be \r
or
.
- Returns
- The line until newline, or std::nullopt if no newline was found.
Definition at line 123 of file string_buffer.hpp.
◆ remaining()
| size_t rav::StringBuffer::remaining |
( |
| ) |
const |
|
inlinenodiscard |
- Returns
- The number of characters available to read.
Definition at line 72 of file string_buffer.hpp.
◆ starts_with()
| bool rav::StringBuffer::starts_with |
( |
const std::string_view | prefix | ) |
const |
|
inlinenodiscard |
Tests if the next available data starts with the given prefix, without consuming any data.
- Parameters
-
| prefix | The prefix to test. |
- Returns
- True if the next available data starts with the prefix.
Definition at line 148 of file string_buffer.hpp.
◆ write()
| void rav::StringBuffer::write |
( |
const std::string_view & | data | ) |
|
|
inline |
The documentation for this class was generated from the following file: