CPP-TOOLBOX
Loading...
Searching...
No Matches
text_utils Namespace Reference

Classes

class  MultilineStringAccumulator
 
struct  Node
 
class  StringAccumulator
 

Functions

std::string remove_consecutive_duplicates (const std::string &input, const std::string &dedup_chars="")
 Remove consecutive duplicate characters from a string.
 
std::string abbreviate_snake_case (const std::string &input)
 Abbreviate a snake_case string by shortening each word.
 
bool is_integer (const std::string &str)
 Check if a string represents an integer.
 
bool is_rational (const std::string &str)
 Check if a string represents a rational (floating-point) number.
 
std::string add_newlines_to_long_string (const std::string &text, size_t max_chars_per_line=25)
 Insert newlines into long strings.
 
std::vector< std::string > split (const std::string &str, const std::string &delimiter)
 Split a string by a delimiter.
 
std::vector< std::string > split_once_from_right (const std::string &str, const std::string &delimiter)
 Split a string once from the right.
 
std::string join (const std::vector< std::string > &elements, const std::string &separator)
 Join elements into a single string with a separator.
 
std::string trim (const std::string &s)
 Trim whitespace from both ends of a string.
 
std::string pascal_to_snake_case (const std::string &input)
 Convert a PascalCase string to snake_case.
 
std::string snake_to_pascal_case (const std::string &input)
 Convert a snake_case string to PascalCase.
 
std::string join_multiline (const std::string &input, bool replace_newlines_with_space=false)
 Join a string with newlines removed or replaced.
 
std::string replace_char (const std::string &input, char from_char, char to_char)
 Replace a character with another in a string.
 
std::string replace_chars (const std::string &input, const std::unordered_map< char, char > &mapping)
 Replace characters in a string according to a mapping.
 
std::string replace_substring (const std::string &input, const std::string &from_substr, const std::string &to_substr)
 Replace all occurrences of a substring with another substring.
 
bool starts_with (const std::string &str, const std::string &prefix)
 Check if a string starts with a prefix.
 
bool contains (const std::string &str, const std::string &substr)
 Check if a string contains a substring.
 
std::string get_substring (const std::string &input, size_t start, size_t end)
 Extract a substring from start to end indices.
 
std::string remove_newlines (const std::string &input)
 Remove all newlines from a string.
 
std::string collapse_whitespace (const std::string &input)
 Collapse consecutive whitespace into a single space.
 
std::string replace_literal_newlines_with_real (const std::string &input)
 Replace literal "\n" sequences with real newlines.
 
std::string indent (const std::string &text, int indent_level, int spaces_per_indent=4)
 Indent text by a given number of levels.
 
std::string surround (const std::string &str, const std::string &left, const std::string &right="")
 Surround a string with left and right substrings.
 
std::string generate_abbreviation (const std::string &snake_case_name)
 
std::string generate_unique_abbreviation (std::unordered_map< std::string, std::string > &current_abbreviation_map, const std::string &word_to_abbreviate)
 
std::unordered_map< std::string, std::string > map_words_to_abbreviations (const std::vector< std::string > &words)
 Create a map from words to their abbreviations.
 
std::string parse_token (const std::string &s, size_t &pos)
 
Node parse_block (const std::string &s, size_t &pos)
 
std::vector< std::string > build_buffer (const Node &node)
 
std::string format_nested_brace_string_recursive (const std::string &input)
 

Variables

const std::string double_quote = "\""
 
const std::string single_quote = "'"
 
const std::string newline = "\n"
 
const std::string tab = "\t"
 
const std::string space = " "
 
const std::string empty = ""
 
const std::string comma = ","
 
const std::string period = "."
 
const std::string colon = ":"
 
const std::string semicolon = ";"
 
const std::string dash = "-"
 
const std::string underscore = "_"
 
const std::string slash = "/"
 
const std::string backslash = "\\"
 
const std::string pipe = "|"
 
const std::string ampersand = "&"
 
const std::string at_sign = "@"
 
const std::string hash = "#"
 
const std::string dollar = "$"
 
const std::string percent = "%"
 
const std::string caret = "^"
 
const std::string asterisk = "*"
 
const std::string plus = "+"
 
const std::string equals = "="
 
const std::string question_mark = "?"
 
const std::string exclamation_mark = "!"
 
const std::string left_paren = "("
 
const std::string right_paren = ")"
 
const std::string left_bracket = "["
 
const std::string right_bracket = "]"
 
const std::string left_brace = "{"
 
const std::string right_brace = "}"
 
const std::string less_than = "<"
 
const std::string greater_than = ">"
 
const std::string newline_windows = "\r\n"
 
const std::string carriage_return = "\r"
 
const std::string natural_numbers = "ℕ"
 
const std::string element_of = "∈"
 

Function Documentation

◆ abbreviate_snake_case()

std::string text_utils::abbreviate_snake_case ( const std::string & input)

Abbreviate a snake_case string by shortening each word.

Parameters
inputInput snake_case string.
Returns
Abbreviated string.

◆ add_newlines_to_long_string()

std::string text_utils::add_newlines_to_long_string ( const std::string & text,
size_t max_chars_per_line = 25 )

Insert newlines into long strings.

Parameters
textInput string.
max_chars_per_lineMaximum characters per line.
Returns
String with line breaks inserted.

◆ build_buffer()

std::vector< std::string > text_utils::build_buffer ( const Node & node)

◆ collapse_whitespace()

std::string text_utils::collapse_whitespace ( const std::string & input)

Collapse consecutive whitespace into a single space.

◆ contains()

bool text_utils::contains ( const std::string & str,
const std::string & substr )

Check if a string contains a substring.

◆ format_nested_brace_string_recursive()

std::string text_utils::format_nested_brace_string_recursive ( const std::string & input)

◆ generate_abbreviation()

std::string text_utils::generate_abbreviation ( const std::string & snake_case_name)

◆ generate_unique_abbreviation()

std::string text_utils::generate_unique_abbreviation ( std::unordered_map< std::string, std::string > & current_abbreviation_map,
const std::string & word_to_abbreviate )

◆ get_substring()

std::string text_utils::get_substring ( const std::string & input,
size_t start,
size_t end )

Extract a substring from start to end indices.

◆ indent()

std::string text_utils::indent ( const std::string & text,
int indent_level,
int spaces_per_indent = 4 )

Indent text by a given number of levels.

Parameters
textInput string.
indent_levelIndentation level.
spaces_per_indentNumber of spaces per level.
Returns
Indented string.

◆ is_integer()

bool text_utils::is_integer ( const std::string & str)

Check if a string represents an integer.

◆ is_rational()

bool text_utils::is_rational ( const std::string & value)

Check if a string represents a rational (floating-point) number.

◆ join()

std::string text_utils::join ( const std::vector< std::string > & elements,
const std::string & separator )

Join elements into a single string with a separator.

Parameters
elementsVector of strings.
separatorSeparator string.
Returns
Concatenated string.

◆ join_multiline()

std::string text_utils::join_multiline ( const std::string & input,
bool replace_newlines_with_space = false )

Join a string with newlines removed or replaced.

Parameters
inputInput string with multiple lines.
replace_newlines_with_spaceWhether to replace newlines with spaces.
Returns
Joined string.

◆ map_words_to_abbreviations()

std::unordered_map< std::string, std::string > text_utils::map_words_to_abbreviations ( const std::vector< std::string > & words)

Create a map from words to their abbreviations.

Parameters
wordsList of words.
Returns
Map where keys are words and values are abbreviations.

◆ parse_block()

Node text_utils::parse_block ( const std::string & s,
size_t & pos )

◆ parse_token()

std::string text_utils::parse_token ( const std::string & s,
size_t & pos )

◆ pascal_to_snake_case()

std::string text_utils::pascal_to_snake_case ( const std::string & input)

Convert a PascalCase string to snake_case.

Parameters
inputInput PascalCase string.
Returns
Converted string.

◆ remove_consecutive_duplicates()

std::string text_utils::remove_consecutive_duplicates ( const std::string & input,
const std::string & dedup_chars = "" )

Remove consecutive duplicate characters from a string.

Parameters
inputInput string.
dedup_charsCharacters to deduplicate (empty = all).
Returns
String with duplicates removed.

◆ remove_newlines()

std::string text_utils::remove_newlines ( const std::string & input)

Remove all newlines from a string.

◆ replace_char()

std::string text_utils::replace_char ( const std::string & input,
char from_char,
char to_char )

Replace a character with another in a string.

◆ replace_chars()

std::string text_utils::replace_chars ( const std::string & input,
const std::unordered_map< char, char > & mapping )

Replace characters in a string according to a mapping.

◆ replace_literal_newlines_with_real()

std::string text_utils::replace_literal_newlines_with_real ( const std::string & input)

Replace literal "\n" sequences with real newlines.

◆ replace_substring()

std::string text_utils::replace_substring ( const std::string & input,
const std::string & from_substr,
const std::string & to_substr )

Replace all occurrences of a substring with another substring.

◆ snake_to_pascal_case()

std::string text_utils::snake_to_pascal_case ( const std::string & input)

Convert a snake_case string to PascalCase.

Parameters
inputInput snake_case string.
Returns
Converted string.

◆ split()

std::vector< std::string > text_utils::split ( const std::string & str,
const std::string & delimiter )

Split a string by a delimiter.

Parameters
strInput string.
delimiterDelimiter string.
Returns
Vector of substrings.

◆ split_once_from_right()

std::vector< std::string > text_utils::split_once_from_right ( const std::string & str,
const std::string & delimiter )

Split a string once from the right.

Parameters
strInput string.
delimiterDelimiter string.
Returns
Vector with up to two substrings.

◆ starts_with()

bool text_utils::starts_with ( const std::string & str,
const std::string & prefix )

Check if a string starts with a prefix.

◆ surround()

std::string text_utils::surround ( const std::string & str,
const std::string & left,
const std::string & right = "" )

Surround a string with left and right substrings.

Parameters
strInput string.
leftLeft surround string.
rightRight surround string (default empty).
Returns
Surrounded string.

◆ trim()

std::string text_utils::trim ( const std::string & s)

Trim whitespace from both ends of a string.

Variable Documentation

◆ ampersand

const std::string text_utils::ampersand = "&"
inline

◆ asterisk

const std::string text_utils::asterisk = "*"
inline

◆ at_sign

const std::string text_utils::at_sign = "@"
inline

◆ backslash

const std::string text_utils::backslash = "\\"
inline

◆ caret

const std::string text_utils::caret = "^"
inline

◆ carriage_return

const std::string text_utils::carriage_return = "\r"
inline

◆ colon

const std::string text_utils::colon = ":"
inline

◆ comma

const std::string text_utils::comma = ","
inline

◆ dash

const std::string text_utils::dash = "-"
inline

◆ dollar

const std::string text_utils::dollar = "$"
inline

◆ double_quote

const std::string text_utils::double_quote = "\""
inline

◆ element_of

const std::string text_utils::element_of = "∈"
inline

◆ empty

const std::string text_utils::empty = ""
inline

◆ equals

const std::string text_utils::equals = "="
inline

◆ exclamation_mark

const std::string text_utils::exclamation_mark = "!"
inline

◆ greater_than

const std::string text_utils::greater_than = ">"
inline

◆ hash

const std::string text_utils::hash = "#"
inline

◆ left_brace

const std::string text_utils::left_brace = "{"
inline

◆ left_bracket

const std::string text_utils::left_bracket = "["
inline

◆ left_paren

const std::string text_utils::left_paren = "("
inline

◆ less_than

const std::string text_utils::less_than = "<"
inline

◆ natural_numbers

const std::string text_utils::natural_numbers = "ℕ"
inline

◆ newline

const std::string text_utils::newline = "\n"
inline

◆ newline_windows

const std::string text_utils::newline_windows = "\r\n"
inline

◆ percent

const std::string text_utils::percent = "%"
inline

◆ period

const std::string text_utils::period = "."
inline

◆ pipe

const std::string text_utils::pipe = "|"
inline

◆ plus

const std::string text_utils::plus = "+"
inline

◆ question_mark

const std::string text_utils::question_mark = "?"
inline

◆ right_brace

const std::string text_utils::right_brace = "}"
inline

◆ right_bracket

const std::string text_utils::right_bracket = "]"
inline

◆ right_paren

const std::string text_utils::right_paren = ")"
inline

◆ semicolon

const std::string text_utils::semicolon = ";"
inline

◆ single_quote

const std::string text_utils::single_quote = "'"
inline

◆ slash

const std::string text_utils::slash = "/"
inline

◆ space

const std::string text_utils::space = " "
inline

◆ tab

const std::string text_utils::tab = "\t"
inline

◆ underscore

const std::string text_utils::underscore = "_"
inline