CBMC
string_utils.cpp File Reference
#include "string_utils.h"
#include "exception_utils.h"
#include "invariant.h"
#include <algorithm>
#include <cctype>
#include <iomanip>
+ Include dependency graph for string_utils.cpp:

Go to the source code of this file.

Functions

std::string strip_string (const std::string &s)
 Remove all whitespace characters from either end of a string. More...
 
void split_string (const std::string &s, char delim, std::vector< std::string > &result, bool strip, bool remove_empty)
 
void split_string (const std::string &s, char delim, std::string &left, std::string &right, bool strip)
 
std::vector< std::string > split_string (const std::string &s, char delim, bool strip, bool remove_empty)
 Given a string s, split into a sequence of substrings when separated by specified delimiter. More...
 
std::string trim_from_last_delimiter (const std::string &s, const char delim)
 
std::string escape (const std::string &s)
 Generic escaping of strings; this is not meant to be a particular programming language. More...
 
std::string escape_non_alnum (const std::string &to_escape)
 Replace non-alphanumeric characters with _xx escapes, where xx are hex digits. More...
 
std::string capitalize (const std::string &str)
 
std::string wrap_line (const std::string &line, const std::size_t left_margin, const std::size_t width)
 Wrap line at spaces to not extend past the right margin, and include given padding with spaces to the left. More...
 
std::string wrap_line (std::string::const_iterator left, std::string::const_iterator right, const std::size_t left_margin, const std::size_t width)
 Wrap line at spaces to not extend past the right margin, and include given padding with spaces to the left. More...
 

Function Documentation

◆ capitalize()

std::string capitalize ( const std::string &  str)

Definition at line 175 of file string_utils.cpp.

◆ escape()

std::string escape ( const std::string &  s)

Generic escaping of strings; this is not meant to be a particular programming language.

Definition at line 138 of file string_utils.cpp.

◆ escape_non_alnum()

std::string escape_non_alnum ( const std::string &  to_escape)

Replace non-alphanumeric characters with _xx escapes, where xx are hex digits.

Underscores are replaced by __.

Parameters
to_escapestring to escape
Returns
string with non-alphanumeric characters escaped

Definition at line 153 of file string_utils.cpp.

◆ split_string() [1/3]

std::vector<std::string> split_string ( const std::string &  s,
char  delim,
bool  strip = false,
bool  remove_empty = false 
)

Given a string s, split into a sequence of substrings when separated by specified delimiter.

Parameters
sThe string to split up
delimThe character to use as the delimiter
stripIf true, strip_string will be used on each element, removing whitespace from the beginning and end of each element
remove_emptyIf true, all empty-string elements will be removed. This is applied after strip so whitespace only elements will be removed if both are set to true.

Definition at line 116 of file string_utils.cpp.

◆ split_string() [2/3]

void split_string ( const std::string &  s,
char  delim,
std::string &  left,
std::string &  right,
bool  strip 
)

Definition at line 91 of file string_utils.cpp.

◆ split_string() [3/3]

void split_string ( const std::string &  s,
char  delim,
std::vector< std::string > &  result,
bool  strip,
bool  remove_empty 
)

Definition at line 39 of file string_utils.cpp.

◆ strip_string()

std::string strip_string ( const std::string &  s)

Remove all whitespace characters from either end of a string.

Whitespace in the middle of the string is left unchanged

Parameters
sthe string to strip
Returns
The stripped string

Definition at line 21 of file string_utils.cpp.

◆ trim_from_last_delimiter()

std::string trim_from_last_delimiter ( const std::string &  s,
const char  delim 
)

Definition at line 127 of file string_utils.cpp.

◆ wrap_line() [1/2]

std::string wrap_line ( const std::string &  line,
const std::size_t  left_margin = 0,
const std::size_t  width = 80 
)

Wrap line at spaces to not extend past the right margin, and include given padding with spaces to the left.

The given string should not contain any newlines.

Parameters
lineline to wrap, should not contain newlines
left_margineach line will be padded to the left with left_margin spaces
widthwidth of the resulting text, i.e., right margin
Returns
resulting string such that each line has length less or equal to width, and each line includes left_margin spaces to the left; if the given line cannot be wrapped (i.e., it cannot be broken up at spaces such that every resulting line fits within the margin), the given line is returned unchanged

Definition at line 184 of file string_utils.cpp.

◆ wrap_line() [2/2]

std::string wrap_line ( const std::string::const_iterator  left,
const std::string::const_iterator  right,
const std::size_t  left_margin = 0,
const std::size_t  width = 80 
)

Wrap line at spaces to not extend past the right margin, and include given padding with spaces to the left.

The given string should not contain any newlines.

Parameters
leftiterator to beginning of string
rightiterator to end of string
left_margineach line will be padded to the left with left_margin spaces
widthwidth of the resulting text, i.e., right margin
Returns
resulting string such that each line has length less or equal to width, and each line includes left_margin spaces to the left; if the given line cannot be wrapped (i.e., it cannot be broken up at spaces such that every resulting line fits within the margin), the given line is returned unchanged

Definition at line 192 of file string_utils.cpp.