String.hpp
Simple class to facilitate string manipulations.
- Todo:
Make constexpr
Make handle non-‘char’ strings (i.e., use CharT template parameter)
Make handle allocators
Make work more broadly with stringviews
Maybe add special construct types like RESERVE, REPEAT, and TO_STRING for special builds?
Note
Status: ALPHA
Functions
-
inline String MakeWebSafe(const String &in)
Take a string and replace reserved HTML characters with character entities.
-
inline String MakeLiteral(const std::string &value)
Take a string or iterable and convert it to a C++-style literal.
-
inline char MakeFromLiteral_Char(const String &value)
Convert a literal character representation to an actual string. (i.e., ‘A’, ‘;’, or ‘
’)
-
inline String MakeFromLiteral_String(const String &value)
Convert a literal string representation to an actual string.
-
inline String MakeCount(int val, String item, const String &plural_suffix)
Make a string with the correct pluralization of the item being counted. For example, MakeCount(1, “cow”) would produce “1 cow”, but MakeCount(2, “cow”) would produce “2 cows”.
-
template<typename CONTAINER_T>
inline String MakeEnglishList(const CONTAINER_T &container)
-
inline String MakeTrimFront(const String &in, const CharSet &chars = WhitespaceCharSet())
-
inline String MakeTrimBack(const String &in, const CharSet &chars = WhitespaceCharSet())
-
inline String MakeTrimmed(String in, const CharSet &chars)
Remove chars from the beginning AND end of a string.
-
inline String MakeCompressed(String in, const CharSet &chars = WhitespaceCharSet(), char compress_to = ' ', bool trim_start = true, bool trim_end = true)
-
inline String MakeRemoveChars(String in, const CharSet &chars)
Remove all instances of specified characters from file.
-
template<typename CONTAINER_T>
inline String Join(const CONTAINER_T &container, std::string join_str = "", std::string open = "", std::string close = "") This function returns values from a container as a single string separated by a given delimeter and with optional surrounding strings.
- Parameters:
container – is any standard-interface container holding objects to be joined.
join_str – optional delimeter
open – string to place before each string (e.g., “[” or “’”)
close – string to place after each string (e.g., “]” or “’”)
- Returns:
merged string of all values
-
template<typename ...Args>
String MakeFormatted(const std::string &format, Args... args) Apply sprintf-like formatting to a string. See https://en.cppreference.com/w/cpp/io/c/fprintf. Adapted from https://stackoverflow.com/a/26221725.
-
class String : public std::string
- #include <String.hpp>
Public Functions
-
String() = default
-
inline String(const char *_str)
-
inline String(size_t count, char _str)
-
inline String(const char *_str, size_t count)
-
inline char &operator[](size_t pos)
-
inline char operator[](size_t pos) const
-
inline char &front()
-
inline char front() const
-
inline char &back()
-
inline char back() const
-
inline char &Get(size_t pos)
-
inline char Get(size_t pos) const
-
inline std::string_view ViewTo(CharSet stop_chars, size_t start = 0, const Syntax &syntax = Syntax::None()) const
-
inline std::string_view ViewBackTo(CharSet stop_chars, size_t start = npos, const Syntax &syntax = Syntax::None()) const
-
inline size_t Hash() const noexcept
-
inline size_t Count(char c, size_t start = 0) const
-
inline size_t Count(char c, size_t start, size_t end) const
-
inline bool IsLiteralChar() const
Test if an string is formatted as a literal character.
Test if an input string is properly formatted as a literal character.
-
inline bool IsLiteralString(const String "e_marks = "\"") const
Test if an string is formatted as a literal string.
Test if an input string is properly formatted as a literal string.
-
inline String DiagnoseLiteralString(const String "e_marks = "\"") const
Explain what string is NOT formatted as a literal string.
Test if an input string is properly formatted as a literal string.
-
inline bool IsComposedOf(CharSet char_set) const
Is string composed only of a set of characters (can be provided as a string)
-
inline bool IsNumber() const
Is string a valid number (int, floating point, or scientific notation all valid)
Determine if this string represents a proper number.
-
inline bool IsIdentifier() const
Is string a valid identifier? At least one char; cannot begin with digit, only letters, digits and
_
-
inline bool OnlyLower() const
-
inline bool OnlyUpper() const
-
inline bool OnlyDigits() const
-
inline bool OnlyAlphanumeric() const
-
inline bool OnlyWhitespace() const
-
inline bool Has(char c) const
-
inline bool HasWhitespace() const
-
inline bool HasNonwhitespace() const
-
inline bool HasUpper() const
-
inline bool HasLower() const
-
inline bool HasLetter() const
-
inline bool HasDigit() const
-
inline bool HasAlphanumeric() const
-
inline bool HasCharAt(char c, size_t pos) const
-
inline bool HasDigitAt(size_t pos) const
-
inline bool HasLetterAt(size_t pos) const
-
inline bool HasWhitespaceAt(size_t pos) const
-
inline size_t CountWhitespace() const
-
inline size_t CountNonwhitespace() const
-
inline size_t CountUpper() const
-
inline size_t CountLower() const
-
inline size_t CountLetters() const
-
inline size_t CountDigits() const
-
inline size_t CountAlphanumeric() const
-
inline iterator erase(const_iterator pos)
-
inline iterator erase(const_iterator first, const_iterator last)
-
inline bool PopIf(char c)
-
inline String PopFixed(std::size_t end_pos, size_t delim_size = 0)
Pop a segment from the beginning of a string as another string, shortening original.
-
inline String Pop(CharSet chars = " \n\t\r", const Syntax &syntax = Syntax::None())
Remove a prefix of the string (up to a specified delimeter) and return it. If the delimeter is not found, return the entire string and clear it.
-
inline String PopTo(String delim, const Syntax &syntax = Syntax::None())
Remove a prefix of the string (up to a specified delimeter) and return it. If the delimeter is not found, return the entire string and clear it.
-
inline long long PopSigned()
-
inline unsigned long long PopUnsigned()
-
inline double PopFloat()
-
inline char PopChar()
-
template<typename MAP_T>
String &ReplaceVars(const MAP_T &var_map, String symbol = "$", const Syntax &syntax = Syntax::Full()) Find any instances of ${X} and replace with dictionary lookup of X.
-
template<typename FUN_T>
String &ReplaceMacro(String start_str, String end_str, FUN_T &&fun, const Syntax &syntax = Syntax{"\"", "()"}) Find any instance of MACRO_NAME(ARGS) and replace it with fun(ARGS).
- Todo:
Separate syntax into find_start syntax and find_end (inside macro) syntax.
- Parameters:
start_str – Initial sequence of macro to look for; for example “REPLACE(”
end_str – Sequence that ends the macro; for example “)”
macro_fun – Function to call with contents of macro. Params are macro_args (string), line_num (size_t), and hit_num (size_t)
syntax – What values should we skip as quotes or parens?
- Returns:
This string object, post processing.
-
inline size_t FindQuoteMatch(size_t pos = 0) const
-
inline size_t RFindQuoteMatch(size_t pos = npos) const
-
inline size_t Find(const CharSet &char_set, size_t start = 0, const Syntax &syntax = Syntax::None()) const
-
inline size_t Find(const char *target, size_t start = 0, const Syntax &syntax = Syntax::None()) const
-
inline size_t RFind(String target, size_t start = npos, const Syntax &syntax = Syntax::None()) const
-
inline size_t RFind(const CharSet &char_set, size_t start = npos, const Syntax &syntax = Syntax::None()) const
-
inline size_t RFind(const char *target, size_t start = npos, const Syntax &syntax = Syntax::None()) const
-
inline void FindAll(char target, vector<size_t> &results, const Syntax &syntax = Syntax::None()) const
-
inline size_t FindNonAlphanumericChar(size_t start = 0, const Syntax &syntax = Syntax::None()) const
-
inline std::string_view ViewNestedBlock(size_t start = 0, const Syntax &syntax = Syntax::Quotes()) const
-
inline std::string_view ScanView(std::function<std::string_view(size_t pos)> fun, size_t &pos) const
-
inline char ScanChar(size_t &pos) const
-
inline void Slice(vector<String> &out_set, String delim = ",", const Syntax &syntax = Syntax::Quotes(), bool trim_whitespace = false) const
Cut up a string based on the provided delimiter; fill them in to the provided vector.
- Parameters:
out_set – destination vector
delim – delimiter to split on (default: “,”)
syntax – identify quotes and parens that should be kept together.
trim_whitespace – Should whitespace around delim or assign be ignored? (default: true)
-
inline vector<String> Slice(String delim = ",", const Syntax &syntax = Syntax::Quotes(), bool trim_whitespace = false) const
Slice a String on a delimiter; return a vector of results.
Note
May be less efficient, but easier than other version of Slice()
- Parameters:
delim – delimiter to split on (default: “,”)
syntax – identify quotes and parens that should be kept together.
trim_whitespace – Should whitespace around delim or assign be ignored? (default: true)
-
inline void ViewSlices(vector<std::string_view> &out_set, String delim = ",", const Syntax &syntax = Syntax::Quotes()) const
Fill vector out_set of string_views based on the provided delimiter.
- Parameters:
out_set – destination vector
delim – delimiter to split on (default: “,”)
syntax – identify quotes and parens that should be kept together.
-
inline vector<std::string_view> ViewSlices(String delim = ",", const Syntax &syntax = Syntax::Quotes()) const
Generate vector of string_views based on the provided delimiter.
- Parameters:
delim – delimiter to split on (default: “,”)
syntax – identify quotes and parens that should be kept together.
-
inline void SliceAssign(std::map<String, String> &out_map, String delim = ",", String assign_op = "=", const Syntax &syntax = Syntax::Quotes(), bool trim_whitespace = true) const
Slice a string and treat each section as an assignment; place results in provided map.
- Parameters:
delim – delimiter to split on (default ‘,’)
assign_op – separator for left and right side of assignment (default: “=”)
syntax – identify quotes and parens that should be kept together.
trim_whitespace – Should whitespace around delim or assign be ignored? (default: true)
-
inline std::map<String, String> SliceAssign(String delim = ",", String assign_op = "=", const Syntax &syntax = Syntax::Quotes(), bool trim_whitespace = true) const
Slice a string and treat each section as an assignment; fill out a map and return it.
- Parameters:
delim – delimiter to split on (default ‘,’)
assign_op – separator for left and right side of assignment (default: “=”)
syntax – identify quotes and parens that should be kept together.
trim_whitespace – Should whitespace around delim or assign be ignored? (default: true)
-
template<typename CONTAINER_T>
inline String &AppendEnglishList(const CONTAINER_T &container)
-
template<typename CONTAINER_T>
inline String &SetEnglishList(const CONTAINER_T &container)
-
template<typename ...ARG_Ts>
inline String &AppendFormatted(const std::string &format, ARG_Ts... args)
-
inline String &AppendTrimFront(const String &in, const CharSet &chars = WhitespaceCharSet())
-
inline String &SetTrimFront(const String &in, const CharSet &chars = WhitespaceCharSet())
-
inline String &TrimFront(const CharSet &chars = WhitespaceCharSet())
-
inline String AsTrimFront(const CharSet &chars = WhitespaceCharSet()) const
-
inline String &AppendTrimBack(const String &in, const CharSet &chars = WhitespaceCharSet())
-
inline String &SetTrimBack(const String &in, const CharSet &chars = WhitespaceCharSet())
-
inline String &TrimBack(const CharSet &chars = WhitespaceCharSet())
-
inline String AsTrimBack(const CharSet &chars = WhitespaceCharSet()) const
-
inline String &AppendTrimmed(const String &in, const CharSet &chars = WhitespaceCharSet())
-
inline String &SetTrimmed(const String &in, const CharSet &chars = WhitespaceCharSet())
-
inline String &Trim(const CharSet &chars = WhitespaceCharSet())
-
inline String AsTrimmed(const CharSet &chars = WhitespaceCharSet()) const
-
inline String &AppendCompressed(const String &in, const CharSet &chars = WhitespaceCharSet(), char compress_to = ' ', bool trim_start = true, bool trim_end = true)
-
inline String &SetCompressed(const String &in, const CharSet &chars = WhitespaceCharSet(), char compress_to = ' ', bool trim_start = true, bool trim_end = true)
-
inline String &Compress(const CharSet &chars = WhitespaceCharSet(), char compress_to = ' ', bool trim_start = true, bool trim_end = true)
-
inline String AsCompressed(const CharSet &chars = WhitespaceCharSet(), char compress_to = ' ', bool trim_start = true, bool trim_end = true) const
Public Static Functions
Private Types
-
using Syntax = StringSyntax
Private Functions
-
inline void _AssertPos([[maybe_unused]] size_t pos) const
-
inline auto _Iterator(size_t pos) const
-
String() = default