Gromacs
2018.1
|
#include <gromacs/utility/stringutil.h>
Wraps lines to a predefined length.
This utility class wraps lines at word breaks to produce lines that are not longer than a predefined length. Explicit newlines ('\n') are preserved. Only space is considered a word separator. If a single word exceeds the maximum line length, it is still printed on a single line. Extra whitespace is stripped from the end of produced lines. Other options on the wrapping, such as the line length or indentation, can be changed using a TextLineWrapperSettings object.
Two interfaces to do the wrapping are provided:
Typical usage:
Public Member Functions | |
TextLineWrapper () | |
Constructs a new line wrapper with default settings. More... | |
TextLineWrapper (const TextLineWrapperSettings &settings) | |
Constructs a new line wrapper with given settings. More... | |
TextLineWrapperSettings & | settings () |
Provides access to settings of this wrapper. More... | |
bool | isTrivial () const |
Returns true if the wrapper would not modify the input string. | |
size_t | findNextLine (const char *input, size_t lineStart) const |
Finds the next line to be wrapped. More... | |
size_t | findNextLine (const std::string &input, size_t lineStart) const |
Finds the next line to be wrapped. More... | |
std::string | formatLine (const std::string &input, size_t lineStart, size_t lineEnd) const |
Formats a single line for output according to wrapping settings. More... | |
std::string | wrapToString (const std::string &input) const |
Formats a string, producing a single string with all the lines. More... | |
std::vector< std::string > | wrapToVector (const std::string &input) const |
Formats a string, producing a vector with all the lines. More... | |
|
inline |
Constructs a new line wrapper with default settings.
Does not throw.
|
inlineexplicit |
Constructs a new line wrapper with given settings.
[in] | settings | Wrapping settings. |
Does not throw.
size_t gmx::TextLineWrapper::findNextLine | ( | const char * | input, |
size_t | lineStart | ||
) | const |
Finds the next line to be wrapped.
[in] | input | String to wrap. |
[in] | lineStart | Index of first character of the line to find. |
If this is the last line, returns the length of input
. In determining the length of the returned line, this function considers the maximum line length, leaving space for indentation, and also whitespace stripping behavior. Thus, the line returned may be longer than the maximum line length if it has leading and/or trailing space. When wrapping a line on a space (not on an explicit line break), the returned index is always on a non-whitespace character after the space.
To iterate over lines in a string, use the following code:
Does not throw.
size_t gmx::TextLineWrapper::findNextLine | ( | const std::string & | input, |
size_t | lineStart | ||
) | const |
Finds the next line to be wrapped.
[in] | input | String to wrap. |
[in] | lineStart | Index of first character of the line to find. |
If this is the last line, returns the length of input
. In determining the length of the returned line, this function considers the maximum line length, leaving space for indentation, and also whitespace stripping behavior. Thus, the line returned may be longer than the maximum line length if it has leading and/or trailing space. When wrapping a line on a space (not on an explicit line break), the returned index is always on a non-whitespace character after the space.
To iterate over lines in a string, use the following code:
Does not throw.
std::string gmx::TextLineWrapper::formatLine | ( | const std::string & | input, |
size_t | lineStart, | ||
size_t | lineEnd | ||
) | const |
Formats a single line for output according to wrapping settings.
[in] | input | Input string. |
[in] | lineStart | Index of first character of the line to format. |
[in] | lineEnd | Index of first character of the next line. |
std::bad_alloc | if out of memory. |
Intended to be used on the lines found by findNextLine(). When used with the lines returned from findNextLine(), the returned line conforms to the wrapper settings. Trailing whitespace is always stripped (including any newlines, i.e., the return value does not contain a newline).
|
inline |
Provides access to settings of this wrapper.
The returned object can be used to modify settings for the wrapper. All subsequent calls to wrapToString() and wrapToVector() use the modified settings.
Does not throw.
std::string gmx::TextLineWrapper::wrapToString | ( | const std::string & | input | ) | const |
Formats a string, producing a single string with all the lines.
[in] | input | String to wrap. |
input
with added newlines such that maximum line length is not exceeded. std::bad_alloc | if out of memory. |
Newlines in the input are preserved, including terminal newlines. Note that if the input does not contain a terminal newline, the output does not either.
std::vector< std::string > gmx::TextLineWrapper::wrapToVector | ( | const std::string & | input | ) | const |
Formats a string, producing a vector with all the lines.
[in] | input | String to wrap. |
input
split into lines such that maximum line length is not exceeded. std::bad_alloc | if out of memory. |
The strings in the returned vector do not contain newlines at the end. Note that a single terminal newline does not affect the output: "line\\n" and "line" both produce the same output (but "line\\n\\n" produces two lines, the second of which is empty).