|
using | ghassanpl::string_ops::wide_char16_t = std::conditional_t< sizeof(wchar_t)==sizeof(char16_t), wchar_t, 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 = std::conditional_t< sizeof(wchar_t)==sizeof(char32_t), wchar_t, 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 = std::conditional_t< same_size_and_alignment< T, char8_t >, char8_t, std::conditional_t< same_size_and_alignment< T, char16_t >, char16_t, std::conditional_t< same_size_and_alignment< T, char32_t >, char32_t, void > > > |
| The utf char type corresponding to the charable type.
|
|
template<charable T> |
using | ghassanpl::string_ops::charable_char_t = std::conditional_t< same_size_and_alignment< T, char >, char, std::conditional_t< same_size_and_alignment< T, wide_char16_t >, wide_char16_t, std::conditional_t< same_size_and_alignment< T, wide_char32_t >, wide_char32_t, void > > > |
| The native char type corresponding to the charable type.
|
|
template<charable T> |
using | ghassanpl::string_ops::best_stringable_type = std::conditional_t< stringable_base_type< T >, T, charable_char_t< T > > |
|
|
template<typename COUT , typename CIN >
requires charable<COUT> && charable<CIN> && same_size_and_alignment<COUT, 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.
|
|
bool | ghassanpl::string_ops::contains (std::string_view str, char c) |
| A pre-C++23 version of str.contains(c)
|
|
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] .
|
|
template<std::ranges::random_access_range T>
requires stringable_base_type<std::ranges::range_value_t<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> |
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> |
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 >
requires std::is_arithmetic_v<T>&& std::is_invocable_r_v<T, FUNC, std::string_view> |
std::vector< RESULT_TYPE > | ghassanpl::string_ops::word_wrap (std::string_view _source, T max_width, FUNC width_getter) |
| 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 >
requires std::is_arithmetic_v<T> |
std::vector< RESULT_TYPE > | ghassanpl::string_ops::word_wrap (std::string_view _source, T max_width, T letter_width) |
| Word-wrapping function for constant-width characters.
|
|
size_t | ghassanpl::string_ops::levenshtein_distance (std::string_view s1, std::string_view s2) |
|
template<typename CALLBACK >
requires std::invocable<CALLBACK, size_t, std::string_view, std::string&> |
std::string | ghassanpl::string_ops::callback_format (std::string_view fmt, CALLBACK &&callback) |
|
|
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>
requires charable<std::iter_value_t<IT>> |
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>
requires charable<std::iter_value_t<IT>> |
constexpr auto | ghassanpl::string_ops::make_sv (IT start, IT2 end) noexcept(noexcept(std::to_address(start))) |
|
template<typename T >
requires stringable_base_type<std::remove_cvref_t<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>
requires charable<std::ranges::range_value_t<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) |
|
|
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 >
requires std::is_invocable_r_v<bool, FUNC, char> |
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 |
|
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 >
requires std::is_invocable_r_v<bool, FUNC, char> |
constexpr void | ghassanpl::string_ops::trim_while (std::string_view &str, FUNC &&func) noexcept |
|
|
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 >
requires std::is_invocable_r_v<bool, PRED, char> |
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 >
requires std::is_invocable_r_v<bool, FUNC, char> |
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 >
requires std::is_invocable_r_v<bool, FUNC, char> |
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 , inclusive.
|
|
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 >
requires std::is_invocable_r_v<bool, FUNC, char> |
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 >
requires std::is_invocable_r_v<bool, CALLBACK, std::string_view&> |
bool | ghassanpl::string_ops::consume_delimited_list_non_empty (std::string_view &str, std::string_view delimiter, CALLBACK callback) |
| Consumes a list of delimiter -delimited strings, calling callback(str) each time; whitespaces before and after items are trimmed.
|
|
template<typename CALLBACK >
requires std::is_invocable_r_v<bool, CALLBACK, std::string_view> |
bool | ghassanpl::string_ops::consume_delimited_list (std::string_view &str, std::string_view delimiter, std::string_view closer, CALLBACK callback) |
| Consumes a list of delimiter -delimited strings, ended with closer , calling callback(str) each time; whitespaces before and after items are trimmed.
|
|
|
Functions that split strings into multiple parts, each delimited with some sort of delimiter.
|
template<typename FUNC >
requires std::is_invocable_v<FUNC, std::string_view, bool> |
constexpr void | ghassanpl::string_ops::split (std::string_view source, char delim, FUNC &&func) noexcept(noexcept(func(std::string_view{}, true))) |
| Performs a basic "split" operation, calling func for each part of source delimited by delim .
|
|
template<typename FUNC >
requires std::is_invocable_v<FUNC, std::string_view, bool> |
constexpr void | ghassanpl::string_ops::split (std::string_view source, std::string_view delim, FUNC &&func) noexcept(noexcept(func(std::string_view{}, true))) |
| Performs a basic "split" operation, calling func for each part of source delimited by delim .
|
|
template<typename FUNC >
requires std::is_invocable_v<FUNC, std::string_view, bool> |
constexpr void | ghassanpl::string_ops::split_on_any (std::string_view source, std::string_view delim, FUNC &&func) noexcept(noexcept(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 >
requires std::is_invocable_v<FUNC, std::string_view, bool>&& std::is_invocable_r_v<size_t, DELIM_FUNC, std::string_view> |
void | ghassanpl::string_ops::split_on (std::string_view source, DELIM_FUNC &&delim, FUNC &&func) noexcept(noexcept(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 >
requires std::is_invocable_v<FUNC, std::string_view, bool> |
void | ghassanpl::string_ops::natural_split (std::string_view source, char delim, FUNC &&func) noexcept |
| 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 >
requires std::is_invocable_r_v<size_t, DELIM_FUNC, std::string_view> |
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.
|
|
|
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<std::ranges::range 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<std::ranges::range 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<std::ranges::range... 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<std::ranges::range 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<std::ranges::range 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<std::ranges::range 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.
|
|
|
- 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 >
requires std::is_invocable_v<ESCAPE_FUNC, std::string_view>&& std::is_constructible_v<std::string_view, std::invoke_result_t<ESCAPE_FUNC, std::string_view>> |
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) |
|
Adds a few utility functions that deal with strings and string_views.