One line of text is stored in this class, with additional metadata to track whether the text was terminated by an EoL (End of Line) sequence, and, if so, what the EoL sequence was.
More...
|
| rline () noexcept |
| Instantiate an empty rline, with no EoL sequences present.
|
|
| rline (char *line, size_t len=-1) noexcept |
| Instantiate a new rline based on the supplied line of text. Multiple lines embedded in the supplied string will be treated as a single line, and ignored therefore.
|
|
| rline (std::string line) noexcept |
| Instantiate a new rline based on the supplied line of text. Multiple lines embedded in the supplied string will be treated as a single line, and ignored therefore.
|
|
rline * | append (const std::string str) |
| Add the specified string (without checking whether it contains any EoL character sequences) onto the line of text (before the EoL sequence).
|
|
rline * | assign (char *line, size_t len=-1) noexcept |
| Assign a new line of text, the same way the constructor assigns it.
|
|
rline * | assign (std::string line) noexcept |
| Assign a new line of text, the same way the constructor assigns it.
|
|
rline * | clear () noexcept |
| Reset all data and internal flags to the original state of the constructor that has no parameters.
|
|
bool | empty () noexcept |
| Indicate whether the line of text is empty. Whether an EoL sequence is present is incidental since this only measures the size of the line of text without regard for the presence of an EoL sequence.
|
|
const std::string | eol () noexcept |
| Obtain a copy of the EoL sequence.
|
|
const std::string | get (const bool include_eol=false) noexcept |
| Obtain a copy of the entire line of text, without the EoL sequence (albeit by default, this can also be included by setting the include_eol flag).
|
|
bool | has_eol () noexcept |
| Indicate whether an EoL sequence was detected.
|
|
rline * | insert (int position, const std::string str) |
| Insert the specified string (without checking whether it contains any EoL character sequences) into the line of text at the specified position.
|
|
bool | is_null () noexcept |
| Indicate whether this line is non-existent, which means there is no text and no EoL sequence. This is less than an an empty() string when it confirms that there literally are no characters at all.
|
|
size_t | length (const bool include_eol=false) noexcept |
| Provides the length of the line of text without the EoL sequence.
|
|
| operator std::string () const noexcept |
| Convert this rline to an std::string without the EoL sequence.
|
|
size_t | size (const bool include_eol=false) noexcept |
| Provides the length of the line of text without the EoL sequence.
|
|
size_t | size_eol () noexcept |
| Provides the length of the EoL sequence.
|
|
One line of text is stored in this class, with additional metadata to track whether the text was terminated by an EoL (End of Line) sequence, and, if so, what the EoL sequence was.
This is particularly useful for reading lines of text from a file or a socket without having to go to additional efforts to track whether the final line is terminated with an EoL sequence (since this class takes care of this detail).
By default, the EoL sequence may be <CR>, <LF>, <CRLF>, or <LFCR>.
- Use case
- One of the challenges with the
std::string
class is that it doesn't provide a way to differentiate between a empty line and NULL
, unless one uses pointers to std::string
objects and sets thoes pointers to nullptr
which entails having to test for that, resulting in more code in various places to test for multiple conditions, thus providing opportunities for more unintended situations (a.k.a., bugs) ... and then there's the matter of how to handle the variety of EoL sequences that persist due to the diversity of Operating System standards, which complicates matters even more.
This class handles all of these details gracefully, and provides a thorough variety of clearly-documented methods that ensure consistency whilst handling the complexity of EoL sequences that may be 1 or 2 characters long and also differentiating between blank and NULL lines, all without causing significant increases in complexity in code.
- History
- 2024-Nov-16 v1.00 Initial version
- Version
- 1.00
- Author
- Randolf Richardson