string_utils.hpp

Simple functions to manipulate strings. Status: RELEASE.

Typedefs

using string_vec_t = vector<std::string>

Functions

static inline const std::string &empty_string()

Return a const reference to an empty string. This function is useful to implement other functions that need to return a const reference for efficiency, but also need a null response.

static inline size_t count(const std::string &str, char c)

Count the number of times a specific character appears in a string (a clean shortcut to std::count)

static inline bool is_literal_char(const std::string &value)

Test if an input string is properly formatted as a literal character.

static inline bool is_literal_string(const std::string &value, const std::string &quote_marks = "\"")

Test if an input string is properly formatted as a literal string.

static inline std::string diagnose_literal_string(const std::string &value, const std::string &quote_marks = "\"")

Test if an input string is properly formatted as a literal string.

static inline bool is_composed_of(const std::string &test_str, const std::string &char_set)

Determine if a string is composed only of a set of characters (represented as a string)

inline bool has_whitespace(const std::string &str)

Determine if there is whitespace anywhere in a string.

inline bool has_nonwhitespace(const std::string &str)
inline bool has_upper_letter(const std::string &str)
inline bool has_lower_letter(const std::string &str)
inline bool has_letter(const std::string &str)
inline bool has_digit(const std::string &str)
inline bool has_alphanumeric(const std::string &str)
inline bool has_char_at(const std::string &str, char c, size_t pos)
inline bool has_one_of_at(const std::string &str, const std::string &opts, size_t pos)
inline bool has_digit_at(const std::string &str, size_t pos)
inline bool has_letter_at(const std::string &str, size_t pos)
inline bool is_lower(const std::string &str)

Determine if there are only lowercase letters in a string.

inline bool is_upper(const std::string &str)

Determine if there are only uppercase letters in a string.

inline bool is_digits(const std::string &str)

Determine if there are only digits in a string.

inline bool is_number(const std::string &str)

Determine if this string represents a proper number.

inline bool is_alphanumeric(const std::string &str)

Determine if string is only letters or digits.

inline bool is_whitespace(const std::string &str)
inline bool is_identifier(const std::string &str)

Determine if string is only letters, digits, or underscore (‘_’).

static inline bool has_one_of(const std::string &str, const std::string &char_set)

Determine if a specified set of characters appears anywhere in a string.

template<typename ...FUNS>
static inline bool is_valid(const std::string &str, FUNS... funs)

For a string to be valid, each character must pass at least one provided function.

inline bool has_prefix(const std::string &in_string, const std::string &prefix)

Test if a string has a given prefix.

static inline size_t find_quote_match(std::string_view in_string, const size_t start_pos = 0, char mark = '"')
static inline size_t find_paren_match(std::string_view in_string, const size_t start_pos = 0, const char open = '(', const char close = ')', const bool skip_quotes = true)
static inline size_t find(std::string_view in_string, std::string target, size_t start_pos, bool skip_quotes = false, bool skip_parens = false, bool skip_braces = false, bool skip_brackets = false)
static inline void find_all(std::string_view in_string, char target, vector<size_t> &results, const bool skip_quotes = false, bool skip_parens = false, bool skip_braces = false, bool skip_brackets = false)
static inline vector<size_t> find_all(std::string_view in_string, char target, bool skip_quotes = false, bool skip_parens = false, bool skip_braces = false, bool skip_brackets = false)
template<typename ...Ts>
static inline size_t find_any_of_from(const std::string &test_str, size_t start_pos, std::string test1, Ts... tests)

Return the first position found for any of a set of substring tests (or std::string::npos if none are found).

template<typename T, typename ...Ts>
static inline size_t find_any_of(const std::string &test_str, T test1, Ts... tests)
static inline size_t find_id(std::string_view in_string, std::string target, size_t start_pos, bool skip_quotes = true, bool skip_parens = false, bool skip_braces = false, bool skip_brackets = false)
static inline size_t find_non_whitespace(std::string_view in_string, size_t pos)
static inline std::string to_escaped_string(char value)

Convert a single character to one that uses a proper escape sequence (in a string) if needed.

static inline std::string to_escaped_string(const std::string &value)

Convert a full string to one that uses proper escape sequences, as needed.

inline std::string to_web_safe_string(const std::string &value)

Take a string and replace reserved HTML characters with character entities.

template<bool encode_space = false>
std::string url_encode(const std::string &value)

Returns url encoding of value. See https://en.wikipedia.org/wiki/Percent-encoding

template<bool decode_plus = false>
std::string url_decode(const std::string &str)

Returns url decoding of string. See https://en.wikipedia.org/wiki/Percent-encoding

template<typename T>
inline std::enable_if<!IsIterable<T>::value, std::string>::type to_literal(const T &value)

Take a value and convert it to a C++-style literal.

static inline std::string to_literal(char value)

Take a char and convert it to a C++-style literal.

static inline std::string to_literal(const std::string &value)

Take a string or iterable and convert it to a C++-style literal.

static inline char from_literal_char(const std::string &value)

Convert a literal character representation to an actual string. (i.e., ‘A’, ‘;’, or ‘

’)

static inline std::string from_literal_string(const std::string &value, [[maybe_unused]] const std::string &quote_marks = "\"")

Convert a literal string representation to an actual string.

static inline std::string to_upper(std::string value)

Convert a string to all uppercase.

static inline std::string to_lower(std::string value)

Convert a string to all lowercase.

static inline std::string to_titlecase(std::string value)

Make first letter of each word upper case.

static inline std::string to_roman_numeral(int val, const std::string &prefix = "")

Convert an integer to a roman numeral string.

static inline void trim_whitespace(std::string &in_str)

Remove whitespace from the beginning or end of a string.

static inline void compress_whitespace(std::string &in_string)

Every time one or more whitespace characters appear replace them with a single space.

static inline void remove_whitespace(std::string &in_string)

Remove all whitespace from anywhere within a string.

static inline void remove_punctuation(std::string &in_string)

Remove all characters from a string except letters, numbers, and whitespace.

static inline void remove_chars(std::string &in_string, std::string chars)

Remove instances of characters from file.

static inline std::string slugify(const std::string &in_string)

Make a string safe(r)

static inline std::string combine_strings(const string_vec_t &strings, std::string spacer = " ")
static inline std::string to_english_list(const string_vec_t &strings)

Convert a vector of strings to an English list, such as “one, two, three, and four.”.

static inline string_vec_t transform_strings(const string_vec_t &in_strings, std::function<std::string(const std::string&)> fun)

Transform all strings in a vector.

static inline string_vec_t quote_strings(const string_vec_t &in_strings, const std::string quote = "'")

Put all strings provided in quotes (Like ‘this’), pre- and post-fixing another string if provided.

static inline string_vec_t quote_strings(const string_vec_t &in_strings, const std::string open_quote, const std::string close_quote)

Pre-pend and post-pend specified sequences to all strings provided.

static inline std::string to_quoted_list(const string_vec_t &in_strings, const std::string quote = "'")

Take a vector of strings, put them in quotes, and then transform it into an English list.

static inline bool string_pop_if_char(std::string &in_string, char c)
static inline std::string string_pop_fixed(std::string &in_string, std::size_t end_pos, size_t delim_size = 0)

Pop a segment from the beginning of a string as another string, shortening original.

static inline std::string string_get_range(const std::string &in_string, std::size_t start_pos, std::size_t end_pos)

Get a segment from the beginning of a string as another string, leaving original untouched.

inline std::string string_pop(std::string &in_string, const char delim = ' ')

Remove a prefix of the input string (up to a specified delimeter) and return it. If the delimeter is not found, return the entire input string and clear it.

inline std::string string_get(const std::string &in_string, const char delim = ' ', size_t start_pos = 0)

Return a prefix of the input string (up to a specified delimeter), but do not modify it. If the delimeter is not found, return the entire input string.

inline std::string string_pop(std::string &in_string, const std::string &delim_set)

Remove a prefix of the input string (up to any of a specified set of delimeters) and return it. If the delimeter is not found, return the entire input string and clear it.

inline std::string string_get(const std::string &in_string, const std::string &delim_set, size_t start_pos = 0)

Return a prefix of the input string (up to any of a specified set of delimeters), but do not modify it. If the delimeter is not found, return the entire input string.

inline std::string string_pop_to(std::string &in_string, const std::string &delim = " ", size_t start_pos = 0, bool skip_quotes = false, bool skip_parens = false, bool skip_braces = false, bool skip_brackets = false)
inline std::string string_get_to(const std::string &in_string, const std::string &delim = " ", size_t start_pos = 0)
inline std::string string_pop_word(std::string &in_string)

Remove a prefix of a string, up to the first whitespace, and return it.

inline std::string string_get_word(const std::string &in_string, size_t start_pos = 0)

Return a prefix of a string, up to the first whitespace (do not modify the original string)

inline std::string string_pop_line(std::string &in_string)

Remove a prefix of a string, up to the first newline, and return it.

inline std::string string_get_line(const std::string &in_string, size_t start_pos = 0)

Return a prefix of a string, up to the first newline (do not modify the original string)

inline std::string string_pop_quote(std::string &in_string)
inline size_t string_uint_size(const std::string &in_string)
inline unsigned long long string_pop_uint(std::string &in_string)
inline unsigned long long string_get_uint(const std::string &in_string)
inline std::string left_justify(std::string &in_string)

Remove all whitespace at the beginning of a string. Return the whitespace removed.

inline void right_justify(std::string &in_string)

Remove all whitespace at the end of a string.

inline void justify(std::string &in_string)

Remove all whitespace at both the beginning and the end of a string.

template<typename MAP_T>
std::string replace_vars(const std::string &in_string, const MAP_T &var_map)

Find any instances of ${X} and replace with dictionary lookup of X.

template<typename FUN_T>
std::string replace_macro(const std::string &str, std::string macro_name, FUN_T &&fun, bool skip_quotes = true)

Find any instance of MACRO_NAME(ARGS) and call replace it with fun(ARGS).

Find any instance of MACRO_NAME(ARGS) and replace it with fun(ARGS).

Parameters:
  • in_string – String to perform macro replacement.

  • macro_name – Name of the macro to look for.

  • macro_fun – Function to call with contents of macro. Params are macro_args (string), line_num (size_t), and hit_num (size_t)

  • skip_quotes – Should we skip quotes when looking for macro?

Returns:

Processed version of in_string with macros replaced.

static inline std::string_view view_string(const std::string_view &str)

Provide a string_view on a given string.

static inline std::string_view view_string(const std::string_view &str, size_t start)

Provide a string_view on a string from a given starting point.

static inline std::string_view view_string(const std::string_view &str, size_t start, size_t npos)

Provide a string_view on a string from a starting point with a given size.

static inline std::string_view view_string_front(const std::string_view &str, size_t npos)

Provide a string_view on a string from the beginning to a given size.

static inline std::string_view view_string_back(const std::string_view &str, size_t npos)

Provide a string_view on a string from a starting point with a given size.

static inline std::string_view view_string_range(const std::string_view &str, size_t start, size_t end)

Provide a string_view on a string from a starting point to an ending point.

static inline std::string_view view_string_to(const std::string_view &in_string, const char delim, size_t start_pos = 0)

Return a view of the prefix of the input string up to a specified delimeter. If the delimeter is not found, return the entire input string.

inline std::string pad_front(const std::string &in_str, char padding, size_t target_size)
inline std::string pad_back(const std::string &in_str, char padding, size_t target_size)
inline std::string repeat(const std::string &value, const size_t n)

Concatenate n copies of a string.

static inline void slice(const std::string_view &in_string, vector<std::string> &out_set, const char delim = '\n', const size_t max_split = std::numeric_limits<size_t>::max(), const bool keep_quotes = false, const bool keep_parens = false, const bool keep_braces = false, const bool keep_brackets = false)

Cut up a string based on the provided delimiter; fill them in to the provided vector.

Cut up a string based on the provided delimiter; fill them in to the provided vector.

Parameters:
  • in_string – string to be sliced

  • out_set – destination

  • delim – delimiter to split on

  • max_split – defines the maximum number of splits

  • keep_quotes – Should quoted text be kept together?

  • keep_parens – Should parentheses (‘(’ and ‘)’) be kept together?

  • keep_braces – Should braces (‘{’ and ‘}’) be kept together?

  • keep_brackets – Should brackets (‘[’ and ‘]’) be kept together?

  • in_string – string to be sliced

  • out_set – destination

  • delim – delimiter to split on

  • max_split – defines the maximum number of splits

  • keep_quotes – Should quoted text be treated as a unit?

  • keep_parens – Should parens be treated as a unit?

  • keep_braces – Should braces be treated as a unit?

  • keep_brackets – Should brackets be treated as a unit?

static inline vector<std::string> slice(const std::string_view &in_string, const char delim = '\n', const size_t max_split = std::numeric_limits<size_t>::max(), const bool keep_quotes = false, const bool keep_parens = false, const bool keep_braces = false, const bool keep_brackets = false)

Slice a string without passing in result vector (may be less efficient).

Parameters:
  • in_string – string to be sliced

  • delim – delimiter to split on

  • max_split – defines the maximum number of splits

  • keep_quotes – Should quoted text be kept together?

  • keep_parens – Should parentheses (‘(’ and ‘)’) be kept together?

  • keep_braces – Should braces (‘{’ and ‘}’) be kept together?

  • keep_brackets – Should brackets (‘[’ and ‘]’) be kept together?

static inline void view_slices(const std::string_view &in_string, vector<std::string_view> &out_set, char delim = '\n', const bool keep_quotes = false, const bool keep_parens = false, const bool keep_braces = false, const bool keep_brackets = false)

Create a set of string_views based on the provided delimiter; fill them in to the provided vector.

Parameters:
  • in_string – string to be sliced

  • out_set – destination vector

  • delim – delimiter to split on

  • keep_quotes – Should quoted text be kept together?

  • keep_parens – Should parentheses (‘(’ and ‘)’) be kept together?

  • keep_braces – Should braces (‘{’ and ‘}’) be kept together?

  • keep_brackets – Should brackets (‘[’ and ‘]’) be kept together?

static inline vector<std::string_view> view_slices(const std::string_view &in_string, char delim = '\n', const bool keep_quotes = false, const bool keep_parens = false, const bool keep_braces = false, const bool keep_brackets = false)

Slice a string without passing in result vector (may be less efficient).

static inline std::map<std::string, std::string> slice_assign(const std::string_view &in_string, const char delim = ',', std::string assign_op = "=", const size_t max_split = std::numeric_limits<size_t>::max(), const bool trim_whitespace = true, const bool keep_quotes = true, const bool keep_parens = true, const bool keep_braces = true, const bool keep_brackets = true)

Slice a string without passing in result vector (may be less efficient).

Parameters:
  • in_string – string to be sliced

  • delim – delimiter to split on (default ‘,’)

  • assign – separator for left and right side of assignment (default: “=”)

  • max_split – defines the maximum number of splits (default, no max)

  • keep_quotes – Should quoted text be kept together? (default: no)

  • trim_whitespace – Should extra whitespace around delim or assign be ignored?

static inline vector<std::string_view> ViewCSV(const std::string_view &in_string)
static inline std::string_view ViewNestedBlock(std::string_view str, const std::string symbols = "()", size_t start = 0)

View a section of a string with the properly matching nested blocks. For example if ((abc(de))f(ghi)) would return “(abc(de))f(ghi)” at 0, “de” at 5, or “” at 2 (since there is no start!)

template<typename T, size_t N>
inline std::string ToString(const array<T, N> &container)

Setup ToString declarations for built-in types.

Setup ToString to work on arrays.

template<typename T, typename ...Ts>
inline std::string ToString(const vector<T, Ts...> &container)

Setup ToString to work on vectors.

template<typename ...Ts>
inline std::string to_string(const Ts&... values)

This function does its best to convert any type to a string. Accepts any number of arguments and returns a single concatenated string. Conversions attempted for an object ‘x’ include (in order):

  • Call a x.ToString()

  • Call appropriate ToString(x) overload

  • Pass x through stringstream

inline const std::string &to_string(const std::string &value)

Overload of to_string() string arguments to be directly returned.

template<typename T>
inline T from_string(const std::string &str)

This function tries to convert a string into any type you’re looking for… You just need to specify the out type as the template argument.

template<typename ...Ts>
inline void from_string(const std::string &str, Ts&... args)

The from_string() function can also take multiple args instead of a return.

template<typename T>
inline vector<T> from_strings(const vector<std::string> &string_v)

The from_strings() function takes a vector of strings and converts them into a vector of the appropriate type.

template<typename T>
inline T from_string(std::string_view str)

This function tries to convert a string_view into any other type… You must need to specify the out type as the template argument.

template<typename CONTAINER_T>
inline std::string join(const CONTAINER_T &container, std::string join_str = "")

This function returns values from a container as a single string separated by a given delimeter.

Parameters:
  • container – is any standard-interface container holding objects to be joined.

  • join_str – optional delimeter

Returns:

merged string of all values

inline constexpr char ANSI_ESC()
inline std::string ANSI_Reset()
inline std::string ANSI_Bold()
inline std::string ANSI_Faint()
inline std::string ANSI_Italic()
inline std::string ANSI_Underline()
inline std::string ANSI_SlowBlink()
inline std::string ANSI_Blink()
inline std::string ANSI_Reverse()
inline std::string ANSI_Strike()
inline std::string ANSI_NoBold()
inline std::string ANSI_NoItalic()
inline std::string ANSI_NoUnderline()
inline std::string ANSI_NoBlink()
inline std::string ANSI_NoReverse()
inline std::string ANSI_Black()
inline std::string ANSI_Red()
inline std::string ANSI_Green()
inline std::string ANSI_Yellow()
inline std::string ANSI_Blue()
inline std::string ANSI_Magenta()
inline std::string ANSI_Cyan()
inline std::string ANSI_White()
inline std::string ANSI_DefaultColor()
inline std::string ANSI_BlackBG()
inline std::string ANSI_RedBG()
inline std::string ANSI_GreenBG()
inline std::string ANSI_YellowBG()
inline std::string ANSI_BlueBG()
inline std::string ANSI_MagentaBG()
inline std::string ANSI_CyanBG()
inline std::string ANSI_WhiteBG()
inline std::string ANSI_DefaultBGColor()
inline std::string ANSI_BrightBlack()
inline std::string ANSI_BrightRed()
inline std::string ANSI_BrightGreen()
inline std::string ANSI_BrightYellow()
inline std::string ANSI_BrightBlue()
inline std::string ANSI_BrightMagenta()
inline std::string ANSI_BrightCyan()
inline std::string ANSI_BrightWhite()
inline std::string ANSI_BrightBlackBG()
inline std::string ANSI_BrightRedBG()
inline std::string ANSI_BrightGreenBG()
inline std::string ANSI_BrightYellowBG()
inline std::string ANSI_BrightBlueBG()
inline std::string ANSI_BrightMagentaBG()
inline std::string ANSI_BrightCyanBG()
inline std::string ANSI_BrightWhiteBG()
inline std::string to_ansi_bold(const std::string &_in)

Make a string appear bold when printed to the command line.

inline std::string to_ansi_italic(const std::string &_in)

Make a string appear italics when printed to the command line.

inline std::string to_ansi_underline(const std::string &_in)

Make a string appear underline when printed to the command line.

inline std::string to_ansi_blink(const std::string &_in)

Make a string appear blink when printed to the command line.

inline std::string to_ansi_reverse(const std::string &_in)

Make a string appear reverse when printed to the command line.

static inline size_t next_pos(std::string_view in_string, size_t pos, bool keep_quotes = false, bool keep_parens = false, bool keep_braces = false, bool keep_brackets = false)

Advance a position in a string, respecting quotes, parens, braces, and brackets as indicated

Parameters:
  • in_string – string being stepped through

  • pos – Position to advance from

  • keep_quotes – Should quoted text be treated as a unit?

  • keep_parens – Should parens be treated as a unit?

  • keep_braces – Should braces be treated as a unit?

  • keep_brackets – Should brackets be treated as a unit?