header_utils
Loading...
Searching...
No Matches
String Operations

Adds a few utility functions that deal with strings and string_views. More...

Topics

 ASCII
 Functions that operate on codepoints and strings encoded as ASCII.
 Unicode
 Functions and types that operate on Unicode codepoints and strings.

Concepts

concept  ghassanpl::string_ops::string_or_char
 The type is a stringable or a character.
concept  ghassanpl::string_ops::stringable
 The type is "stringable", that is, a continuous range of characters.
concept  ghassanpl::string_ops::string8
 The type is a string with an 8-bit char type.
concept  ghassanpl::string_ops::stringable8
 The type is convertible to a string view with an 8-bit char type.
concept  ghassanpl::string_ops::string_view8
 The type is a string view with an 8-bit char type.
concept  ghassanpl::string_ops::string16
 The type is a string with a 16-bit char type.
concept  ghassanpl::string_ops::stringable16
 The type is convertible to a string view with a 16-bit char type.
concept  ghassanpl::string_ops::string_view16
 The type is a string view with a 16-bit char type.
concept  ghassanpl::string_ops::string32
 The type is a string with an 32-bit char type.
concept  ghassanpl::string_ops::stringable32
 The type is convertible to a string view with a 32-bit char type.
concept  ghassanpl::string_ops::string_view32
 The type is a string view with a 32-bit char type.
concept  ghassanpl::string_ops::charable
 Can a type be bit-cast to a native/utf char type?
concept  ghassanpl::string_ops::char_type
 Whether the type is a native char type.
concept  ghassanpl::string_ops::utf_type
 Whether the type is a utf char type.
concept  ghassanpl::string_ops::stringable_base_type
 Whether the type is a native or utf char type.

Classes

struct  ghassanpl::string_ops::split_range< SINGLE >
 A very basic "range" (not really a C++ range yet) that can be iterated over as if its a range of elements in source split by split_on. More...

Macros

#define GHPL_FORMAT_TEMPLATE   typename... GHPL_ARGS
#define GHPL_FORMAT_ARGS   std::format_string<GHPL_ARGS...> ghpl_fmt, GHPL_ARGS&&... ghpl_args
#define GHPL_FORMAT_FORWARD   ghpl_fmt, ghpl_args...
#define GHPL_FORMAT_CALL   std::format(ghpl_fmt, std::forward<GHPL_ARGS>(ghpl_args)...)
#define GHPL_PRINT_CALL   std::print_unicode(ghpl_fmt, std::forward<GHPL_ARGS>(ghpl_args)...)

Typedefs

using ghassanpl::string_ops::wide_char16_t
 The default 16-bit char type for the current platform (wchar_t if it is 16-bit, char16_t otherwise).
using ghassanpl::string_ops::wide_char32_t
 The default 32-bit char type for the current platform (wchar_t if it is 32-bit, char32_t otherwise).
template<charable T>
using ghassanpl::string_ops::charable_utf_t
 The utf char type corresponding to the charable type.
template<charable T>
using ghassanpl::string_ops::charable_char_t
 The native char type corresponding to the charable type.
template<charable T>
using ghassanpl::string_ops::best_stringable_type

Functions

template<typename COUT, typename CIN>
constexpr std::basic_string_view< COUT > ghassanpl::string_ops::string_view_cast (std::basic_string_view< CIN > id) noexcept
 Casts a string_view to a string_view with a different char type via a simple reinterpret_cast.
constexpr std::string_view ghassanpl::string_ops::back (std::string_view child_to_back_up, std::string_view parent, size_t n=1) noexcept
 Creates a string_view with its beginning moved back by n characters, limited to a parent range.
constexpr std::string_view ghassanpl::string_ops::back (std::string_view child_to_back_up, size_t n=1) noexcept
 Creates a string_view with its beginning moved back by n characters.
constexpr bool ghassanpl::string_ops::is_inside (std::string_view big_string, std::string_view smaller_string)
 Checks if smaller_string is a true subset of big_string (true subset meaning they view over overlapping memory subregions).
constexpr bool ghassanpl::string_ops::isascii (char32_t cp) noexcept
 Returns true if cp is an ascii codepoint.
constexpr bool ghassanpl::string_ops::is_ascii (char32_t cp) noexcept
 Returns true if cp is an ascii codepoint.
constexpr bool ghassanpl::string_ops::string_contains (std::string_view str, char c)
 A pre-C++23 version of str.contains(c).
constexpr auto ghassanpl::string_ops::data_end (std::string_view str) noexcept
std::string_view ghassanpl::string_ops::substr (std::string_view str, intptr_t start, size_t count=std::string::npos) noexcept
 Gets a substring of str starting at start and containing count characters.
std::string_view ghassanpl::string_ops::prefix (std::string_view str, size_t count) noexcept
 Returns a substring containing the count leftmost characters of str. Always valid, clamped to the bounds of str (or empty).
std::string_view ghassanpl::string_ops::without_suffix (std::string_view str, size_t count) noexcept
 Returns a substring created by removing count characters from the end. Always valid, clamped to the bounds of str (or empty).
std::string_view ghassanpl::string_ops::suffix (std::string_view str, size_t count) noexcept
 Returns a substring containing the count rightmost characters of str. Always valid, clamped to the bounds of str (or empty).
std::string_view ghassanpl::string_ops::without_prefix (std::string_view str, size_t count) noexcept
 Returns a substring created by removing count characters from the start. Always valid, clamped to the bounds of str (or empty).
void ghassanpl::string_ops::erase_outside_n (std::string &str, size_t start, size_t count) noexcept
 Erases all characters in str outside of the range [start, start + count]. Always safe.
void ghassanpl::string_ops::erase_outside_from_to (std::string &str, size_t from, size_t to) noexcept
 Erases all characters in str outside of the range [from, to].
void ghassanpl::string_ops::remove_suffix (std::string &str, size_t prefix_size) noexcept
 Removes the last prefix_size characters from str.
void ghassanpl::string_ops::remove_prefix (std::string &str, size_t prefix_size) noexcept
 Removes the first prefix_size characters from str.
template<std::ranges::random_access_range T>
constexpr bool ghassanpl::string_ops::isany (char32_t cp, T &&chars) noexcept
 Checks if cp is any of the characters in chars.
constexpr bool ghassanpl::string_ops::isany (char32_t c, char32_t c2) noexcept
 A isany overload that takes a single character.
template<string_or_char NEEDLE, typename FUNC>
void ghassanpl::string_ops::find_all (std::string_view subject, NEEDLE &&search, FUNC &&func)
template<typename RESULT_TYPE = std::string_view, string_or_char NEEDLE>
std::vector< RESULT_TYPE > ghassanpl::string_ops::find_all (std::string_view subject, NEEDLE &&search)
std::string ghassanpl::string_ops::url_encode (std::string_view text)
 Returns a url-encoded version of the string.
std::string ghassanpl::string_ops::url_unencode (std::string_view text)
 Returns a url-decoded version of the string.
template<std::integral T>
constexpr auto ghassanpl::string_ops::from_chars (std::string_view str, T &value, const int base=10) noexcept
 A version of std::from_chars that takes a std::string_view as the first argument.
template<std::floating_point T>
constexpr auto ghassanpl::string_ops::from_chars (std::string_view str, T &value, const std::chars_format chars_format=std::chars_format::general) noexcept
 A version of std::from_chars that takes a std::string_view as the first argument.
template<typename RESULT_TYPE = std::string_view, typename T, typename FUNC>
std::vector< RESULT_TYPE > ghassanpl::string_ops::word_wrap (std::string_view _source, T max_width, FUNC width_getter, std::string_view split_chars=" ")
 Performs a basic word-wrapping split of _source, as if it was constrained to max_width.
template<typename RESULT_TYPE = std::string_view, typename T>
std::vector< RESULT_TYPE > ghassanpl::string_ops::word_wrap (std::string_view _source, T max_width, T letter_width)
 TODO: wildcard_match.
auto ghassanpl::string_ops::repeat_string (stringable auto str, size_t num)
size_t ghassanpl::string_ops::levenshtein_distance (std::string_view s1, std::string_view s2)
template<std::integral T = std::make_unsigned_t<size_t>>
auto ghassanpl::string_ops::to_number (std::string_view str, int base=10) -> expected< T, std::from_chars_result >
 If str contains an integral number (and nothing else), returns that number, else an error-carrying from_chars_result.
template<std::floating_point T = std::float_t>
auto ghassanpl::string_ops::to_number (std::string_view str, std::chars_format format=std::chars_format::general) -> expected< T, std::from_chars_result >
 If str contains an floating-point number (and nothing else), returns that number, else an error-carrying from_chars_result.
template<typename CALLBACK_TYPE>
std::string ghassanpl::string_ops::callback_format (std::string_view fmt, CALLBACK_TYPE &&callback)

Make Functions

Functions that create string_view and string types from various values

Rationale
Even though C++20 has a range constructor, it uses operator- on its arguments, which means that iterators from disparate string_views do not work, even if they point to a contiguous string range (for example, were made from substrings of the same string_view). Hence these functions. They should work with any pair of iterators (no support for sentinels yet, unfortunately), support nulls, and are almost as strong as the string_view range constructor in terms of exception and type safety. As with the respective constructors, undefined behavior when start > end.
template<typename C = char>
constexpr std::basic_string_view< C > ghassanpl::string_ops::make_sv (std::nullptr_t, std::nullptr_t) noexcept
template<stringable_base_type CT, std::contiguous_iterator IT, std::contiguous_iterator IT2>
constexpr auto ghassanpl::string_ops::make_sv (IT start, IT2 end) noexcept(noexcept(std::to_address(start)))
template<std::contiguous_iterator IT, std::contiguous_iterator IT2>
constexpr auto ghassanpl::string_ops::make_sv (IT start, IT2 end) noexcept(noexcept(std::to_address(start)))
template<typename T>
constexpr auto ghassanpl::string_ops::make_sv (T &&single_char) noexcept
template<charable T>
constexpr auto ghassanpl::string_ops::make_sv (const T *str) noexcept
template<typename C>
constexpr std::basic_string_view< C > ghassanpl::string_ops::make_sv (std::basic_string_view< C > id) noexcept
template<typename C>
constexpr std::basic_string_view< C > ghassanpl::string_ops::make_sv (std::basic_string< C > const &id) noexcept
template<typename C>
constexpr std::basic_string_view< C > ghassanpl::string_ops::make_sv (std::basic_string< C > &&id) noexcept=delete
template<std::ranges::range RANGE>
constexpr auto ghassanpl::string_ops::make_sv (RANGE &&range) noexcept
template<typename... NONARGS, typename... ARGS>
constexpr auto ghassanpl::string_ops::make_string (ARGS &&... args)

to_string Functions

Basic identity and utility to_string functions.

See also
Stringification
std::string ghassanpl::string_ops::to_string (std::string_view from) noexcept
std::string ghassanpl::string_ops::string_view_to_string (std::string_view from) noexcept
std::string ghassanpl::string_ops::to_string (std::u8string_view from) noexcept
 Version for simple function pointers.
template<typename T>
requires requires { std::to_string(t); }
std::string ghassanpl::string_ops::to_string (T const &t)
constexpr std::string const & ghassanpl::string_ops::to_string (std::same_as< std::string > auto const &s)
template<typename T>
std::string ghassanpl::string_ops::to_string (std::optional< T > const &o)

Trimming Functions

Functions that trim (remove ascii whitespace from) strings and string_views.

constexpr std::string_view ghassanpl::string_ops::trimmed_whitespace_right (std::string_view str) noexcept
constexpr std::string_view ghassanpl::string_ops::trimmed_whitespace_left (std::string_view str) noexcept
constexpr std::string_view ghassanpl::string_ops::trimmed_whitespace (std::string_view str) noexcept
constexpr std::string_view ghassanpl::string_ops::trimmed_until (std::string_view str, char chr) noexcept
constexpr std::string_view ghassanpl::string_ops::trimmed (std::string_view str, char chr) noexcept
constexpr std::string ghassanpl::string_ops::trimmed_whitespace_right (std::string str) noexcept
constexpr std::string ghassanpl::string_ops::trimmed_whitespace_left (std::string str) noexcept
constexpr std::string ghassanpl::string_ops::trimmed_whitespace (std::string str) noexcept
constexpr std::string ghassanpl::string_ops::trimmed_until (std::string str, char chr) noexcept
constexpr std::string ghassanpl::string_ops::trimmed (std::string str, char chr) noexcept
template<typename FUNC>
std::string_view ghassanpl::string_ops::trimmed_while (std::string_view str, FUNC &&func) noexcept
constexpr void ghassanpl::string_ops::trim_whitespace_right (std::string_view &str) noexcept
 TODO: trim_*(std::string&) overloads.
constexpr void ghassanpl::string_ops::trim_whitespace_left (std::string_view &str) noexcept
constexpr void ghassanpl::string_ops::trim_whitespace (std::string_view &str) noexcept
constexpr void ghassanpl::string_ops::trim_until (std::string_view &str, char chr) noexcept
constexpr void ghassanpl::string_ops::trim (std::string_view &str, char chr) noexcept
template<typename FUNC>
constexpr void ghassanpl::string_ops::trim_while (std::string_view &str, FUNC &&func) noexcept

Consume Functions

Functions that "consume" parts of a string_view (that is, remove a section from the beginning or end if the conditions apply).

Most of the functions return the consumed part, or 'true/false' if the part to be consumed is given explicitly. These functions do nothing (or the maximum safe amount) if there is nothing appropriate available to consume.

char ghassanpl::string_ops::consume (std::string_view &str)
 Consumes and returns the first character in the str, or \0 if no more characters.
bool ghassanpl::string_ops::consume (std::string_view &str, char val)
 Consumes the character val if it's at the beginning of str.
bool ghassanpl::string_ops::consume (std::string_view &str, std::string_view val)
 Consumes the string val if it's at the beginning of str.
template<typename... ARGS>
char ghassanpl::string_ops::consume_any (std::string_view &str, ARGS &&... args)
 Consumes any of the characters in 'chars' if it's the first char of str.
template<typename PRED>
char ghassanpl::string_ops::consume (std::string_view &str, PRED &&pred)
 Consumes a character from the beginning of str if it matches pred(str[0]).
char ghassanpl::string_ops::consume_or (std::string_view &str, char or_else)
 Consumes the first character from str, returning it, or or_else if string is empty.
bool ghassanpl::string_ops::consume_at_end (std::string_view &str, char val)
 Consumes the last character from str if it matches val.
bool ghassanpl::string_ops::consume_at_end (std::string_view &str, std::string_view val)
 Consumes the string val from the end str.
template<typename FUNC>
std::string_view ghassanpl::string_ops::consume_while (std::string_view &str, FUNC &&pred)
 Consumes characters from the beginning of str while they match pred(str[0]).
std::string_view ghassanpl::string_ops::consume_while (std::string_view &str, char c)
 Consumes characters from the beginning of str while they are equal to c.
template<typename... ARGS>
std::string_view ghassanpl::string_ops::consume_while_any (std::string_view &str, ARGS &&... args)
 Consumes a run of any of the characters in 'chars' at the beginning of str.
template<typename FUNC>
std::string_view ghassanpl::string_ops::consume_until (std::string_view &str, FUNC &&pred)
 Consumes characters from the beginning of str until one matches pred(str[0]), exclusive.
std::string_view ghassanpl::string_ops::consume_until (std::string_view &str, char c)
 Consumes characters from the beginning of str until one is equal to c, exclusive.
std::string_view ghassanpl::string_ops::consume_until (std::string_view &str, std::string_view end)
 Consumes characters from the beginning of str until the string starts with end, exclusive.
template<typename... ARGS>
std::string_view ghassanpl::string_ops::consume_until_any (std::string_view &str, ARGS &&... args)
 Consumes characters from the beginning of str until one is equal to any in the parameter pack, exclusive.
std::string_view ghassanpl::string_ops::consume_until_delim (std::string_view &str, char c)
 Consumes characters from the beginning of str until one is equal to c, consuming c and including it as part of the result.
std::string_view ghassanpl::string_ops::consume_until_delim_ex (std::string_view &str, char c)
 Consumes characters from the beginning of str until one is equal to c, consuming c but not including it as part of the result.
std::string_view ghassanpl::string_ops::consume_n (std::string_view &str, size_t n)
 Consumes at most n characters from the beginning of str.
template<typename FUNC>
std::string_view ghassanpl::string_ops::consume_n (std::string_view &str, size_t n, FUNC &&pred)
 Consumes at most n characters from the beginning of str that match pred(str[0]).
template<typename CALLBACK_TYPE>
bool ghassanpl::string_ops::consume_delimited_list_non_empty (std::string_view &str, std::string_view delimiter, CALLBACK_TYPE callback)
 Consumes a list of delimiter-delimited strings, calling callback(str) each time; whitespaces before and after items are trimmed.
template<typename CALLBACK_TYPE>
bool ghassanpl::string_ops::consume_delimited_list (std::string_view &str, std::string_view delimiter, std::string_view closer, CALLBACK_TYPE callback)
 Consumes a list of delimiter-delimited strings, ended with closer, calling callback(str) each time; whitespaces before and after items are trimmed.

Split Functions

Functions that split strings into multiple parts, each delimited with some sort of delimiter.

template<typename FUNC>
constexpr auto ghassanpl::string_ops::split_invoke (FUNC &func, std::string_view sv, bool is_final) noexcept(noexcept(func(std::string_view{}, true)))
template<typename FUNC>
constexpr auto ghassanpl::string_ops::split (std::string_view source, char delim, FUNC &&func) noexcept(noexcept(split_invoke(func, std::string_view{}, true)))
 Performs a basic "split" operation, calling func for each part of source delimited by delim.
template<typename FUNC>
constexpr void ghassanpl::string_ops::split (std::string_view source, std::string_view delim, FUNC &&func) noexcept(noexcept(split_invoke(func, std::string_view{}, true)))
 Performs a basic "split" operation, calling func for each part of source delimited by delim.
template<typename FUNC>
constexpr void ghassanpl::string_ops::split_on_any (std::string_view source, std::string_view delim, FUNC &&func) noexcept(noexcept(split_invoke(func, std::string_view{}, true)))
 Performs a basic "split" operation, calling func for each part of source delimited by any character in delim.
template<typename DELIM_FUNC, typename FUNC>
void ghassanpl::string_ops::split_on (std::string_view source, DELIM_FUNC &&delim, FUNC &&func) noexcept(noexcept(split_invoke(func, std::string_view{}, true)) &&noexcept(delim(std::string_view{})))
 Performs a basic "split" operation, calling func for each part of source delimited by the delim function.
constexpr std::pair< std::string_view, std::string_view > ghassanpl::string_ops::split_at (std::string_view src, size_t split_at) noexcept
 Does not include the character at split_at in the returned strings.
constexpr bool ghassanpl::string_ops::split_at (std::string_view src, size_t split_at, std::string_view &first, std::string_view &second) noexcept
 Does not include the character at split_at in the returned strings.
constexpr std::pair< std::string_view, std::string_view > ghassanpl::string_ops::single_split (std::string_view src, char delim) noexcept
 Splits src once on the first instance of delim.
constexpr std::pair< std::string_view, std::string_view > ghassanpl::string_ops::single_split_last (std::string_view src, char delim) noexcept
 Splits src once on the last instance of delim.
constexpr bool ghassanpl::string_ops::single_split (std::string_view src, char delim, std::string_view &first, std::string_view &second) noexcept
 Splits src once on the first instance of delim.
constexpr bool ghassanpl::string_ops::single_split_last (std::string_view src, char delim, std::string_view &first, std::string_view &second) noexcept
 Splits src once on the last instance of delim.
template<typename FUNC>
void ghassanpl::string_ops::natural_split (std::string_view source, char delim, FUNC &&func) noexcept(noexcept(split_invoke(func, source, delim)))
 Performs a more natural split of the string, that is: ignoring multiple delimiters in a row, and empty items.
template<typename RESULT_TYPE = std::string_view, string_or_char DELIM>
constexpr std::vector< RESULT_TYPE > ghassanpl::string_ops::split (std::string_view source, DELIM &&delim) noexcept
 Performs a basic "split" operation, returning a std::vector of the split parts.
template<typename RESULT_TYPE = std::string_view>
constexpr std::vector< RESULT_TYPE > ghassanpl::string_ops::split_on_any (std::string_view source, std::string_view delim) noexcept
 Performs a basic "split" operation, returning a std::vector of the split parts.
template<typename RESULT_TYPE = std::string_view, typename DELIM_FUNC>
std::vector< RESULT_TYPE > ghassanpl::string_ops::split_on (std::string_view source, DELIM_FUNC &&delim) noexcept(noexcept(delim(std::string_view{})))
 Performs a basic "split" operation, returning a std::vector of the split parts.
template<typename RESULT_TYPE = std::string_view, string_or_char DELIM>
std::vector< RESULT_TYPE > ghassanpl::string_ops::natural_split (std::string_view source, DELIM &&delim) noexcept
 Performs a more natural split of the string, that is: ignoring multiple delimiters in a row, and empty items; returns a std::vector of the split parts.

Join Functions

Functions that join a range of formattable elements into a single string

Note
Formatting is done using stream operators (operator<<).
Todo
Use Stringification instead
template<typename T>
auto ghassanpl::string_ops::join (T &&source)
 Returns a string that is created by joining together string representation of the elements in the source range.
template<typename T, string_or_char DELIM>
auto ghassanpl::string_ops::join (T &&source, DELIM const &delim)
 Returns a string that is created by joining together string representation of the elements in the source range, separated by delim; delim is only added between elements.
template<typename... RANGES, string_or_char DELIM>
auto ghassanpl::string_ops::join_multiple (DELIM const &delim, RANGES &&... sources)
 Returns a string that is created by joining together string representation of the elements in the sources ranges, separated by delim; delim is only added between elements.
template<typename T, string_or_char DELIM, string_or_char LAST_DELIM>
auto ghassanpl::string_ops::join_and (T &&source, DELIM const &delim, LAST_DELIM &&last_delim)
 Returns a string that is created by joining together string representation of the elements in the source range, separated by delim; delim is only added between elements; the last element is delimited by last_delim instead of delim.
template<typename T, string_or_char DELIM, string_or_char LAST_DELIM, typename FUNC>
auto ghassanpl::string_ops::join_and (T &&source, DELIM const &delim, LAST_DELIM &&last_delim, FUNC &&transform_func)
 Same as join(T&& source, DELIM const& delim, LAST_DELIM&& last_delim) except each element is transformed by transform_func before being stringified and added to the result.
template<typename T, typename FUNC, string_or_char DELIM>
auto ghassanpl::string_ops::join (T &&source, DELIM const &delim, FUNC &&transform_func)
 Same as join(T&& source, DELIM const& delim) except each element is transformed by transform_func before being stringified and added to the result.

Replace and Escape Functions

Warning
A lot of these functions have stupid and/or subtle bugs, or are not intuitive in their behavior. Use at your own risk. Pull requests welcome.
Todo
C++23's format has a string format specifications that automatically escape, use those when they become available
template<string_or_char NEEDLE, string_or_char REPLACE>
void ghassanpl::string_ops::replace (std::string &subject, NEEDLE &&search, REPLACE &&replace)
template<string_or_char NEEDLE, string_or_char REPLACE>
std::string ghassanpl::string_ops::replaced (std::string subject, NEEDLE &&search, REPLACE &&replace)
template<string_or_char DELIMITER = char, string_or_char ESCAPE = char>
void ghassanpl::string_ops::quote (std::string &subject, DELIMITER delimiter='"', ESCAPE escape = '\\')
template<string_or_char DELIMITER = char, string_or_char ESCAPE = char>
std::string ghassanpl::string_ops::quoted (std::string &&subject, DELIMITER &&delimiter='"', ESCAPE&& escape = '\\')
template<string_or_char DELIMITER = char, string_or_char ESCAPE = char>
std::string ghassanpl::string_ops::quoted (std::string_view subject, DELIMITER &&delimiter='"', ESCAPE&& escape = '\\')
template<string_or_char DELIMITER = char, string_or_char ESCAPE = char>
std::string ghassanpl::string_ops::quoted (const char *subject, DELIMITER &&delimiter='"', ESCAPE&& escape = '\\')
template<typename ESCAPE_FUNC>
void ghassanpl::string_ops::escape (std::string &subject, std::string_view chars_to_escape, ESCAPE_FUNC &&escape_func)
template<string_or_char ESCAPE = char>
void ghassanpl::string_ops::escape (std::string &subject, std::string_view chars_to_escape, ESCAPE &&escape='\\')
template<typename ESCAPE_FUNC, typename ISPRINTABLE_FUNC = decltype(ascii::isprint)>
void ghassanpl::string_ops::escape_non_printable (std::string &subject, ESCAPE_FUNC &&escape_func, ISPRINTABLE_FUNC &&isprintable_func=ascii::isprint)
void ghassanpl::string_ops::escape_non_printable (std::string &subject)
template<typename STR, string_or_char ESCAPE = char>
std::string ghassanpl::string_ops::escaped (STR &&subject, std::string_view to_escape="\"\\", ESCAPE &&escape_str='\\')
 Lint Note: Changing the initializer of to_escape to a R-string breaks doxygen.
template<typename STR>
std::string ghassanpl::string_ops::escaped_non_printable (STR &&subject)

sto* replacements

Functions equivalent to std::stoi, std::stod, etc that take std::string_view as its first argument

auto ghassanpl::string_ops::stob (std::string_view str, size_t *idx=nullptr, int base=10)
auto ghassanpl::string_ops::stoc (std::string_view str, size_t *idx=nullptr, int base=10)
auto ghassanpl::string_ops::stouc (std::string_view str, size_t *idx=nullptr, int base=10)
auto ghassanpl::string_ops::stoi (std::string_view str, size_t *idx=nullptr, int base=10)
auto ghassanpl::string_ops::stol (std::string_view str, size_t *idx=nullptr, int base=10)
auto ghassanpl::string_ops::stoll (std::string_view str, size_t *idx=nullptr, int base=10)
auto ghassanpl::string_ops::stoul (std::string_view str, size_t *idx=nullptr, int base=10)
auto ghassanpl::string_ops::stoull (std::string_view str, size_t *idx=nullptr, int base=10)
auto ghassanpl::string_ops::stof (std::string_view str, size_t *idx=nullptr, std::chars_format format=std::chars_format::general)
auto ghassanpl::string_ops::stod (std::string_view str, size_t *idx=nullptr, std::chars_format format=std::chars_format::general)
auto ghassanpl::string_ops::stold (std::string_view str, size_t *idx=nullptr, std::chars_format format=std::chars_format::general)

Detailed Description

Adds a few utility functions that deal with strings and string_views.

Macro Definition Documentation

◆ GHPL_FORMAT_ARGS

#define GHPL_FORMAT_ARGS   std::format_string<GHPL_ARGS...> ghpl_fmt, GHPL_ARGS&&... ghpl_args

Definition at line 1925 of file string_ops.h.

◆ GHPL_FORMAT_CALL

#define GHPL_FORMAT_CALL   std::format(ghpl_fmt, std::forward<GHPL_ARGS>(ghpl_args)...)

Definition at line 1929 of file string_ops.h.

◆ GHPL_FORMAT_FORWARD

#define GHPL_FORMAT_FORWARD   ghpl_fmt, ghpl_args...

Definition at line 1927 of file string_ops.h.

◆ GHPL_FORMAT_TEMPLATE

#define GHPL_FORMAT_TEMPLATE   typename... GHPL_ARGS

Definition at line 1923 of file string_ops.h.

◆ GHPL_PRINT_CALL

#define GHPL_PRINT_CALL   std::print_unicode(ghpl_fmt, std::forward<GHPL_ARGS>(ghpl_args)...)

Definition at line 1931 of file string_ops.h.

Typedef Documentation

◆ best_stringable_type

template<charable T>
using ghassanpl::string_ops::best_stringable_type

Definition at line 127 of file string_ops.h.

◆ charable_char_t

template<charable T>
using ghassanpl::string_ops::charable_char_t

The native char type corresponding to the charable type.

Definition at line 120 of file string_ops.h.

◆ charable_utf_t

template<charable T>
using ghassanpl::string_ops::charable_utf_t

The utf char type corresponding to the charable type.

Definition at line 112 of file string_ops.h.

◆ wide_char16_t

The default 16-bit char type for the current platform (wchar_t if it is 16-bit, char16_t otherwise).

Definition at line 80 of file string_ops.h.

◆ wide_char32_t

The default 32-bit char type for the current platform (wchar_t if it is 32-bit, char32_t otherwise).

Definition at line 83 of file string_ops.h.

Function Documentation

◆ back() [1/2]

std::string_view ghassanpl::string_ops::back ( std::string_view child_to_back_up,
size_t n = 1 )
nodiscardconstexprnoexcept

Creates a string_view with its beginning moved back by n characters.

Example
parent = "HelloWorld";
child = parent.substr(5);
n = 1;
back(child, n) => "oWorld";
constexpr std::string_view back(std::string_view child_to_back_up, std::string_view parent, size_t n=1) noexcept
Creates a string_view with its beginning moved back by n characters, limited to a parent range.
Definition string_ops.h:244
Note
This is, of course, unsafe to do if the new beginning is beyond the underlying string's range
Parameters
child_to_back_upthe input string_view
nhow many characters to back up

Definition at line 263 of file string_ops.h.

◆ back() [2/2]

std::string_view ghassanpl::string_ops::back ( std::string_view child_to_back_up,
std::string_view parent,
size_t n = 1 )
nodiscardconstexprnoexcept

Creates a string_view with its beginning moved back by n characters, limited to a parent range.

Example
parent = "HelloWorld";
child = parent.substr(5);
n = 1;
back(child, parent, n) => "oWorld";
Parameters
child_to_back_upthe input string_view
parenta string_view that child_to_back_up is a subview of
nhow many characters to back up

Definition at line 244 of file string_ops.h.

◆ callback_format()

template<typename CALLBACK_TYPE>
std::string ghassanpl::string_ops::callback_format ( std::string_view fmt,
CALLBACK_TYPE && callback )

Definition at line 1881 of file string_ops.h.

◆ consume() [1/4]

char ghassanpl::string_ops::consume ( std::string_view & str)
inlinenodiscard

Consumes and returns the first character in the str, or \0 if no more characters.

Definition at line 702 of file string_ops.h.

◆ consume() [2/4]

bool ghassanpl::string_ops::consume ( std::string_view & str,
char val )
inlinenodiscard

Consumes the character val if it's at the beginning of str.

Returns
whether it actually consumed

Definition at line 713 of file string_ops.h.

◆ consume() [3/4]

template<typename PRED>
char ghassanpl::string_ops::consume ( std::string_view & str,
PRED && pred )
inlinenodiscard

Consumes a character from the beginning of str if it matches pred(str[0]).

Returns
the matched character, or \0 if no match

Definition at line 753 of file string_ops.h.

◆ consume() [4/4]

bool ghassanpl::string_ops::consume ( std::string_view & str,
std::string_view val )
inlinenodiscard

Consumes the string val if it's at the beginning of str.

Returns
whether it actually consumed

Definition at line 725 of file string_ops.h.

◆ consume_any()

template<typename... ARGS>
char ghassanpl::string_ops::consume_any ( std::string_view & str,
ARGS &&... args )
inlinenodiscard

Consumes any of the characters in 'chars' if it's the first char of str.

Returns
the consumed character, or \0 if none found

Definition at line 738 of file string_ops.h.

◆ consume_at_end() [1/2]

bool ghassanpl::string_ops::consume_at_end ( std::string_view & str,
char val )
inlinenodiscard

Consumes the last character from str if it matches val.

Returns
whether it consumed
See also
consume(std::string_view&, char)

Definition at line 777 of file string_ops.h.

◆ consume_at_end() [2/2]

bool ghassanpl::string_ops::consume_at_end ( std::string_view & str,
std::string_view val )
inlinenodiscard

Consumes the string val from the end str.

Returns
whether it consumed
See also
consume(std::string_view&, char)

Definition at line 791 of file string_ops.h.

◆ consume_delimited_list()

template<typename CALLBACK_TYPE>
bool ghassanpl::string_ops::consume_delimited_list ( std::string_view & str,
std::string_view delimiter,
std::string_view closer,
CALLBACK_TYPE callback )
inlinenodiscard

Consumes a list of delimiter-delimited strings, ended with closer, calling callback(str) each time; whitespaces before and after items are trimmed.

Stops when closer is consumed.

Attention
callback will be called with the ENTIRE string left after consuming the delimiter, so you have to consume the list elements yourself!
Example
consume_delimited_list("alpha, beta, gamma, omega ), blah, bleh", ",", ")" [](auto& sv) { println("'{}'", consume_while(sv, ascii::isalpha)); return true; });
>> will print
'alpha'
'beta'
'gamma'
'omega'
bool consume_delimited_list(std::string_view &str, std::string_view delimiter, std::string_view closer, CALLBACK_TYPE callback)
Consumes a list of delimiter-delimited strings, ended with closer, calling callback(str) each time; w...
Definition string_ops.h:986
std::string_view consume_while(std::string_view &str, FUNC &&pred)
Consumes characters from the beginning of str while they match pred(str[0]).
Definition string_ops.h:805
Parameters
callbackmust be invocable as callback(string_view&) -> bool; if the callback returns false, consumption is stopped
Returns
true if closer is consumed; false if callback returned false at any point or if end-of-string is encountered before closer is consumed

Definition at line 986 of file string_ops.h.

◆ consume_delimited_list_non_empty()

template<typename CALLBACK_TYPE>
bool ghassanpl::string_ops::consume_delimited_list_non_empty ( std::string_view & str,
std::string_view delimiter,
CALLBACK_TYPE callback )
inlinenodiscard

Consumes a list of delimiter-delimited strings, calling callback(str) each time; whitespaces before and after items are trimmed.

Attention
callback will be called with the ENTIRE string left after consuming the delimiter, so you have to consume the list elements yourself!
Example
consume_delimited_list_non_empty("alpha, beta, gamma, omega", ",", [](auto& sv) { println("'{}'", consume_until(sv, ',')); return true; });
>> will print
'alpha'
'beta'
'gamma'
'omega'
std::string_view consume_until(std::string_view &str, FUNC &&pred)
Consumes characters from the beginning of str until one matches pred(str[0]), exclusive.
Definition string_ops.h:838
bool consume_delimited_list_non_empty(std::string_view &str, std::string_view delimiter, CALLBACK_TYPE callback)
Consumes a list of delimiter-delimited strings, calling callback(str) each time; whitespaces before a...
Definition string_ops.h:957
Parameters
callbackmust be invocable as callback(string_view&) -> bool; if the callback returns false, consumption is stopped; otherwise, this function must consume its item from the given string view
Returns
true if consumption ended, either by making the string empty, or encountering a non-delimiter after callback; false if callback returned false at any point

Definition at line 957 of file string_ops.h.

◆ consume_n() [1/2]

std::string_view ghassanpl::string_ops::consume_n ( std::string_view & str,
size_t n )
inlinenodiscard

Consumes at most n characters from the beginning of str.

Returns
the consumed prefix as a string_view

Definition at line 919 of file string_ops.h.

◆ consume_n() [2/2]

template<typename FUNC>
std::string_view ghassanpl::string_ops::consume_n ( std::string_view & str,
size_t n,
FUNC && pred )
inlinenodiscard

Consumes at most n characters from the beginning of str that match pred(str[0]).

Returns
the consumed prefix as a string_view

Definition at line 931 of file string_ops.h.

◆ consume_or()

char ghassanpl::string_ops::consume_or ( std::string_view & str,
char or_else )
inlinenodiscard

Consumes the first character from str, returning it, or or_else if string is empty.

Definition at line 765 of file string_ops.h.

◆ consume_until() [1/3]

std::string_view ghassanpl::string_ops::consume_until ( std::string_view & str,
char c )
inlinenodiscard

Consumes characters from the beginning of str until one is equal to c, exclusive.

Returns
the consumed prefix as a string_view

Definition at line 848 of file string_ops.h.

◆ consume_until() [2/3]

template<typename FUNC>
std::string_view ghassanpl::string_ops::consume_until ( std::string_view & str,
FUNC && pred )
inlinenodiscard

Consumes characters from the beginning of str until one matches pred(str[0]), exclusive.

Returns
the consumed prefix as a string_view

Definition at line 838 of file string_ops.h.

◆ consume_until() [3/3]

std::string_view ghassanpl::string_ops::consume_until ( std::string_view & str,
std::string_view end )
inlinenodiscard

Consumes characters from the beginning of str until the string starts with end, exclusive.

Returns
the consumed prefix as a string_view

Definition at line 858 of file string_ops.h.

◆ consume_until_any()

template<typename... ARGS>
std::string_view ghassanpl::string_ops::consume_until_any ( std::string_view & str,
ARGS &&... args )
inlinenodiscard

Consumes characters from the beginning of str until one is equal to any in the parameter pack, exclusive.

Returns
the consumed prefix as a string_view

Definition at line 884 of file string_ops.h.

◆ consume_until_delim()

std::string_view ghassanpl::string_ops::consume_until_delim ( std::string_view & str,
char c )
inlinenodiscard

Consumes characters from the beginning of str until one is equal to c, consuming c and including it as part of the result.

Returns
the consumed prefix as a string_view

Definition at line 894 of file string_ops.h.

◆ consume_until_delim_ex()

std::string_view ghassanpl::string_ops::consume_until_delim_ex ( std::string_view & str,
char c )
inlinenodiscard

Consumes characters from the beginning of str until one is equal to c, consuming c but not including it as part of the result.

Returns
the consumed prefix as a string_view

Definition at line 907 of file string_ops.h.

◆ consume_while() [1/2]

std::string_view ghassanpl::string_ops::consume_while ( std::string_view & str,
char c )
inlinenodiscard

Consumes characters from the beginning of str while they are equal to c.

Returns
the consumed prefix as a string_view

Definition at line 815 of file string_ops.h.

◆ consume_while() [2/2]

template<typename FUNC>
std::string_view ghassanpl::string_ops::consume_while ( std::string_view & str,
FUNC && pred )
inlinenodiscard

Consumes characters from the beginning of str while they match pred(str[0]).

Returns
the consumed prefix as a string_view

Definition at line 805 of file string_ops.h.

◆ consume_while_any()

template<typename... ARGS>
std::string_view ghassanpl::string_ops::consume_while_any ( std::string_view & str,
ARGS &&... args )
inlinenodiscard

Consumes a run of any of the characters in 'chars' at the beginning of str.

Returns
the consumed character, or \0 if none found

Definition at line 826 of file string_ops.h.

◆ data_end()

auto ghassanpl::string_ops::data_end ( std::string_view str)
nodiscardconstexprnoexcept

Definition at line 588 of file string_ops.h.

◆ erase_outside_from_to()

void ghassanpl::string_ops::erase_outside_from_to ( std::string & str,
size_t from,
size_t to )
inlinenoexcept

Erases all characters in str outside of the range [from, to].

Always safe. If from > to, acts as if calling erase_outside_from_to(str, to, from)

Definition at line 632 of file string_ops.h.

◆ erase_outside_n()

void ghassanpl::string_ops::erase_outside_n ( std::string & str,
size_t start,
size_t count )
inlinenoexcept

Erases all characters in str outside of the range [start, start + count]. Always safe.

Definition at line 625 of file string_ops.h.

◆ escape() [1/2]

template<string_or_char ESCAPE = char>
void ghassanpl::string_ops::escape ( std::string & subject,
std::string_view chars_to_escape,
ESCAPE && escape = '\\' )
inline

Definition at line 1509 of file string_ops.h.

◆ escape() [2/2]

template<typename ESCAPE_FUNC>
void ghassanpl::string_ops::escape ( std::string & subject,
std::string_view chars_to_escape,
ESCAPE_FUNC && escape_func )
inline

Definition at line 1478 of file string_ops.h.

◆ escape_non_printable() [1/2]

void ghassanpl::string_ops::escape_non_printable ( std::string & subject)
inline

Definition at line 1543 of file string_ops.h.

◆ escape_non_printable() [2/2]

template<typename ESCAPE_FUNC, typename ISPRINTABLE_FUNC = decltype(ascii::isprint)>
void ghassanpl::string_ops::escape_non_printable ( std::string & subject,
ESCAPE_FUNC && escape_func,
ISPRINTABLE_FUNC && isprintable_func = ascii::isprint )
inline

Definition at line 1525 of file string_ops.h.

◆ escaped()

template<typename STR, string_or_char ESCAPE = char>
std::string ghassanpl::string_ops::escaped ( STR && subject,
std::string_view to_escape = "\"\\",
ESCAPE && escape_str = '\\' )
inlinenodiscard

Lint Note: Changing the initializer of to_escape to a R-string breaks doxygen.

Definition at line 1551 of file string_ops.h.

◆ escaped_non_printable()

template<typename STR>
std::string ghassanpl::string_ops::escaped_non_printable ( STR && subject)
inlinenodiscard

Definition at line 1559 of file string_ops.h.

◆ find_all() [1/2]

template<typename RESULT_TYPE = std::string_view, string_or_char NEEDLE>
std::vector< RESULT_TYPE > ghassanpl::string_ops::find_all ( std::string_view subject,
NEEDLE && search )
inline

Definition at line 1398 of file string_ops.h.

◆ find_all() [2/2]

template<string_or_char NEEDLE, typename FUNC>
void ghassanpl::string_ops::find_all ( std::string_view subject,
NEEDLE && search,
FUNC && func )
inline

Definition at line 1382 of file string_ops.h.

◆ from_chars() [1/2]

template<std::integral T>
auto ghassanpl::string_ops::from_chars ( std::string_view str,
T & value,
const int base = 10 )
nodiscardconstexprnoexcept

A version of std::from_chars that takes a std::string_view as the first argument.

Definition at line 1631 of file string_ops.h.

◆ from_chars() [2/2]

template<std::floating_point T>
auto ghassanpl::string_ops::from_chars ( std::string_view str,
T & value,
const std::chars_format chars_format = std::chars_format::general )
nodiscardconstexprnoexcept

A version of std::from_chars that takes a std::string_view as the first argument.

Definition at line 1636 of file string_ops.h.

◆ is_ascii()

bool ghassanpl::string_ops::is_ascii ( char32_t cp)
nodiscardconstexprnoexcept

Returns true if cp is an ascii codepoint.

Definition at line 576 of file string_ops.h.

◆ is_inside()

bool ghassanpl::string_ops::is_inside ( std::string_view big_string,
std::string_view smaller_string )
nodiscardconstexpr

Checks if smaller_string is a true subset of big_string (true subset meaning they view over overlapping memory subregions).

Definition at line 269 of file string_ops.h.

◆ isany() [1/2]

bool ghassanpl::string_ops::isany ( char32_t c,
char32_t c2 )
constexprnoexcept

A isany overload that takes a single character.

Definition at line 693 of file string_ops.h.

◆ isany() [2/2]

template<std::ranges::random_access_range T>
bool ghassanpl::string_ops::isany ( char32_t cp,
T && chars )
nodiscardconstexprnoexcept

Checks if cp is any of the characters in chars.

Definition at line 688 of file string_ops.h.

◆ isascii()

bool ghassanpl::string_ops::isascii ( char32_t cp)
nodiscardconstexprnoexcept

Returns true if cp is an ascii codepoint.

Definition at line 572 of file string_ops.h.

◆ join() [1/3]

template<typename T>
auto ghassanpl::string_ops::join ( T && source)
inlinenodiscard

Returns a string that is created by joining together string representation of the elements in the source range.

Definition at line 1263 of file string_ops.h.

◆ join() [2/3]

template<typename T, string_or_char DELIM>
auto ghassanpl::string_ops::join ( T && source,
DELIM const & delim )
inlinenodiscard

Returns a string that is created by joining together string representation of the elements in the source range, separated by delim; delim is only added between elements.

Definition at line 1273 of file string_ops.h.

◆ join() [3/3]

template<typename T, typename FUNC, string_or_char DELIM>
auto ghassanpl::string_ops::join ( T && source,
DELIM const & delim,
FUNC && transform_func )
inlinenodiscard

Same as join(T&& source, DELIM const& delim) except each element is transformed by transform_func before being stringified and added to the result.

Parameters
transform_funcmust be invocable as transform_fun(el) for each element in the source range

Definition at line 1366 of file string_ops.h.

◆ join_and() [1/2]

template<typename T, string_or_char DELIM, string_or_char LAST_DELIM>
auto ghassanpl::string_ops::join_and ( T && source,
DELIM const & delim,
LAST_DELIM && last_delim )
inlinenodiscard

Returns a string that is created by joining together string representation of the elements in the source range, separated by delim; delim is only added between elements; the last element is delimited by last_delim instead of delim.

Definition at line 1307 of file string_ops.h.

◆ join_and() [2/2]

template<typename T, string_or_char DELIM, string_or_char LAST_DELIM, typename FUNC>
auto ghassanpl::string_ops::join_and ( T && source,
DELIM const & delim,
LAST_DELIM && last_delim,
FUNC && transform_func )
inlinenodiscard

Same as join(T&& source, DELIM const& delim, LAST_DELIM&& last_delim) except each element is transformed by transform_func before being stringified and added to the result.

Parameters
transform_funcmust be invocable as transform_fun(el) for each element in the source range

Definition at line 1337 of file string_ops.h.

◆ join_multiple()

template<typename... RANGES, string_or_char DELIM>
auto ghassanpl::string_ops::join_multiple ( DELIM const & delim,
RANGES &&... sources )
inlinenodiscard

Returns a string that is created by joining together string representation of the elements in the sources ranges, separated by delim; delim is only added between elements.

Definition at line 1288 of file string_ops.h.

◆ levenshtein_distance()

size_t ghassanpl::string_ops::levenshtein_distance ( std::string_view s1,
std::string_view s2 )
inline

Definition at line 1782 of file string_ops.h.

◆ make_string()

template<typename... NONARGS, typename... ARGS>
auto ghassanpl::string_ops::make_string ( ARGS &&... args)
nodiscardconstexpr

Definition at line 190 of file string_ops.h.

◆ make_sv() [1/8]

template<charable T>
auto ghassanpl::string_ops::make_sv ( const T * str)
nodiscardconstexprnoexcept

Definition at line 170 of file string_ops.h.

◆ make_sv() [2/8]

template<std::contiguous_iterator IT, std::contiguous_iterator IT2>
auto ghassanpl::string_ops::make_sv ( IT start,
IT2 end )
nodiscardconstexprnoexcept

Definition at line 157 of file string_ops.h.

◆ make_sv() [3/8]

template<stringable_base_type CT, std::contiguous_iterator IT, std::contiguous_iterator IT2>
auto ghassanpl::string_ops::make_sv ( IT start,
IT2 end )
nodiscardconstexprnoexcept

Definition at line 148 of file string_ops.h.

◆ make_sv() [4/8]

template<std::ranges::range RANGE>
auto ghassanpl::string_ops::make_sv ( RANGE && range)
nodiscardconstexprnoexcept

Definition at line 184 of file string_ops.h.

◆ make_sv() [5/8]

template<typename C>
std::basic_string_view< C > ghassanpl::string_ops::make_sv ( std::basic_string< C > const & id)
nodiscardconstexprnoexcept

Definition at line 178 of file string_ops.h.

◆ make_sv() [6/8]

template<typename C>
std::basic_string_view< C > ghassanpl::string_ops::make_sv ( std::basic_string_view< C > id)
nodiscardconstexprnoexcept

Definition at line 176 of file string_ops.h.

◆ make_sv() [7/8]

template<typename C = char>
std::basic_string_view< C > ghassanpl::string_ops::make_sv ( std::nullptr_t ,
std::nullptr_t  )
nodiscardconstexprnoexcept

Definition at line 144 of file string_ops.h.

◆ make_sv() [8/8]

template<typename T>
auto ghassanpl::string_ops::make_sv ( T && single_char)
nodiscardconstexprnoexcept

Definition at line 164 of file string_ops.h.

◆ natural_split() [1/2]

template<typename FUNC>
void ghassanpl::string_ops::natural_split ( std::string_view source,
char delim,
FUNC && func )
inlinenoexcept

Performs a more natural split of the string, that is: ignoring multiple delimiters in a row, and empty items.

See also
split(std::string_view source, char delim, FUNC&& func)
Example
split("these are matched words ", ' ', [](auto sv, bool){ println("'{}'", sv); });
>> will print
'these'
'are'
'matched'
'words'
constexpr auto split(std::string_view source, char delim, FUNC &&func) noexcept(noexcept(split_invoke(func, std::string_view{}, true)))
Performs a basic "split" operation, calling func for each part of source delimited by delim.

Definition at line 1181 of file string_ops.h.

◆ natural_split() [2/2]

template<typename RESULT_TYPE = std::string_view, string_or_char DELIM>
std::vector< RESULT_TYPE > ghassanpl::string_ops::natural_split ( std::string_view source,
DELIM && delim )
inlinenodiscardnoexcept

Performs a more natural split of the string, that is: ignoring multiple delimiters in a row, and empty items; returns a std::vector of the split parts.

See also
natural_split(std::string_view source, char delim, FUNC&& func)

Definition at line 1244 of file string_ops.h.

◆ prefix()

std::string_view ghassanpl::string_ops::prefix ( std::string_view str,
size_t count )
inlinenodiscardnoexcept

Returns a substring containing the count leftmost characters of str. Always valid, clamped to the bounds of str (or empty).

Definition at line 605 of file string_ops.h.

◆ quote()

template<string_or_char DELIMITER = char, string_or_char ESCAPE = char>
void ghassanpl::string_ops::quote ( std::string & subject,
DELIMITER delimiter = '"',
ESCAPE escape = '\\' )
inline

Definition at line 1439 of file string_ops.h.

◆ quoted() [1/3]

template<string_or_char DELIMITER = char, string_or_char ESCAPE = char>
std::string ghassanpl::string_ops::quoted ( const char * subject,
DELIMITER && delimiter = '"',
ESCAPE && escape = '\\' )
inlinenodiscard

Definition at line 1471 of file string_ops.h.

◆ quoted() [2/3]

template<string_or_char DELIMITER = char, string_or_char ESCAPE = char>
std::string ghassanpl::string_ops::quoted ( std::string && subject,
DELIMITER && delimiter = '"',
ESCAPE && escape = '\\' )
inlinenodiscard

Definition at line 1456 of file string_ops.h.

◆ quoted() [3/3]

template<string_or_char DELIMITER = char, string_or_char ESCAPE = char>
std::string ghassanpl::string_ops::quoted ( std::string_view subject,
DELIMITER && delimiter = '"',
ESCAPE && escape = '\\' )
inlinenodiscard

Definition at line 1463 of file string_ops.h.

◆ remove_prefix()

void ghassanpl::string_ops::remove_prefix ( std::string & str,
size_t prefix_size )
inlinenoexcept

Removes the first prefix_size characters from str.

Always safe. TODO: Returning version?

Definition at line 647 of file string_ops.h.

◆ remove_suffix()

void ghassanpl::string_ops::remove_suffix ( std::string & str,
size_t prefix_size )
inlinenoexcept

Removes the last prefix_size characters from str.

Always safe. TODO: Returning version?

Definition at line 640 of file string_ops.h.

◆ repeat_string()

auto ghassanpl::string_ops::repeat_string ( stringable auto str,
size_t num )

Definition at line 1772 of file string_ops.h.

◆ replace()

template<string_or_char NEEDLE, string_or_char REPLACE>
void ghassanpl::string_ops::replace ( std::string & subject,
NEEDLE && search,
REPLACE && replace )
inline

Definition at line 1412 of file string_ops.h.

◆ replaced()

template<string_or_char NEEDLE, string_or_char REPLACE>
std::string ghassanpl::string_ops::replaced ( std::string subject,
NEEDLE && search,
REPLACE && replace )
inlinenodiscard

Definition at line 1432 of file string_ops.h.

◆ single_split() [1/2]

std::pair< std::string_view, std::string_view > ghassanpl::string_ops::single_split ( std::string_view src,
char delim )
nodiscardconstexprnoexcept

Splits src once on the first instance of delim.

Returns
a pair of string_views: the left and right parts of src split the first instance of delim; if no delim is found, returns { src, {} }

Definition at line 1139 of file string_ops.h.

◆ single_split() [2/2]

bool ghassanpl::string_ops::single_split ( std::string_view src,
char delim,
std::string_view & first,
std::string_view & second )
nodiscardconstexprnoexcept

Splits src once on the first instance of delim.

Returns
whether delim was found
Parameters
firstwill be filled with the left part of the string, if delim is found
secondwill be filled with the right part of the string, if delim is found

Definition at line 1155 of file string_ops.h.

◆ single_split_last() [1/2]

std::pair< std::string_view, std::string_view > ghassanpl::string_ops::single_split_last ( std::string_view src,
char delim )
nodiscardconstexprnoexcept

Splits src once on the last instance of delim.

Returns
a pair of string_views: the left and right parts of src split the first instance of delim; if no delim is found, returns { src, {} }

Definition at line 1146 of file string_ops.h.

◆ single_split_last() [2/2]

bool ghassanpl::string_ops::single_split_last ( std::string_view src,
char delim,
std::string_view & first,
std::string_view & second )
nodiscardconstexprnoexcept

Splits src once on the last instance of delim.

Returns
whether delim was found
Parameters
firstwill be filled with the left part of the string, if delim is found
secondwill be filled with the right part of the string, if delim is found

Definition at line 1164 of file string_ops.h.

◆ split() [1/3]

template<typename FUNC>
auto ghassanpl::string_ops::split ( std::string_view source,
char delim,
FUNC && func )
constexprnoexcept

Performs a basic "split" operation, calling func for each part of source delimited by delim.

If no delimiters are found, calls func for entire string.

Parameters
funcmust be invocable as func(string_view, bool); the second argument specifies if the given string is the final part of the split

Definition at line 1029 of file string_ops.h.

◆ split() [2/3]

template<typename RESULT_TYPE = std::string_view, string_or_char DELIM>
std::vector< RESULT_TYPE > ghassanpl::string_ops::split ( std::string_view source,
DELIM && delim )
nodiscardconstexprnoexcept

Performs a basic "split" operation, returning a std::vector of the split parts.

See also
split(std::string_view source, char delim, FUNC&& func)
Template Parameters
RESULT_TYPEthe type of the elements in the resulting vector; must be constructible from string_view

Definition at line 1203 of file string_ops.h.

◆ split() [3/3]

template<typename FUNC>
void ghassanpl::string_ops::split ( std::string_view source,
std::string_view delim,
FUNC && func )
constexprnoexcept

Performs a basic "split" operation, calling func for each part of source delimited by delim.

If no delimiters are found, calls func for entire string.

Parameters
funcmust be invocable as func(string_view, bool); the second argument specifies if the given string is the final part of the split

Definition at line 1053 of file string_ops.h.

◆ split_at() [1/2]

std::pair< std::string_view, std::string_view > ghassanpl::string_ops::split_at ( std::string_view src,
size_t split_at )
nodiscardconstexprnoexcept

Does not include the character at split_at in the returned strings.

Definition at line 1120 of file string_ops.h.

◆ split_at() [2/2]

bool ghassanpl::string_ops::split_at ( std::string_view src,
size_t split_at,
std::string_view & first,
std::string_view & second )
nodiscardconstexprnoexcept

Does not include the character at split_at in the returned strings.

Definition at line 1128 of file string_ops.h.

◆ split_invoke()

template<typename FUNC>
auto ghassanpl::string_ops::split_invoke ( FUNC & func,
std::string_view sv,
bool is_final )
constexprnoexcept

Definition at line 1014 of file string_ops.h.

◆ split_on() [1/2]

template<typename RESULT_TYPE = std::string_view, typename DELIM_FUNC>
std::vector< RESULT_TYPE > ghassanpl::string_ops::split_on ( std::string_view source,
DELIM_FUNC && delim )
nodiscardnoexcept

Performs a basic "split" operation, returning a std::vector of the split parts.

See also
split_on(std::string_view source, DELIM_FUNC&& delim, FUNC&& func)
Template Parameters
RESULT_TYPEthe type of the elements in the resulting vector; must be constructible from string_view

Definition at line 1232 of file string_ops.h.

◆ split_on() [2/2]

template<typename DELIM_FUNC, typename FUNC>
void ghassanpl::string_ops::split_on ( std::string_view source,
DELIM_FUNC && delim,
FUNC && func )
inlinenoexcept

Performs a basic "split" operation, calling func for each part of source delimited by the delim function.

If no delimiters are found, calls func for entire string.

Example
split_on("alpha,beta;gamma", [](auto sv) { return sv.find_first_of(",;"); }), [](auto sv, bool) { println("'{}', sv); });
>> will print
'alpha'
'beta'
'gamma'
void split_on(std::string_view source, DELIM_FUNC &&delim, FUNC &&func) noexcept(noexcept(split_invoke(func, std::string_view{}, true)) &&noexcept(delim(std::string_view{})))
Performs a basic "split" operation, calling func for each part of source delimited by the delim funct...
Parameters
delimmust be invocable as delim(string_view) -> std::string::size_type; it should return the position in its parameter where the next delimiter is; if it returns std::string::npos, splitting ends there, calling func for the rest of the string
funcmust be invocable as func(string_view, bool); the second argument specifies if the given string is the final part of the split

Definition at line 1101 of file string_ops.h.

◆ split_on_any() [1/2]

template<typename RESULT_TYPE = std::string_view>
std::vector< RESULT_TYPE > ghassanpl::string_ops::split_on_any ( std::string_view source,
std::string_view delim )
nodiscardconstexprnoexcept

Performs a basic "split" operation, returning a std::vector of the split parts.

See also
split_on_any(std::string_view source, std::string_view delim, FUNC&& func)
Template Parameters
RESULT_TYPEthe type of the elements in the resulting vector; must be constructible from string_view

Definition at line 1216 of file string_ops.h.

◆ split_on_any() [2/2]

template<typename FUNC>
void ghassanpl::string_ops::split_on_any ( std::string_view source,
std::string_view delim,
FUNC && func )
constexprnoexcept

Performs a basic "split" operation, calling func for each part of source delimited by any character in delim.

If no delimiters are found, calls func for entire string.

Parameters
funcmust be invocable as func(string_view, bool); the second argument specifies if the given string is the final part of the split

Definition at line 1071 of file string_ops.h.

◆ stob()

auto ghassanpl::string_ops::stob ( std::string_view str,
size_t * idx = nullptr,
int base = 10 )
inlinenodiscard

Definition at line 1843 of file string_ops.h.

◆ stoc()

auto ghassanpl::string_ops::stoc ( std::string_view str,
size_t * idx = nullptr,
int base = 10 )
inlinenodiscard

Definition at line 1844 of file string_ops.h.

◆ stod()

auto ghassanpl::string_ops::stod ( std::string_view str,
size_t * idx = nullptr,
std::chars_format format = std::chars_format::general )
inlinenodiscard

Definition at line 1853 of file string_ops.h.

◆ stof()

auto ghassanpl::string_ops::stof ( std::string_view str,
size_t * idx = nullptr,
std::chars_format format = std::chars_format::general )
inlinenodiscard

Definition at line 1852 of file string_ops.h.

◆ stoi()

auto ghassanpl::string_ops::stoi ( std::string_view str,
size_t * idx = nullptr,
int base = 10 )
inlinenodiscard

Definition at line 1847 of file string_ops.h.

◆ stol()

auto ghassanpl::string_ops::stol ( std::string_view str,
size_t * idx = nullptr,
int base = 10 )
inlinenodiscard

Definition at line 1848 of file string_ops.h.

◆ stold()

auto ghassanpl::string_ops::stold ( std::string_view str,
size_t * idx = nullptr,
std::chars_format format = std::chars_format::general )
inlinenodiscard

Definition at line 1854 of file string_ops.h.

◆ stoll()

auto ghassanpl::string_ops::stoll ( std::string_view str,
size_t * idx = nullptr,
int base = 10 )
inlinenodiscard

Definition at line 1849 of file string_ops.h.

◆ stouc()

auto ghassanpl::string_ops::stouc ( std::string_view str,
size_t * idx = nullptr,
int base = 10 )
inlinenodiscard

Definition at line 1845 of file string_ops.h.

◆ stoul()

auto ghassanpl::string_ops::stoul ( std::string_view str,
size_t * idx = nullptr,
int base = 10 )
inlinenodiscard

Definition at line 1850 of file string_ops.h.

◆ stoull()

auto ghassanpl::string_ops::stoull ( std::string_view str,
size_t * idx = nullptr,
int base = 10 )
inlinenodiscard

Definition at line 1851 of file string_ops.h.

◆ string_contains()

bool ghassanpl::string_ops::string_contains ( std::string_view str,
char c )
nodiscardconstexpr

A pre-C++23 version of str.contains(c).

Definition at line 579 of file string_ops.h.

◆ string_view_cast()

template<typename COUT, typename CIN>
std::basic_string_view< COUT > ghassanpl::string_ops::string_view_cast ( std::basic_string_view< CIN > id)
nodiscardconstexprnoexcept

Casts a string_view to a string_view with a different char type via a simple reinterpret_cast.

Definition at line 207 of file string_ops.h.

◆ string_view_to_string()

std::string ghassanpl::string_ops::string_view_to_string ( std::string_view from)
inlinenodiscardnoexcept

Definition at line 218 of file string_ops.h.

◆ substr()

std::string_view ghassanpl::string_ops::substr ( std::string_view str,
intptr_t start,
size_t count = std::string::npos )
inlinenodiscardnoexcept

Gets a substring of str starting at start and containing count characters.

The result will always be valid, clamped to the bounds of str (or empty).

Parameters
countthe (maximum) number of characters to get
startif negative, starts -start characters before the end

Definition at line 593 of file string_ops.h.

◆ suffix()

std::string_view ghassanpl::string_ops::suffix ( std::string_view str,
size_t count )
inlinenodiscardnoexcept

Returns a substring containing the count rightmost characters of str. Always valid, clamped to the bounds of str (or empty).

Definition at line 615 of file string_ops.h.

◆ to_number() [1/2]

template<std::integral T = std::make_unsigned_t<size_t>>
auto ghassanpl::string_ops::to_number ( std::string_view str,
int base = 10 ) -> expected< T, std::from_chars_result >
inlinenodiscard

If str contains an integral number (and nothing else), returns that number, else an error-carrying from_chars_result.

Definition at line 1859 of file string_ops.h.

◆ to_number() [2/2]

template<std::floating_point T = std::float_t>
auto ghassanpl::string_ops::to_number ( std::string_view str,
std::chars_format format = std::chars_format::general ) -> expected< T, std::from_chars_result >
inlinenodiscard

If str contains an floating-point number (and nothing else), returns that number, else an error-carrying from_chars_result.

Definition at line 1870 of file string_ops.h.

◆ to_string() [1/5]

template<typename T>
std::string ghassanpl::string_ops::to_string ( std::optional< T > const & o)
inlinenodiscard

Definition at line 228 of file string_ops.h.

◆ to_string() [2/5]

std::string const & ghassanpl::string_ops::to_string ( std::same_as< std::string > auto const & s)
nodiscardconstexpr

Definition at line 225 of file string_ops.h.

◆ to_string() [3/5]

std::string ghassanpl::string_ops::to_string ( std::string_view from)
inlinenodiscardnoexcept

Definition at line 217 of file string_ops.h.

◆ to_string() [4/5]

std::string ghassanpl::string_ops::to_string ( std::u8string_view from)
inlinenodiscardnoexcept

Version for simple function pointers.

Definition at line 220 of file string_ops.h.

◆ to_string() [5/5]

template<typename T>
requires requires { std::to_string(t); }
std::string ghassanpl::string_ops::to_string ( T const & t)
inlinenodiscard

Definition at line 223 of file string_ops.h.

◆ trim()

void ghassanpl::string_ops::trim ( std::string_view & str,
char chr )
constexprnoexcept

Definition at line 678 of file string_ops.h.

◆ trim_until()

void ghassanpl::string_ops::trim_until ( std::string_view & str,
char chr )
constexprnoexcept

Definition at line 677 of file string_ops.h.

◆ trim_while()

template<typename FUNC>
void ghassanpl::string_ops::trim_while ( std::string_view & str,
FUNC && func )
constexprnoexcept

Definition at line 681 of file string_ops.h.

◆ trim_whitespace()

void ghassanpl::string_ops::trim_whitespace ( std::string_view & str)
constexprnoexcept

Definition at line 676 of file string_ops.h.

◆ trim_whitespace_left()

void ghassanpl::string_ops::trim_whitespace_left ( std::string_view & str)
constexprnoexcept

Definition at line 675 of file string_ops.h.

◆ trim_whitespace_right()

void ghassanpl::string_ops::trim_whitespace_right ( std::string_view & str)
constexprnoexcept

TODO: trim_*(std::string&) overloads.

Definition at line 674 of file string_ops.h.

◆ trimmed() [1/2]

std::string ghassanpl::string_ops::trimmed ( std::string str,
char chr )
nodiscardconstexprnoexcept

Definition at line 666 of file string_ops.h.

◆ trimmed() [2/2]

std::string_view ghassanpl::string_ops::trimmed ( std::string_view str,
char chr )
nodiscardconstexprnoexcept

Definition at line 660 of file string_ops.h.

◆ trimmed_until() [1/2]

std::string ghassanpl::string_ops::trimmed_until ( std::string str,
char chr )
nodiscardconstexprnoexcept

Definition at line 665 of file string_ops.h.

◆ trimmed_until() [2/2]

std::string_view ghassanpl::string_ops::trimmed_until ( std::string_view str,
char chr )
nodiscardconstexprnoexcept

Definition at line 659 of file string_ops.h.

◆ trimmed_while()

template<typename FUNC>
std::string_view ghassanpl::string_ops::trimmed_while ( std::string_view str,
FUNC && func )
inlinenodiscardnoexcept

Definition at line 670 of file string_ops.h.

◆ trimmed_whitespace() [1/2]

std::string ghassanpl::string_ops::trimmed_whitespace ( std::string str)
nodiscardconstexprnoexcept

Definition at line 664 of file string_ops.h.

◆ trimmed_whitespace() [2/2]

std::string_view ghassanpl::string_ops::trimmed_whitespace ( std::string_view str)
nodiscardconstexprnoexcept

Definition at line 658 of file string_ops.h.

◆ trimmed_whitespace_left() [1/2]

std::string ghassanpl::string_ops::trimmed_whitespace_left ( std::string str)
nodiscardconstexprnoexcept

Definition at line 663 of file string_ops.h.

◆ trimmed_whitespace_left() [2/2]

std::string_view ghassanpl::string_ops::trimmed_whitespace_left ( std::string_view str)
nodiscardconstexprnoexcept

Definition at line 657 of file string_ops.h.

◆ trimmed_whitespace_right() [1/2]

std::string ghassanpl::string_ops::trimmed_whitespace_right ( std::string str)
nodiscardconstexprnoexcept

Definition at line 662 of file string_ops.h.

◆ trimmed_whitespace_right() [2/2]

std::string_view ghassanpl::string_ops::trimmed_whitespace_right ( std::string_view str)
nodiscardconstexprnoexcept

Definition at line 656 of file string_ops.h.

◆ url_encode()

std::string ghassanpl::string_ops::url_encode ( std::string_view text)
inlinenodiscard

Returns a url-encoded version of the string.

Definition at line 1569 of file string_ops.h.

◆ url_unencode()

std::string ghassanpl::string_ops::url_unencode ( std::string_view text)
inlinenodiscard

Returns a url-decoded version of the string.

Definition at line 1587 of file string_ops.h.

◆ without_prefix()

std::string_view ghassanpl::string_ops::without_prefix ( std::string_view str,
size_t count )
inlinenodiscardnoexcept

Returns a substring created by removing count characters from the start. Always valid, clamped to the bounds of str (or empty).

Definition at line 620 of file string_ops.h.

◆ without_suffix()

std::string_view ghassanpl::string_ops::without_suffix ( std::string_view str,
size_t count )
inlinenodiscardnoexcept

Returns a substring created by removing count characters from the end. Always valid, clamped to the bounds of str (or empty).

Definition at line 610 of file string_ops.h.

◆ word_wrap() [1/2]

template<typename RESULT_TYPE = std::string_view, typename T, typename FUNC>
std::vector< RESULT_TYPE > ghassanpl::string_ops::word_wrap ( std::string_view _source,
T max_width,
FUNC width_getter,
std::string_view split_chars = " " )

Performs a basic word-wrapping split of _source, as if it was constrained to max_width.

Splits will be performed implicitly on any of the ASCII characters in split_chars (or character boundaries if no other choice), and explictly on '\n' characters.

Template Parameters
Tthe type for the width values
Parameters
width_gettermust be invocable as width_getter(string_view) -> T and should return the width of the given string calculating it however it deems appropriate
Returns
a vector containing a string_view for each line
Todo
example

Definition at line 1728 of file string_ops.h.

◆ word_wrap() [2/2]

template<typename RESULT_TYPE = std::string_view, typename T>
std::vector< RESULT_TYPE > ghassanpl::string_ops::word_wrap ( std::string_view _source,
T max_width,
T letter_width )

TODO: wildcard_match.

Word-wrapping function for constant-width characters.

See also
word_wrap(std::string_view _source, T max_width, FUNC width_getter)

Definition at line 1767 of file string_ops.h.