simstr 1.5.0
Yet another strings library
 
Loading...
Searching...
No Matches
String Expressions

Description of String Expressions. More...

Concepts

concept  simstr::StrExpr
 Concept of "String Expressions".
 
concept  simstr::StrExprForType
 The concept of a string expression compatible with a given character type.
 

Classes

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< L, A, N, Compare >
 Conditional selection string expression. More...
 
struct  simstr::expr_choice_two_lit< K, N, P, 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...
 
struct  simstr::expr_replaced< K >
 A string expression that generates a string replacing all occurrences of the given substring to another string. More...
 
struct  simstr::expr_replaced_e< K, E >
 A string expression that generates a string replacing all occurrences of the given substring to string expression. 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...
 

Enumerations

enum  simstr::HexFlags : unsigned { simstr::Short = 1 , No0x = 2 , Lcase = 4 }
 Flags for the e_hex function. More...
 

Functions

template<StrExpr A, StrExprForType< typename A::symb_type > B>
constexpr strexprjoin< A, B > 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 strexprjoin_c< A, expr_char< K > > simstr::operator+ (const A &a, K s)
 Addition operator of a string expression and one character.
 
template<typename K>
constexpr expr_char< K > simstr::e_char (K s)
 Generates a string of 1 given character.
 
template<typename T, size_t N = const_lit<T>::Count>
constexpr expr_literal< typename const_lit< T >::symb_type, static_cast< size_t >(N - 1)> simstr::e_t (T &&s)
 Converts a string literal to a string expression.
 
template<StrExpr A, typename T, typename P = typename const_lit<T>::symb_type, size_t N = const_lit<T>::Count>
requires is_equal_str_type_v<typename A::symb_type, P>
constexpr expr_literal_join< false, P,(N - 1), A > 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 T, typename P = typename const_lit<T>::symb_type, size_t N = const_lit<T>::Count>
requires is_equal_str_type_v<typename A::symb_type, P>
constexpr expr_literal_join< true, P,(N - 1), A > 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 expr_spaces< u8s, N > simstr::e_spca ()
 Generates a string of N char spaces.
 
template<size_t N>
constexpr expr_spaces< uws, N > simstr::e_spcw ()
 Generates a string of N wchar_t spaces.
 
template<typename K>
constexpr expr_pad< K > simstr::e_c (size_t l, K s)
 Generates a string of l characters s of type K.
 
template<typename T, typename K = const_lit<T>::symb_type, size_t M = const_lit<T>::Count>
requires (M > 0)
constexpr expr_repeat_lit< K, M - 1 > simstr::e_repeat (T &&s, size_t l)
 Generate a string from l string constants s of type K.
 
template<StrExpr A>
constexpr expr_repeat_expr< A > simstr::e_repeat (const A &s, size_t l)
 Generate a string from l string expressions s of type K.
 
template<StrExpr A, StrExprForType< typename A::symb_type > B>
constexpr expr_choice< A, B > 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 expr_choice_one_lit< typename const_lit< T >::symb_type, A, N - 1, true > 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 expr_choice_one_lit< typename const_lit< T >::symb_type, A, N - 1, false > 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, typename K = typename const_lit<T>::symb_type, typename P = typename const_lit<L>::symb_type, size_t N = const_lit<T>::Count, size_t M = const_lit_for<typename const_lit<T>::symb_type, L>::Count>
requires is_equal_str_type_v<K, P>
constexpr expr_choice_two_lit< K, N -1, P, M - 1 > 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 expr_if< A > 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<StdStrSource T, StrExprForType< typename T::value_type > A>
constexpr strexprjoin_c< A, expr_stdstr< typename T::value_type, T >, true > simstr::operator+ (const A &a, const T &s)
 The addition operator for string expression and standard strings of compatible type.
 
template<StdStrSource T, StrExprForType< typename T::value_type > A>
constexpr strexprjoin_c< A, expr_stdstr< typename T::value_type, T >, false > simstr::operator+ (const T &s, const A &a)
 The addition operator for standard strings and string expressions of compatible type.
 
template<StrExpr A, FromIntNumber T>
constexpr strexprjoin_c< A, expr_num< typename A::symb_type, T > > simstr::operator+ (const A &a, T s)
 Concatenation operator for string expression and integer.
 
template<StrExpr A, FromIntNumber T>
constexpr strexprjoin_c< A, expr_num< typename A::symb_type, T >, false > simstr::operator+ (T s, const A &a)
 Concatenation operator for integer and string expression.
 
template<typename K, FromIntNumber T>
constexpr expr_num< K, T > 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 strexprjoin_c< A, expr_real< typename A::symb_type > > simstr::operator+ (const A &a, R s)
 Concatenation operator for string expression and real number (float, double).
 
template<StrExpr A, typename R>
requires (std::is_same_v<R, double> || std::is_same_v<R, float>)
constexpr strexprjoin_c< A, expr_real< typename A::symb_type >, false > simstr::operator+ (R s, const A &a)
 Concatenation operator for float (float, double) and string expression.
 
template<typename K>
requires is_one_of_char_v<K>
constexpr expr_real< K > simstr::e_num (double t)
 Convert a double number to a string expression.
 
template<unsigned Flags = 0, FromIntNumber T>
constexpr auto simstr::e_hex (T v)
 Allows you to concatenate text and a unsigned number in hexadecimal.
 
template<StrExpr A, typename Val, bool All, bool Ucase, bool Ox>
constexpr strexprjoin_c< A, expr_hex< typename A::symb_type, Val, All, Ucase, Ox >, true > simstr::operator+ (const A &a, const expr_hex_src< Val, All, Ucase, Ox > &b)
 Concatenation operator for a string expression and a hexadecimal representation of a number from e_hex().
 
template<StrExpr A, typename Val, bool All, bool Ucase, bool Ox>
constexpr strexprjoin_c< A, expr_hex< typename A::symb_type, Val, All, Ucase, Ox >, false > simstr::operator+ (const expr_hex_src< Val, All, Ucase, Ox > &b, const A &a)
 Concatenation operator for the hexadecimal representation of a number from e_hex() and a string expression.
 
template<StrExpr A>
constexpr strexprjoin_c< A, expr_hex< typename A::symb_type, uintptr_t, true, true, true >, true > simstr::operator+ (const A &a, const void *b)
 Concatenation operator for a string expression and a pointer, representing it as 0xDEADBEEF.
 
template<StrExpr A>
constexpr strexprjoin_c< A, expr_hex< typename A::symb_type, uintptr_t, true, true, true >, false > simstr::operator+ (const void *b, const A &a)
 Concatenation operator for a pointer and a string expression, representing it as 0xDEADBEEF.
 
template<StrExpr A, typename K = typename A::symb_type>
expr_fill< K, A, true > simstr::e_fill_left (const A &a, size_t width, K symbol=K(' '))
 Creates an expression that expands the specified string expression to the specified length given character to the left.
 
template<StrExpr A, typename K = typename A::symb_type>
expr_fill< K, A, false > simstr::e_fill_right (const A &a, size_t width, K symbol=K(' '))
 Creates an expression that expands the specified string expression to the specified length given character to the right.
 
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<StrSource A, typename K = src_str_t<A>, 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 (A &&w, T &&p, X &&r)
 Get a string expression that generates a string with all occurrences of a given substring replaced.
 
template<StrSource A, typename K = src_str_t<A>, typename T, typename X>
requires (std::is_constructible_v<str_src<K>, T> && std::is_constructible_v<str_src<K>, X> && (!is_const_lit_v<T> || !is_const_lit_v<X>))
constexpr auto simstr::e_repl (A &&w, T &&p, X &&r)
 Get a string expression that generates a string with all occurrences of a given substring replaced.
 
template<StrSource A, typename K = src_str_t<A>, typename T, StrExprForType< K > E>
requires std::is_constructible_v<str_src<K>, T>
constexpr auto simstr::e_repl (A &&w, T &&p, const E &expr)
 Get a string expression that generates a string with all occurrences of a given substring replaced.
 
template<bool UseVector = false, StrSource A, typename K = src_str_t<A>, typename ... Repl>
requires (sizeof...(Repl) % 2 == 0)
auto simstr::e_repl_const_symbols (A &&src, Repl &&... other)
 Returns a string expression that generates a string containing the given characters replaced with given substrings.
 
template<simstr::StdStrSource T>
simstr::expr_stdstr< typename T::value_type, T > std::operator+ (const T &str)
 Unary operator + for converting standard strings to string expressions.
 

Variables

template<typename K1, typename K2>
constexpr bool simstr::is_equal_str_type_v = is_one_of_char_v<K1> && is_one_of_char_v<K2> && sizeof(K1) == sizeof(K2)
 Checks whether two types are compatible string types.
 
constexpr empty_expr< u8s > simstr::eea {}
 Empty string expression of type char.
 
constexpr empty_expr< u8s > simstr::eeb {}
 Empty string expression of type char8_t.
 
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.
 

Detailed Description

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:

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.

Additionally, for compatibility with std, simstr string expressions can be converted to standard strings (std::basic_string) compatible types. Before C++23, resize and data is used, starting with C++23 the more optimal resize_and_overwrite is used. This allows for fast concatenation where standard strings are required.

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:

stringa text = header + ", count = " + count + ", done";

There are several types of string expressions out of the box to perform various operations on strings

String expressions for characters of different but compatible types can be combined into one expression. That is, you can combine char and char8_t, and under Linux wchar_t and char32_t, under Windows wchar_t and char16_t.

Enumeration Type Documentation

◆ HexFlags

enum simstr::HexFlags : unsigned

Flags for the e_hex function.

Enumerator
Short 

without leading zeroes

Function Documentation

◆ e_c()

template<typename K>
expr_pad< K > simstr::e_c ( size_t l,
K s )
constexpr

Generates a string of l characters s of type K.

Template Parameters
Kis a symbol.
Parameters
l- number of characters.
s- symbol.
Returns
a string expression that generates a string of l characters k.

◆ e_char()

template<typename K>
expr_char< K > simstr::e_char ( K s)
constexpr

Generates a string of 1 given character.

Parameters
s- symbol.
Returns
string expression for a single character string.

◆ e_choice()

template<StrExpr A, StrExprForType< typename A::symb_type > B>
expr_choice< A, B > simstr::e_choice ( bool c,
const A & a,
const B & b )
constexpr

Create a conditional string expression expr_choice.

Template Parameters
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.
Parameters
cis a Boolean condition.
ais a string expression that is executed when c == true.
bis 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:

columns_metadata.emplace_back(e_choice(name.is_empty(), "?column?", name) + "::" + metadata_column.type.to_string());
constexpr expr_choice< A, B > e_choice(bool c, const A &a, const B &b)
Create a conditional string expression expr_choice.
Definition strexpr.h:1240
lstringa<512> str = e_choice(!ret_type_resolver_, sql_value::type_name(ret_type_), "any") + " " + name_ + "(";
Small namespace for standard string methods.
Definition strexpr.h:1393

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)

◆ e_fill_left()

template<StrExpr A, typename K = typename A::symb_type>
expr_fill< K, A, true > simstr::e_fill_left ( const A & a,
size_t width,
K symbol = K(' ') )

Creates an expression that expands the specified string expression to the specified length given character to the left.

Parameters
width- pad to this width
symbol- symbol to fill

If a string expression produces a string shorter than the given length, prepend it with the specified character, padding to the desired length. Does not truncate the string to the specified length. Be careful, the length is taken in code units, not in code points, and the padding character cannot be a surrogate, that is, occupy more than one code unit. If you have to be precise with Unicode characters - use conversion to char32_t and vice versa.

◆ e_fill_right()

template<StrExpr A, typename K = typename A::symb_type>
expr_fill< K, A, false > simstr::e_fill_right ( const A & a,
size_t width,
K symbol = K(' ') )

Creates an expression that expands the specified string expression to the specified length given character to the right.

Parameters
width- pad to this width
symbol- symbol to fill

If a string expression produces a string shorter than the given length, appends it the specified character, padding to the desired length. Does not truncate the string to the specified length. Be careful, the length is taken in code units, not in code points, and the padding character cannot be a surrogate, that is, occupy more than one code unit. If you have to be precise with Unicode characters - use conversion to char32_t and vice versa.

◆ e_hex()

template<unsigned Flags = 0, FromIntNumber T>
auto simstr::e_hex ( T v)
constexpr

Allows you to concatenate text and a unsigned number in hexadecimal.

Template Parameters
Flags- format flags, bitwise OR of HexFlags.
T- number type, deducted automatically.

Example

stringa text = +"val = "sv + e_hex(10u);
EXPECT_EQ(text, "val = 0x0000000A");
stringu textu = +u"val = 0X"sv + e_hex<HexFlags::No0x | HexFlags::Short | HexFlags::Lcase>(0x12Au);
EXPECT_EQ(textu, u"val = 0X12a");
constexpr auto e_hex(T v)
Allows you to concatenate text and a unsigned number in hexadecimal.
Definition strexpr.h:1704

◆ e_if()

template<StrExpr A>
expr_if< A > simstr::e_if ( bool c,
const A & a )
constexpr

Creating a conditional string expression expr_if.

Template Parameters
A- Type expression when the condition is true, inferred from the argument
Parameters
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

void sql_func_info::build_full_name() {
// Временный буфер для результата, возьмём с запасом
// Temporary buffer for the result, take it with reserve
lstringa<512> str = e_choice(!ret_type_resolver_, sql_value::type_name(ret_type_), "any") + " " + name_ + "(";
bool add_comma = false;
for (const auto& param : params_) {
str += e_if(add_comma, ", ") + e_if(param.optional_, "[");
// Добавляет к str названия допустимых типов
// Adds the names of valid types to str
param.allowed_types.to_string(str);
if (param.optional_) {
str += "]";
}
add_comma = true;
}
// Сохраним в stringa
// Save it in stringa
full_name_ = str + e_if(unlim_params_, e_if(add_comma, ", ") + "...") + ")";
}
constexpr expr_if< A > e_if(bool c, const A &a)
Creating a conditional string expression expr_if.
Definition strexpr.h:1318

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)

◆ e_join()

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>
auto simstr::e_join ( const T & s,
L && d )
inlineconstexpr

Get a string expression concatenating the strings in the container into a single string with the given delimiter.limiter.limiter.

Template Parameters
tail- whether to add a separator after the last line.
skip_empty- skip empty lines without adding a separator.
Parameters
s- container with strings, must support range for.
d- delimiter, string literal.

◆ e_num() [1/2]

template<typename K>
requires is_one_of_char_v<K>
expr_real< K > simstr::e_num ( double t)
inlineconstexpr

Convert a double number to a string expression.

Parameters
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.

◆ e_num() [2/2]

template<typename K, FromIntNumber T>
expr_num< K, T > simstr::e_num ( T t)
constexpr

Convert an integer to a string expression.

Template Parameters
K- character type.
T- number type, inferred from the argument.
Parameters
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.

◆ e_repeat() [1/2]

template<StrExpr A>
expr_repeat_expr< A > simstr::e_repeat ( const A & s,
size_t l )
constexpr

Generate a string from l string expressions s of type K.

Template Parameters
K- character type
Parameters
l- number of repetitions
s- string expression
Returns
a string expression generating a string of l string expressions s

◆ e_repeat() [2/2]

template<typename T, typename K = const_lit<T>::symb_type, size_t M = const_lit<T>::Count>
requires (M > 0)
expr_repeat_lit< K, M - 1 > simstr::e_repeat ( T && s,
size_t l )
constexpr

Generate a string from l string constants s of type K.

Template Parameters
K- character type
Parameters
l- number of repetitions
s- string literal
Returns
a string expression generating a string of l strings s

◆ e_repl() [1/3]

template<StrSource A, typename K = src_str_t<A>, typename T, StrExprForType< K > E>
requires std::is_constructible_v<str_src<K>, T>
auto simstr::e_repl ( A && w,
T && p,
const E & expr )
constexpr

Get a string expression that generates a string with all occurrences of a given substring replaced.

Template Parameters
K- the type of the symbol, inferred from the first argument.
Parameters
w- starting string.
p- string object, searched substring, maybe runtime.
expr- string expression, what to replace with.

◆ e_repl() [2/3]

template<StrSource A, typename K = src_str_t<A>, 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)
auto simstr::e_repl ( A && w,
T && p,
X && r )
constexpr

Get a string expression that generates a string with all occurrences of a given substring replaced.

Template Parameters
K- the type of the symbol, inferred from the first argument.
Parameters
w- starting string.
p- string literal, searched substring.
r- string literal, what to replace with.

◆ e_repl() [3/3]

template<StrSource A, typename K = src_str_t<A>, typename T, typename X>
requires (std::is_constructible_v<str_src<K>, T> && std::is_constructible_v<str_src<K>, X> && (!is_const_lit_v<T> || !is_const_lit_v<X>))
auto simstr::e_repl ( A && w,
T && p,
X && r )
constexpr

Get a string expression that generates a string with all occurrences of a given substring replaced.

Template Parameters
K- the type of the symbol, inferred from the first argument.
Parameters
w- starting string.
p- string object, searched substring, maybe runtime.
r- string object, replace substring, maybe runtime.

◆ e_repl_const_symbols()

template<bool UseVector = false, StrSource A, typename K = src_str_t<A>, typename ... Repl>
requires (sizeof...(Repl) % 2 == 0)
auto simstr::e_repl_const_symbols ( A && src,
Repl &&... other )

Returns a string expression that generates a string containing the given characters replaced with given substrings.

Template Parameters
UseVector- use a vector to save symbol search results. Described in more detail in expr_replace_symbols.
Parameters
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:

out += "<div>" + e_repl_const_symbols(text, '\"', "&quot;", '<', "&lt;", '\'', "&#39;", '&', "&amp;") + "</div>";
auto e_repl_const_symbols(A &&src, Repl &&... other)
Returns a string expression that generates a string containing the given characters replaced with giv...
Definition strexpr.h:4968

In principle, e_repl_const_symbols is quite safe to return from a function if the source string external to function.

auto repl_html_symbols(ssa text) {
return e_repl_const_symbols(text, '\"', "&quot;", '<', "&lt;", '\'', "&#39;", '&', "&amp;");
}
....
out += "<div>" + repl_html_symbols(content) + "</div>";

◆ e_spca()

template<size_t N>
expr_spaces< u8s, N > simstr::e_spca ( )
constexpr

Generates a string of N char spaces.

Template Parameters
N- Number of spaces.
Returns
string expression for N char spaces.

Example:

stringa text = e_spca<10>() + text + e_spca<10>();
constexpr expr_spaces< u8s, N > e_spca()
Generates a string of N char spaces.
Definition strexpr.h:860

◆ e_spcw()

template<size_t N>
expr_spaces< uws, N > simstr::e_spcw ( )
constexpr

Generates a string of N wchar_t spaces.

Template Parameters
N- Number of spaces.
Returns
string expression for N wchar_t spaces.

Example:

stringw text = e_spcw<10>() + text + e_spcw<10>();
constexpr expr_spaces< uws, N > e_spcw()
Generates a string of N wchar_t spaces.
Definition strexpr.h:878

◆ e_t()

template<typename T, size_t N = const_lit<T>::Count>
expr_literal< typename const_lit< T >::symb_type, static_cast< size_t >(N - 1)> simstr::e_t ( T && s)
constexpr

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:

int intVar = calculate();
...
res = "text" + intVar;
...
res = intVar + "text";

In this case, you can convert the literal to a string expression in two ways:

  • add _ss: "text"_ss, which converts the literal to simple_str_nt: res = "text"_ss + intVar
  • apply e_t: e_t("text"), which converts the literal to expr_literal: res = e_t("text") + intVar

In 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:

  • Add an operand - an empty string expression: result = eea + "text" + intVar, result = "text" + eea + intVar
  • Convert another operand to a string expression: result = "text" + e_num<u8s>(intVar).

All these methods work and give the same result. Which one to use is a matter of taste.

◆ operator+() [1/13]

template<StrExpr A, StrExprForType< typename A::symb_type > B>
strexprjoin< A, B > simstr::operator+ ( const A & a,
const B & b )
constexpr

An addition operator for two arbitrary string expressions of the same character type.

Parameters
a- first string expression
b- second string expression
Returns
strexprjoin<A, B>, a string expression that generates a join of the given expressions.

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.

◆ operator+() [2/13]

template<StrExpr A, typename Val, bool All, bool Ucase, bool Ox>
strexprjoin_c< A, expr_hex< typename A::symb_type, Val, All, Ucase, Ox >, true > simstr::operator+ ( const A & a,
const expr_hex_src< Val, All, Ucase, Ox > & b )
constexpr

Concatenation operator for a string expression and a hexadecimal representation of a number from e_hex().

Parameters
ais a string expression.
bis e_hex(number).

◆ operator+() [3/13]

template<StrExpr A>
strexprjoin_c< A, expr_hex< typename A::symb_type, uintptr_t, true, true, true >, true > simstr::operator+ ( const A & a,
const void * b )
constexpr

Concatenation operator for a string expression and a pointer, representing it as 0xDEADBEEF.

Parameters
ais a string expression.
bis a pointer.

◆ operator+() [4/13]

template<typename K, StrExprForType< K > A>
strexprjoin_c< A, expr_char< K > > simstr::operator+ ( const A & a,
K s )
constexpr

Addition operator of a string expression and one character.

Returns
a string expression that combines the passed expression and a character.

Example:

reply = prompt + '>' + result;

◆ operator+() [5/13]

template<StrExpr A, typename R>
requires (std::is_same_v<R, double> || std::is_same_v<R, float>)
strexprjoin_c< A, expr_real< typename A::symb_type > > simstr::operator+ ( const A & a,
R s )
inlineconstexpr

Concatenation operator for string expression and real number (float, double).

Parameters
ais a string expression.
s- number.

The number is converted to a string representation via sprintf("%.16g").

◆ operator+() [6/13]

template<StrExpr A, typename T, typename P = typename const_lit<T>::symb_type, size_t N = const_lit<T>::Count>
requires is_equal_str_type_v<typename A::symb_type, P>
expr_literal_join< false, P,(N - 1), A > simstr::operator+ ( const A & a,
T && s )
constexpr

The addition operator for a string expression and a string literal of the same character type.

Returns
A string expression concatenating the operands.

◆ operator+() [7/13]

template<StrExpr A, FromIntNumber T>
strexprjoin_c< A, expr_num< typename A::symb_type, T > > simstr::operator+ ( const A & a,
T s )
constexpr

Concatenation operator for string expression and integer.

Parameters
ais a string expression.
s- number.

The number is converted to a decimal string representation.

◆ operator+() [8/13]

template<StrExpr A, typename Val, bool All, bool Ucase, bool Ox>
strexprjoin_c< A, expr_hex< typename A::symb_type, Val, All, Ucase, Ox >, false > simstr::operator+ ( const expr_hex_src< Val, All, Ucase, Ox > & b,
const A & a )
constexpr

Concatenation operator for the hexadecimal representation of a number from e_hex() and a string expression.

Parameters
a- e_hex(number).
bis a string expression.

◆ operator+() [9/13]

template<StrExpr A>
strexprjoin_c< A, expr_hex< typename A::symb_type, uintptr_t, true, true, true >, false > simstr::operator+ ( const void * b,
const A & a )
constexpr

Concatenation operator for a pointer and a string expression, representing it as 0xDEADBEEF.

Parameters
a- pointer.
bis a string expression.

◆ operator+() [10/13]

template<StrExpr A, typename R>
requires (std::is_same_v<R, double> || std::is_same_v<R, float>)
strexprjoin_c< A, expr_real< typename A::symb_type >, false > simstr::operator+ ( R s,
const A & a )
inlineconstexpr

Concatenation operator for float (float, double) and string expression.

Parameters
s- number.
ais a string expression.

The number is converted to a string representation via sprintf("%.16g").

◆ operator+() [11/13]

template<StrExpr A, typename T, typename P = typename const_lit<T>::symb_type, size_t N = const_lit<T>::Count>
requires is_equal_str_type_v<typename A::symb_type, P>
expr_literal_join< true, P,(N - 1), A > simstr::operator+ ( T && s,
const A & a )
constexpr

The addition operator for a string literal of the same character type and string expression.

Returns
A string expression concatenating the operands.

◆ operator+() [12/13]

template<StrExpr A, FromIntNumber T>
strexprjoin_c< A, expr_num< typename A::symb_type, T >, false > simstr::operator+ ( T s,
const A & a )
constexpr

Concatenation operator for integer and string expression.

Parameters
s- number.
ais a string expression.

The number is converted to a decimal string representation.

◆ operator+() [13/13]

template<simstr::StdStrSource T>
simstr::expr_stdstr< typename T::value_type, T > std::operator+ ( const T & str)

Unary operator + for converting standard strings to string expressions.

Standard strings can only participate directly in string expressions when the other operand is also a string expression. If the other operand is not a string expression, use this operator to turn std::basic_string or std::basic_string_view into string expression. Example

std::string make_text(const std::string& text, int count, std::string_view what, std::string_view what_p = ""sv) {
return +text + " " + count + " " + e_choice(what_p.empty(), what + e_if(count > 1, "s"), e_choice(count > 1, +what_p, +what));
}

Variable Documentation

◆ is_equal_str_type_v

template<typename K1, typename K2>
bool simstr::is_equal_str_type_v = is_one_of_char_v<K1> && is_one_of_char_v<K2> && sizeof(K1) == sizeof(K2)
constexpr

Checks whether two types are compatible string types.

Template Parameters
K1is the first type to check.
K2is the second type to check.

Both types must be string types and the same size. That is, char and char8_t are always compatible, wchar_t is identical to char32_t on Linux, and char16_t on Windows. This is for the ability to mix string expressions of compatible types.