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

Description of String Expressions. More...

Concepts

concept  simstr::is_equal_str_type_v
 Checks whether two types are compatible string types.
 
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::basic_string and std::basic_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...
 
struct  simstr::e_concat< K, G, Arg, Args >
 String expression concatenating the specified string expressions using the specified delimiter. More...
 
struct  simstr::e_subst< K, PtLen, Args >
 String expression that substitutes the values ​​of the passed string expressions into the specified places in a string literal. More...
 
struct  simstr::e_vsubst< K, Args >
 String expression that substitutes the values ​​of the passed string expressions into the specified positions in the pattern string, specified at runtime. 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<typename K, FromIntNumber T>
constexpr expr_num< K, T > simstr::e_num (T t)
 Convert an integer to a string expression.
 
template<unsigned R, auto fp = f::df, FromIntNumber T>
requires good_int_flags<flags_checker<R, T, decltype(fp)>, false>
constexpr auto simstr::e_int (T v)
 Creates an object that is converted to a string expression that generates a string representation of a number.
 
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)
 Creates an object that can be converted to a string expression that generates a hexadecimal representation of a number.
 
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<char... Chars>
SS_CONSTEVAL auto simstr::operator""_fmt ()
 Creates a set of radix and flags that can be applied to integers numbers to set formatting parameters using the division operator: num / 0xFormatOptions_fmt. Formatting parameters are set as follows: first comes 0x, then the radix written in decimal form. Next may be symbols indicating various flags:
 
template<StrSource A, typename K = symb_type_from_src_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 = symb_type_from_src_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 = symb_type_from_src_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 = symb_type_from_src_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

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.

Remember: a string expression is not a string, it is only an "instruction" on how to assemble the string.

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:1466
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:1600

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

Creates an object that can be converted to a string expression that generates a hexadecimal representation of a number.

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

This is a faster conversion to a hexadecimal string than e_int, but with less power formatting. By default, a representation of the form 0x000012AB is created, that is, prefixed with 0x and padded with zeros to the width by number type size (2 characters per byte). You can specify flags:

  • HexFlags::Short - do not pad with zeros to a fixed width.
  • HexFlags::Lcase - display characters in lowercase.
  • HexFlags::No0x - do not display the prefix.

In addition, this expression can be written briefly as num / N_f16 N stands for formatting flags

Example

stringa text = +"val = "sv + e_hex(10);
EXPECT_EQ(text, "val = 0x0000000A");
stringu textu = +u"val = 0X"sv + e_hex<HexFlags::No0x | HexFlags::Short | HexFlags::Lcase>(0x12A);
EXPECT_EQ(textu, u"val = 0X12a");
text = "Num in hex: " + num / 0_f16; // same as e_hex(num);
text = "Num in hex: " + num / 13_f16; // same as e_hex<HexFlags::Short | HexFlags::No0x>(num);
text = "Num in hex: " + num / 123_f16; // same as e_hex<HexFlags::Short | HexFlags::No0x | HexFlags::Lcase>(num);
text = "Num in hex: " + num / 2_f16; // same as e_hex<HexFlags::No0x | HexFlags::Lcase>(num);
constexpr auto e_hex(T v)
Creates an object that can be converted to a string expression that generates a hexadecimal represent...
Definition strexpr.h:2584

◆ 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:1544

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

template<unsigned R, auto fp = f::df, FromIntNumber T>
requires good_int_flags<flags_checker<R, T, decltype(fp)>, false>
auto simstr::e_int ( T v)
constexpr

Creates an object that is converted to a string expression that generates a string representation of a number.

Template Parameters
R- Number base, must be from 2 to 36.
fp- parameters for format number.
T- number type, deduced from the argument.
Parameters
v- number.
Returns
the source for the string expression expr_integer_src.

number formatting parameters are specified by a set of constants, via ’|'.

  • f::l when setting the field width, align to the left.
  • f::r when setting the field width, align to the right.
  • f::с when setting the field width, align to the center.
  • f::p print number system prefix, 0b for 2, 0 for 8, 0x for 16.
  • f::P print number system prefix, 0B for 2, 0 for 8, 0X for 16.
  • f::z pad the number to the specified width with zeros on the let. Not compatible with alignment options.
  • f::u For radix greater than 10, output characters in uppercase.
  • f::sp For signed number types, print '+' before the number for positive values.
  • f::ss For signed number types, print a space before the number for positive values.
  • f::f<'c'> Specifies a placeholder character when specifying the field width. Default is space.
  • f::w<N> Sets the field width. When specifying the width, you can either specify the desired alignment (r, l, c), or zero padding (z), but not both. If none of these are specified, right alignment is applied. The number is padded to the given width if it is shorter. If it is longer, then the entire number is displayed.
  • f::wp Same as f::w<-1>. In this case, the width must be passed as an additional argument e_int.
    Example:
    stringu u16t = u"Number "_ss + num
    + " in octal is " + e_int<8, f::w<16> | f::z>(num)
    + ", in binary is " + e_int<2, f::w<32> | f::p | f::z>(num);
    constexpr auto e_int(T v)
    Creates an object that is converted to a string expression that generates a string representation of ...
    Definition strexpr.h:2383
    It is also possible to write this function in a shortened form: num / 0xFormatOptions_fmt. Formatting parameters are set as follows: first comes 0x, then the radix written in decimal form. Next may be symbols indicating various flags:
  • b when setting the field width, align to the left, analogous to f::l.
  • d when setting the field width, align to the right, analogous to f::r.
  • c when setting the field width, align to the center, analogous to f::c.
  • a display the number system prefix, 0b for 2, 0 for 8, 0x for 16, analogous to f::p.
  • A display the number system prefix, 0B for 2, 0 for 8, 0X for 16, analogous to f::P.
  • 0 pad the number to the specified width with zeros on the left. Not compatible with alignment options, similar to f::z.
  • E For radix greater than 10, display characters in uppercase, analogous to f::u.
  • e For signed number types, for positive values, print '+' before the number, analogous to f::sp.
  • f For signed number types, for positive values, print a space before the number, analogous to f::ss.
  • FCodeInhex Sets the code of the filler character when specifying the field width, analogous to f::f<'c'>.
  • Number in decimal form, not starting from 0. Sets the field width, analogous to f::w<N>. When specifying the width, you can either specify the desired alignment (b, c, d), or zero padding (0), but not both. If none of these are specified, right alignment is applied. The number is padded to the given width if it is shorter. If it is longer, then the entire number is displayed.
  • ' delimiter, skipped.

Since after F all characters are perceived as hex code of the delimiter character, if necessary, use it it is better to put it at the end of format literal, or separate with '.
Example:

stringu u16t = u"Number "_ss + num
+ " in octal is " + num / 0x8'016_fmt // same as e_int<8, f::w<16> | f::z>(num)
+ ", in binary is " + num / 0x2a032_fmt; // same as e_int<2, f::w<32> | f::p | f::z>(num);
....
stringa text = "Count is "_ss + count / 0x16A08E_fmt; // same as e_int<16, f::P | f::z | f::w<8> | f::u>(count)
....
stringa text = "Number "_ss + count / 0x16c20EF5F_fmt; // same as e_int<16, f::c | f::w<20> | f::u | f::f<'_'>>(count)

◆ 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 string.
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 = symb_type_from_src_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 = symb_type_from_src_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 = symb_type_from_src_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 = symb_type_from_src_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:6140

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

◆ 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:1104

◆ 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""_fmt()

template<char... Chars>
SS_CONSTEVAL auto simstr::literals::operator""_fmt ( )

Creates a set of radix and flags that can be applied to integers numbers to set formatting parameters using the division operator: num / 0xFormatOptions_fmt. Formatting parameters are set as follows: first comes 0x, then the radix written in decimal form. Next may be symbols indicating various flags:

  • b when setting the field width, align to the left, analogous to f::l.
  • d when setting the field width, align to the right, analogous to f::r.
  • c when setting the field width, align to the center, analogous to f::c.
  • a display the number system prefix, 0b for 2, 0 for 8, 0x for 16, analogous to f::p.
  • A display the number system prefix, 0B for 2, 0 for 8, 0X for 16, analogous to f::P.
  • 0 pad the number to the specified width with zeros on the left. Not compatible with alignment options, similar to f::z.
  • E For radix greater than 10, display characters in uppercase, analogous to f::u.
  • e For signed number types, for positive values, print '+' before the number, analogous to f::sp.
  • f For signed number types, for positive values, print a space before the number, analogous to f::ss.
  • FCodeInhex Sets the code of the filler character when specifying the field width, analogous to f::f<'c'>.
  • Number in decimal form, not starting from 0. Sets the field width, analogous to f::w<N>. When specifying the width, you can either specify the desired alignment (b, c, d), or zero padding (0), but not both. If none of these are specified, right alignment is applied. The number is padded to the given width if it is shorter. If it is longer, then the entire number is displayed.
  • ' delimiter, skipped.

Since after F all characters are perceived as hex code of the delimiter character, if necessary, use it it is better to put it at the end of format literal, or separate with '.
Example:

stringu u16t = u"Number "_ss + num
+ " in octal is " + num / 0x8'016_fmt // same as e_int<8, f::w<16> | f::z>(num)
+ ", in binary is " + num / 0x2a032_fmt; // same as e_int<2, f::w<32> | f::p | f::z>(num);
....
stringa text = "Count is "_ss + count / 0x16A08E_fmt; // same as e_int<16, f::P | f::z | f::w<8> | f::u>(count)
....
stringa text = "Number "_ss + count / 0x16c20EF5F_fmt; // same as e_int<16, f::c | f::w<20> | f::u | f::f<'_'>>(count)

◆ operator+() [1/5]

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

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+() [3/5]

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+() [4/5]

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+() [5/5]

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));
}