simstr 1.2.4
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 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.
 

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.

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

Function Documentation

◆ e_c()

template<typename K>
auto simstr::e_c ( size_t l,
K s )
inlineconstexpr

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>
auto simstr::e_char ( K s)
inlineconstexpr

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>
auto simstr::e_choice ( bool c,
const A & a,
const B & b )
inlineconstexpr

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 auto e_choice(bool c, const A &a, const B &b)
Create a conditional string expression expr_choice.
Definition strexpr.h:1037
lstringa<512> str = e_choice(!ret_type_resolver_, sql_value::type_name(ret_type_), "any") + " " + name_ + "(";

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_if()

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

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 auto e_if(bool c, const A &a)
Creating a conditional string expression expr_if.
Definition strexpr.h:1113

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()

template<typename K, typename T>
auto simstr::e_num ( T t)
inlineconstexpr

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_real()

template<typename K>
requires (is_one_of_std_char_v<K>)
auto simstr::e_real ( 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_repl()

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)
auto simstr::e_repl ( simple_str< K > w,
T && p,
X && r )
inlineconstexpr

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 line.
p- string literal, searched substring.
r- string literal, what to replace with.

◆ e_repl_const_symbols()

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 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(simple_str< K > src, Repl &&... other)
Returns a string expression that generates a string containing the given characters replaced with giv...
Definition sstring.h:6520

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>
auto simstr::e_spca ( )
inlineconstexpr

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 auto e_spca()
Generates a string of N char spaces.
Definition strexpr.h:751

◆ e_spcw()

template<size_t N>
auto simstr::e_spcw ( )
inlineconstexpr

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 auto e_spcw()
Generates a string of N wchar_t spaces.
Definition strexpr.h:769

◆ e_t()

template<typename T, size_t N = const_lit<T>::Count>
auto simstr::e_t ( T && s)
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:

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/12]

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

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/12]

template<StrExprForType< uws > A>
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.

◆ operator+() [3/12]

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.

Addition operator for wchar_t compatible string expression (char16_t or char32_t, depending on the compiler) and std::wstring_view.

◆ operator+() [4/12]

template<typename K, StrExprForType< K > A>
auto simstr::operator+ ( const A & a,
K s )
inlineconstexpr

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/12]

template<StrExpr A, typename R>
requires (std::is_same_v<R, double> || std::is_same_v<R, float>)
auto 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/12]

template<StrExpr A, typename K = typename A::symb_type, typename T, size_t N = const_lit_for<K, T>::Count>
auto simstr::operator+ ( const A & a,
T && s )
inlineconstexpr

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/12]

template<StrExpr A, FromIntNumber T>
auto simstr::operator+ ( const A & a,
T s )
inlineconstexpr

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/12]

template<StrExprForType< uws > A>
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).

◆ operator+() [9/12]

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.

Addition operator for std::wstring_view and wchar_t-compatible string expression (char16_t or char32_t, depending on the compiler).

◆ operator+() [10/12]

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>))
auto 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/12]

template<StrExpr A, typename K = typename A::symb_type, typename T, size_t N = const_lit_for<K, T>::Count>
auto simstr::operator+ ( T && s,
const A & a )
inlineconstexpr

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

Returns
A string expression concatenating the operands.

◆ operator+() [12/12]

template<StrExpr A, FromIntNumber T>
auto simstr::operator+ ( T s,
const A & a )
inlineconstexpr

Concatenation operator for integer and string expression.

Parameters
s- number.
ais a string expression.

The number is converted to a decimal string representation.