Description of String Expressions. More...
Concepts | |
| concept | simstr::StrExpr |
| Concept of "String Expressions". | |
| concept | simstr::StrExprForType |
| The concept of a string expression of a given character type. | |
Classes | |
| struct | simstr::expr_replaced< K > |
| A string expression that generates a string replacing all occurrences of the given substring. More... | |
| struct | simstr::expr_replace_symbols< K, UseVectorForReplace > |
| A type for a string expression that generates a string in which the given characters are replaced by the given strings. More... | |
| struct | simstr::strexprjoin< A, B > |
Template class for concatenating two string expressions into one using operator + More... | |
| struct | simstr::strexprjoin_c< A, B, last > |
| Concatenation of a reference to a string expression and the value of the string expression. More... | |
| struct | simstr::empty_expr< K > |
| An "empty" string expression. More... | |
| struct | simstr::expr_spaces< K, N, S > |
| A type of string expression that returns N specified characters. More... | |
| struct | simstr::expr_pad< K > |
| A type of string expression that returns N specified characters. More... | |
| struct | simstr::expr_choice< A, B > |
| Conditional selection string expression. More... | |
| struct | simstr::expr_if< A > |
| Conditional selection string expression. More... | |
| struct | simstr::expr_choice_one_lit< A, N, Compare > |
| Conditional selection string expression. More... | |
| struct | simstr::expr_choice_two_lit< K, N, M > |
| Conditional selection string expression. More... | |
| struct | simstr::expr_stdstr< K, T > |
| A type for using std::string and std::string_view as sources in string expressions. More... | |
Functions | |
| template<StrExpr A, FromIntNumber T> | |
| constexpr auto | simstr::operator+ (const A &a, T s) |
| Concatenation operator for string expression and integer. | |
| template<StrExpr A, FromIntNumber T> | |
| constexpr auto | simstr::operator+ (T s, const A &a) |
| Concatenation operator for integer and string expression. | |
| template<typename K, typename T> | |
| constexpr auto | simstr::e_num (T t) |
| Convert an integer to a string expression. | |
| template<StrExpr A, typename R> requires (std::is_same_v<R, double> || std::is_same_v<R, float>) | |
| constexpr auto | simstr::operator+ (const A &a, R s) |
Concatenation operator for string expression and real number (float, double). | |
| template<StrExpr A, typename R> requires (is_one_of_std_char_v<typename A::symb_type> && (std::is_same_v<R, double> || std::is_same_v<R, float>)) | |
| constexpr auto | simstr::operator+ (R s, const A &a) |
Concatenation operator for float (float, double) and string expression. | |
| template<typename K> requires (is_one_of_std_char_v<K>) | |
| constexpr auto | simstr::e_real (double t) |
Convert a double number to a string expression. | |
| template<bool tail = false, bool skip_empty = false, typename L, typename K = typename const_lit<L>::symb_type, size_t I = const_lit<L>::Count, typename T> | |
| constexpr auto | simstr::e_join (const T &s, L &&d) |
| Get a string expression concatenating the strings in the container into a single string with the given delimiter.limiter.limiter. | |
| template<typename K, typename T, size_t N = const_lit_for<K, T>::Count, typename X, size_t L = const_lit_for<K, X>::Count> requires (N > 1) | |
| constexpr auto | simstr::e_repl (simple_str< K > w, T &&p, X &&r) |
| Get a string expression that generates a string with all occurrences of a given substring replaced. | |
| template<bool UseVector = false, typename K, typename ... Repl> requires (sizeof...(Repl) % 2 == 0) | |
| auto | simstr::e_repl_const_symbols (simple_str< K > src, Repl &&... other) |
| Returns a string expression that generates a string containing the given characters replaced with given substrings. | |
| template<StrExpr A, StrExprForType< typename A::symb_type > B> | |
| auto | simstr::operator+ (const A &a, const B &b) |
| An addition operator for two arbitrary string expressions of the same character type. | |
| template<typename K, StrExprForType< K > A> | |
| constexpr auto | simstr::operator+ (const A &a, K s) |
| Addition operator of a string expression and one character. | |
| template<typename K> | |
| constexpr auto | simstr::e_char (K s) |
| Generates a string of 1 given character. | |
| template<typename T, size_t N = const_lit<T>::Count> | |
| constexpr auto | simstr::e_t (T &&s) |
| Converts a string literal to a string expression. | |
| template<StrExpr A, typename K = typename A::symb_type, typename T, size_t N = const_lit_for<K, T>::Count> | |
| constexpr auto | simstr::operator+ (const A &a, T &&s) |
| The addition operator for a string expression and a string literal of the same character type. | |
| template<StrExpr A, typename K = typename A::symb_type, typename T, size_t N = const_lit_for<K, T>::Count> | |
| constexpr auto | simstr::operator+ (T &&s, const A &a) |
| The addition operator for a string literal of the same character type and string expression. | |
| template<size_t N> | |
| constexpr auto | simstr::e_spca () |
| Generates a string of N char spaces. | |
| template<size_t N> | |
| constexpr auto | simstr::e_spcw () |
| Generates a string of N wchar_t spaces. | |
| template<typename K> | |
| constexpr auto | simstr::e_c (size_t l, K s) |
| Generates a string of l characters s of type K. | |
| template<StrExpr A, StrExprForType< typename A::symb_type > B> | |
| constexpr auto | simstr::e_choice (bool c, const A &a, const B &b) |
| Create a conditional string expression expr_choice. | |
| template<StrExpr A, typename T, size_t N = const_lit_for<typename A::symb_type, T>::Count> | |
| constexpr auto | simstr::e_choice (bool c, const A &a, T &&str) |
| Overload e_choice when the third argument is a string literal. | |
| template<StrExpr A, typename T, size_t N = const_lit_for<typename A::symb_type, T>::Count> | |
| constexpr auto | simstr::e_choice (bool c, T &&str, const A &a) |
| Overload e_choice when the second argument is a string literal. | |
| template<typename T, typename L, size_t N = const_lit<T>::Count, size_t M = const_lit_for<typename const_lit<T>::symb_type, L>::Count> | |
| constexpr auto | simstr::e_choice (bool c, T &&str_a, L &&str_b) |
| Overload e_choice when the second and third arguments are string literals. | |
| template<StrExpr A> | |
| constexpr auto | simstr::e_if (bool c, const A &a) |
| Creating a conditional string expression expr_if. | |
| template<typename T, size_t N = const_lit<T>::Count> | |
| constexpr auto | simstr::e_if (bool c, T &&str) |
| Overload e_if when the second argument is a string literal. | |
| template<StrExprForType< u8s > A> | |
| auto | simstr::operator+ (const A &a, const std::string &s) |
| Addition operator for char string expression and std::string. | |
| template<StrExprForType< u8s > A> | |
| auto | simstr::operator+ (const std::string &s, const A &a) |
| Addition operator for std::string and char string expression. | |
| template<StrExprForType< u8s > A> | |
| auto | simstr::operator+ (const A &a, const std::string_view &s) |
| Addition operator for char string expression and std::string_view. | |
| template<StrExprForType< u8s > A> | |
| auto | simstr::operator+ (const std::string_view &s, const A &a) |
| Addition operator for std::string_view and char string expression. | |
| template<StrExprForType< uws > A> | |
| auto | simstr::operator+ (const A &a, const std::wstring &s) |
| Addition operator for wchar_t string expression and std::wstring. | |
| template<StrExprForType< uws > A> | |
| auto | simstr::operator+ (const std::wstring &s, const A &a) |
| Addition operator for std::wstring and wchar_t string expression. | |
| template<StrExprForType< uws > A> | |
| auto | simstr::operator+ (const A &a, const std::wstring_view &s) |
| Addition operator for wchar_t string expression and std::wstring_view. | |
| template<StrExprForType< uws > A> | |
| auto | simstr::operator+ (const std::wstring_view &s, const A &a) |
| Addition operator for std::wstring_view and wchar_t string expression. | |
Variables | |
| constexpr empty_expr< u8s > | simstr::eea {} |
| Empty string expression of type char. | |
| constexpr empty_expr< uws > | simstr::eew {} |
| Empty string expression of type wchar_t. | |
| constexpr empty_expr< u16s > | simstr::eeu {} |
| Empty string expression of type char16_t. | |
| constexpr empty_expr< u32s > | simstr::eeuu {} |
| Empty string expression of type char32_t. | |
Description of String Expressions.
All owning string types can be initialized using "string expressions" (essentially a variant of https://en.wikipedia.org/wiki/Expression_templates for strings). A string expression is an object of an arbitrary type that has methods:
size_t length() const: returns the length of the stringK* place(K*) const: copy the characters of the string to the intended buffer and return a pointer behind the last charactertypename symb_type: shows what type of symbols it works with.During initialization, a string object asks the string expression for its size, allocates the necessary memory, and passes the memory to a string expression that places the characters in the allocated buffer.
All library string objects are themselves string expressions that simply copy the original string. Basically, string expressions are used to concatenate or convert strings.
For all string expressions, operator + is defined, which creates a new string expression from two operands simstr::strexprjoin, which combines two string expressions, and which in the length method returns the sum of the length original operands, and in the place method - places first the first operand, then the second, into the result buffer. And since this operator itself returns a string expression, you can again apply operator + to it, forming a chain of several string expressions, and eventually “materialize” the last resulting object, which will first calculate the size of the entire shared memory for the final result, and then will place the nested subexpressions into a single buffer.
Also operator + is defined for string expressions and string literals, string expressions and numbers (numbers are converted to decimal representation), and you can also add the desired types of string expressions yourself. Example:
There are several types of string expressions out of the box to perform various operations on strings
expr_spaces< Character Type, Number of Characters, Symbol>{}: returns a string of length Number of Characters, filled with the specified character. The number of characters and character are compile-time constants. For some cases there is a shorthand notation:e_spca< Number of Characters>(): char string of spacese_spcw< Number of Characters>(): w_char string of spacesexpr_pad< Character Type>{Number of Characters, Symbol}: produces a string long Number of Characters, filled with the specified character. The number of characters and the symbol can be set at runtime. Shorthand: e_c(Number of Characters, Character)e_choice(bool Condition, StrExpr1, StrExpr2): if Condition == true, the result will be StrExpr1, otherwise StrExpr2e_num< CharacterType>(IntegerNumber): Converts a number to decimal notation. Rarely used because for string expressions and numbers the "+" operator is redefined, and the number can simply be written as text + number;e_real< CharacterType>(RealNumber): Converts a number to decimal notation. Rarely used because for string expressions and numbers the "+" operator is overridden, and the number can simply be written as text + number;e_join< bool AfterLast = false, bool SkipEmpty = false>(container, "Separator"): concatenates all strings in a container using a separator. If AfterLast == true, then the separator is added after the last element of the container, otherwise only between elements. If Skip Empty == true, then empty lines do not add a separator, otherwise for each empty line a separator is also insertede_repl(SourceString, "Search", "Replace"): replaces occurrences of "Search" with "Replace" in the source string. Find and replace patterns are compile-time string literals.expr_replaced< CharacterType>{SourceString, Search, Replace}: replaces occurrences of Search with Replace in the source string. Search and replace patterns - can be any string objects at runtime. etc. etc.
|
inlineconstexpr |
Generates a string of l characters s of type K.
| K | is a symbol. |
| l | - number of characters. |
| s | - symbol. |
|
inlineconstexpr |
Generates a string of 1 given character.
| s | - symbol. |
|
inlineconstexpr |
Create a conditional string expression expr_choice.
| A | - Type expression when the condition is true, inferred from the argument. |
| B | - The type of expression when the condition is false, inferred from the argument. |
| c | is a Boolean condition. |
| a | is a string expression that is executed when c == true. |
| b | is a string expression that is executed when c == false. |
Serves to allow you to select different options in one expression depending on the condition.
Example:
Otherwise, such operations would have to be split into several string modifications or the use of temporary strings, which is not optimal and will reduce performance. (This is checked in the "Build Full Func Name" benchmark)
|
inlineconstexpr |
Creating a conditional string expression expr_if.
| A | - Type expression when the condition is true, inferred from the argument |
| c | - boolean condition |
| a | - string expression executed when c == true |
Serves to allow one expression to generate, depending on the condition, either the specified option or an empty string.
Example
Otherwise, such operations would have to be split into several string modifications or the use of temporary strings, which is not optimal and will reduce performance. (This example is tested in the "Build Full Func Name" benchmark)
|
inlineconstexpr |
Get a string expression concatenating the strings in the container into a single string with the given delimiter.limiter.limiter.
| tail | - whether to add a separator after the last line. |
| skip_empty | - skip empty lines without adding a separator. |
| s | - container with strings, must support range for. |
| d | - delimiter, string literal. |
|
inlineconstexpr |
Convert an integer to a string expression.
| K | - character type. |
| T | - number type, inferred from the argument. |
| t | - number. |
Returns a string expression that generates the decimal representation of the given number. Can be used when you need to concatenate a number and a string literal.
|
inlineconstexpr |
Convert a double number to a string expression.
| t | - number. |
Returns a string expression that generates the decimal representation of the given number. using sprintf("%.16g"). Can be used when you need to concatenate a number and a string literal.
|
inlineconstexpr |
Get a string expression that generates a string with all occurrences of a given substring replaced.
| K | - the type of the symbol, inferred from the first argument. |
| w | - starting line. |
| p | - string literal, searched substring. |
| r | - string literal, what to replace with. |
| auto simstr::e_repl_const_symbols | ( | simple_str< K > | src, |
| Repl &&... | other ) |
Returns a string expression that generates a string containing the given characters replaced with given substrings.
| UseVector | - use a vector to save symbol search results. Described in more detail in expr_replace_symbols. |
| src | - source string. |
| symbol | - constant symbol that needs to be replaced. |
| repl | - string literal to replace the character with. |
| ... | symbol, repl - other symbols and strings. |
Used to generate character replacements for strings if all of them are known at compile time. Example:
In principle, e_repl_const_symbols is quite safe to return from a function if the source string external to function.
|
inlineconstexpr |
Generates a string of N char spaces.
| N | - Number of spaces. |
Example:
|
inlineconstexpr |
Generates a string of N wchar_t spaces.
| N | - Number of spaces. |
Example:
|
inlineconstexpr |
Converts a string literal to a string expression.
String literals are not themselves string expressions. This usually does not cause problems in concatenation operations, since the second operand is already a string expression, and addition with a literal works for it. But there are situations when the second operand is not either string expression. For example:
In this case, you can convert the literal to a string expression in two ways:
"text"_ss, which converts the literal to simple_str_nt: res = "text"_ss + intVare_t("text"), which converts the literal to expr_literal: res = e_t("text") + intVarIn the second method, the compiler can more aggressively apply optimizations related to what is known at compilation literal size.
Although strictly speaking, in these situations you can use other methods:
result = eea + "text" + intVar, result = "text" + eea + intVarresult = "text" + e_num<u8s>(intVar).All these methods work and give the same result. Which one to use is a matter of taste.
|
inline |
An addition operator for two arbitrary string expressions of the same character type.
| a | - first string expression |
| b | - second string expression |
When two objects are added - string expressions, one of type A, the other of type B, we return an object of type strexprjoin<A, B>, which contains references to these two operands. And the strexprjoin<A, B> object itself, in turn, is also a string expression, and can participate in the following addition operations. In this way, a “tree” is formed from the original strings expressions, which are then “materialized” into the final result in one call.
| auto simstr::operator+ | ( | const A & | a, |
| const std::wstring & | s ) |
Addition operator for wchar_t string expression and std::wstring.
Addition operator for wchar_t compatible string expression (char16_t or char32_t, depending on the compiler) and std::wstring.
| auto simstr::operator+ | ( | const A & | a, |
| const std::wstring_view & | s ) |
Addition operator for wchar_t string expression and std::wstring_view.
Addition operator for wchar_t compatible string expression (char16_t or char32_t, depending on the compiler) and std::wstring_view.
|
inlineconstexpr |
Addition operator of a string expression and one character.
Example:
|
inlineconstexpr |
Concatenation operator for string expression and real number (float, double).
| a | is a string expression. |
| s | - number. |
The number is converted to a string representation via sprintf("%.16g").
|
inlineconstexpr |
The addition operator for a string expression and a string literal of the same character type.
|
inlineconstexpr |
Concatenation operator for string expression and integer.
| a | is a string expression. |
| s | - number. |
The number is converted to a decimal string representation.
| auto simstr::operator+ | ( | const std::wstring & | s, |
| const A & | a ) |
Addition operator for std::wstring and wchar_t string expression.
Addition operator for std::wstring and wchar_t-compatible string expression (char16_t or char32_t, depending on the compiler).
| auto simstr::operator+ | ( | const std::wstring_view & | s, |
| const A & | a ) |
Addition operator for std::wstring_view and wchar_t string expression.
Addition operator for std::wstring_view and wchar_t-compatible string expression (char16_t or char32_t, depending on the compiler).
|
inlineconstexpr |
Concatenation operator for float (float, double) and string expression.
| s | - number. |
| a | is a string expression. |
The number is converted to a string representation via sprintf("%.16g").
|
inlineconstexpr |
The addition operator for a string literal of the same character type and string expression.
|
inlineconstexpr |
Concatenation operator for integer and string expression.
| s | - number. |
| a | is a string expression. |
The number is converted to a decimal string representation.