randolf.ca
1.00
Randolf Richardson's C++ classes
|
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...
#include <rline>
Public Member Functions | |
rline () noexcept | |
Instantiate an empty rline, with no EoL sequences present. | |
rline (const char *line, const size_t len=-1) noexcept | |
Instantiate a new rline based on the supplied line of text, ending early upon encountering the first EoL sequence (use either the size(bool) or the size_with_eol() method to obtain the total quantity of characters that were absorbed, including the EoL sequence {if present}). | |
rline (const std::string &line, const size_t len=-1) noexcept | |
Instantiate a new rline based on the supplied line of text, ending early upon encountering the first EoL sequence (use either the size(true) or the size_full() method to obtain the total quantity of characters that were absorbed, including the EoL sequence {if present}). | |
rline (const std::string &line, const std::string &eol_sequence) | |
Instantiate a new rline based on the supplied line of text, ending early upon encountering the first EoL sequence (the size(true) and size_full() methods obtain the total quantity of characters that were absorbed, including the EoL sequence {if present}). | |
rline & | append (const char *line, const size_t len=-1) |
Add the specified string (without checking whether it contains any EoL character sequences) onto the line of text (before the EoL sequence). | |
rline & | append (const std::string &line, const size_t len=-1) |
Add the specified string (without checking whether it contains any EoL character sequences) onto the line of text (before the EoL sequence). | |
rline & | assign (const char *line, const size_t len=-1) noexcept |
Assign a new rline based on the supplied line of text, ending early upon encountering the first EoL sequence (use either the size(bool) or the size_with_eol() method to obtain the total quantity of characters that were absorbed, including the EoL sequence {if present}). | |
rline & | assign (const std::string &line, const size_t len=-1) noexcept |
Assign a new rline based on the supplied line of text, ending early upon encountering the first EoL sequence (use either the size(bool) or the size_with_eol() method to obtain the total quantity of characters that were absorbed, including the EoL sequence {if present}). | |
char | at (const int index) |
Array-style access to the underlying std::string (without the EoL sequence). | |
const char * | c_str () noexcept |
Returns a pointer to the underlying string array, without the EoL sequence. | |
rline & | clear () noexcept |
Reset all data and internal flags to the original state of the constructor that has no parameters. | |
char * | data () noexcept |
Returns a pointer to the underlying string array, without the EoL sequence. | |
std::string * | data_string () noexcept |
Returns a pointer to the underlying std::string object, without the EoL sequence. | |
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 (const bool detected=true) noexcept |
Obtain a copy of the EoL sequence that was detected. | |
rline & | eol (const char c1, const char c2=0) noexcept |
Define the EoL sequence to use when detecting an EoL sequence (also clears the detected EoL sequence, if present). | |
rline & | eol (const std::string &eol_sequence) |
Define the EoL sequence to use when detecting an EoL sequence (also clears the detected EoL sequence, if present). | |
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. | |
bool | has_LFCR (const bool detected=true) noexcept |
Indicate whether a broken EoL sequence detected of <LFCR> is being used as the EoL sequence. | |
rline & | insert (const int position, const std::string &str, const size_t len=-1) |
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 detected 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. | |
bool | operator!= (const std::string &s) |
Compare with the underlying std::string (without the EoL sequence). | |
bool | operator== (const std::string &s) |
Compare with the underlying std::string (without the EoL sequence). | |
char | operator[] (int index) |
Array-style access to the underlying 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 (const bool detected=true) noexcept |
Provides the length of the detected EoL sequence. | |
size_t | size_with_eol () noexcept |
Provides the length of the line of text, including the EoL sequence (if one is present). | |
Friends | |
std::ostream & | operator<< (std::ostream &o, rline const &c) |
Support convenient streaming usage with std::cout, std::cerr, and friends. | |
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>.
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.
at()
method, and [], ==, and != operators
|
inlinenoexcept |
Instantiate an empty rline, with no EoL sequences present.
|
inlinenoexcept |
Instantiate a new rline based on the supplied line of text, ending early upon encountering the first EoL sequence (use either the size(bool) or the size_with_eol() method to obtain the total quantity of characters that were absorbed, including the EoL sequence {if present}).
If no EoL sequence was encountered, the has_eol() method will return false
even though the contents of the supplied line were copied (and are still accessible) – this is useful in loops reading lines of text that are too long to store in memory with a single allocation (and either interpreting the absorbed data somehow, or using a simple loop to skip through to the end of the line before issuing an error that indicates the total line length).
line
is a nullptr
then it will be treated as an empty string without an EoL sequence, instead of throwing a Null Pointer Exception. This provides an opportunity to write simpler code (see the is_null method for the way to check for this condition, which also happens to be the result of using the default constructor or calling clear). line | Line of text to embrace |
len | Length of line (-1 == ASCIIZ string) |
|
inlinenoexcept |
Instantiate a new rline based on the supplied line of text, ending early upon encountering the first EoL sequence (use either the size(true)
or the size_full()
method to obtain the total quantity of characters that were absorbed, including the EoL sequence {if present}).
If no EoL sequence was encountered, the has_eol() method will return false
even though the contents of the supplied line were copied (and are still accessible) – this is useful in loops reading lines of text that are too long to store in memory with a single allocation (and either interpreting the absorbed data somehow, or using a simple loop to skip through to the end of the line before issuing an error that indicates the total line length).
line | Line of text to embrace |
len | Length of line (-1 == defer to length of line provided) |
|
inline |
Instantiate a new rline based on the supplied line of text, ending early upon encountering the first EoL sequence (the size(true)
and size_full()
methods obtain the total quantity of characters that were absorbed, including the EoL sequence {if present}).
If no EoL sequence was encountered, the has_eol() method will return false
even though the contents of the supplied line were copied (and are still accessible) – this is useful in loops reading lines of text that are too long to store in memory with a single allocation (and either interpreting the absorbed data somehow, or using a simple loop to skip through to the end of the line before issuing an error that indicates the total line length).
std::length_error | if the EoL sequence is longer than 2 characters |
line | Line of text to embrace, without the EoL sequence characters |
eol_sequence | The EoL sequence to impose as if it had been detected (may be only 1 or 2 characters long) |
|
inline |
|
inline |
Add the specified string (without checking whether it contains any EoL character sequences) onto the line of text (before the EoL sequence).
line | The string to insert |
len | Length of line (-1 == defer to length of line provided) |
|
inlinenoexcept |
Assign a new rline based on the supplied line of text, ending early upon encountering the first EoL sequence (use either the size(bool) or the size_with_eol() method to obtain the total quantity of characters that were absorbed, including the EoL sequence {if present}).
If no EoL sequence was encountered, the has_eol() method will return false
even though the contents of the supplied line were copied (and are still accessible) – this is useful in loops reading lines of text that are too long to store in memory with a single allocation (and either interpreting the absorbed data somehow, or using a simple loop to skip through to the end of the line before issuing an error that indicates the total line length).
line
is a nullptr
then it will be treated as an empty string without an EoL sequence, instead of throwing a Null Pointer Exception. This provides an opportunity to write simpler code (see the is_null method for the way to check for this condition, which also happens to be the result of using the default constructor or calling clear). line | Line of text to embrace |
len | Length of line (-1 == ASCIIZ string) |
|
inlinenoexcept |
Assign a new rline based on the supplied line of text, ending early upon encountering the first EoL sequence (use either the size(bool) or the size_with_eol() method to obtain the total quantity of characters that were absorbed, including the EoL sequence {if present}).
If no EoL sequence was encountered, the has_eol() method will return false
even though the contents of the supplied line were copied (and are still accessible) – this is useful in loops reading lines of text that are too long to store in memory with a single allocation (and either interpreting the absorbed data somehow, or using a simple loop to skip through to the end of the line before issuing an error that indicates the total line length).
line | Line of text to embrace |
len | Length of line (-1 == defer to length of line provided) |
|
inline |
Array-style access to the underlying std::string (without the EoL sequence).
The first element is at index 0.
std::out_of_range | if the index is out-of-range |
index | Index of character to access (0 = first element; negative index values are calculated in reverse, starting with -1 as the final position) |
|
inlinenoexcept |
Reset all data and internal flags to the original state of the constructor that has no parameters.
|
inlinenoexcept |
Returns a pointer to the underlying string array, without the EoL sequence.
|
inlinenoexcept |
Returns a pointer to the underlying string array, without the EoL sequence.
|
inlinenoexcept |
Returns a pointer to the underlying std::string object, without the EoL sequence.
|
inlinenoexcept |
|
inlinenoexcept |
Obtain a copy of the EoL sequence that was detected.
detected | TRUE = return the detected EoL sequence (default) FALSE = return the EoL sequence to use when detecting an EoL sequence |
|
inlinenoexcept |
Define the EoL sequence to use when detecting an EoL sequence (also clears the detected EoL sequence, if present).
eol
method to use for defining the specific EoL sequence to use. c1 | The first character of the EoL sequence (typically 10 or 13)10 = LF (Line Feed) 13 = CR (Carriage Return) 0 = enable EoL sequence auto-detection (this is the default in all constructors, and is also set by the clear method) |
c2 | The second character of the EoL sequence (typically 10 or 13)10 = LF (Line Feed) 13 = CR (Carriage Return) 0 = indicate that this is not a two-character EoL sequence |
|
inline |
Define the EoL sequence to use when detecting an EoL sequence (also clears the detected EoL sequence, if present).
std::length_error | if the EoL sequence is longer than 2 characters |
eol_sequence | The EoL sequence (may be only 1 or 2 characters long; or 0 characters to enable EoL auto-detection, which is also the default in all constructors) |
|
inlinenoexcept |
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).
include_eol | Whether to include the EoL detected (default = FALSE) |
|
inlinenoexcept |
Indicate whether an EoL sequence was detected.
|
inlinenoexcept |
Indicate whether a broken EoL sequence detected of <LFCR>
is being used as the EoL sequence.
detected | TRUE = report on whether a broken EoL sequence was detected (default) FALSE = report on whether a broken EoL sequence was specifically defined as the EoL sequence to detect |
|
inline |
Insert the specified string (without checking whether it contains any EoL character sequences) into the line of text at the specified position.
std::out_of_range | When the position (pos ) provided is out of range (e.g., position is larger than the entire size of the string) |
position | Where to insert the supplied string (0 = insert before first character; and use a negative number to set the position relative to the end of the line of text) |
str | The string to insert |
len | Length of string (-1 == defer to length of string provided) |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
Provides the length of the line of text without the EoL sequence.
This method is identital to the length method.
include_eol | Whether to include the EoL sequence (default = FALSE) |
|
inlinenoexcept |
Provides the length of the detected EoL sequence.
detected | TRUE = report on the size of the detected EoL sequence (default) FALSE = report on the size of the EoL sequence to use when detecting an EoL sequence |
|
inlinenoexcept |
|
inlinenoexcept |
Convert this rline
to an std::string
without the EoL sequence.
|
inline |
Compare with the underlying std::string (without the EoL sequence).
s | String to compare with |
|
inline |
Compare with the underlying std::string (without the EoL sequence).
s | String to compare with |
|
inline |
Array-style access to the underlying std::string (without the EoL sequence).
The first element is at index 0.
std::out_of_range | if the index is out-of-range |
index | Index of character to access (0 = first element; negative index values are calculated in reverse, starting with -1 as the final position) |
|
friend |
Support convenient streaming usage with std::cout, std::cerr, and friends.
o | Output stream (provided automatically by std::cout and std::cerr) |
c | Object class (matched by compiler) |