simstr 1.5.0
Yet another strings library
 
Loading...
Searching...
No Matches
simstr::str Namespace Reference

Small namespace for standard string methods. More...

Functions

template<typename K, typename A, StrExprForType< K > E>
std::basic_string< K, std::char_traits< K >, A > & change (std::basic_string< K, std::char_traits< K >, A > &str, size_t from, size_t count, const E &expr)
 Replace a portion of a standard string with the given string expression.
 
template<typename K, typename A, StrExprForType< K > E>
std::basic_string< K, std::char_traits< K >, A > & append (std::basic_string< K, std::char_traits< K >, A > &str, const E &expr)
 Append a string expression to a standard string.
 
template<typename K, typename A, StrExprForType< K > E>
std::basic_string< K, std::char_traits< K >, A > & prepend (std::basic_string< K, std::char_traits< K >, A > &str, const E &expr)
 Insert a string expression at the beginning of a standard string.
 
template<typename K, typename A, StrExprForType< K > E>
std::basic_string< K, std::char_traits< K >, A > & insert (std::basic_string< K, std::char_traits< K >, A > &str, size_t from, const E &expr)
 Insert a string expression at the specified position in a standard string.
 
template<typename K, typename A, StrExprForType< K > E, typename T>
requires (std::is_constructible_v<str_src<K>, T>)
std::basic_string< K, std::char_traits< K >, A > & replace (std::basic_string< K, std::char_traits< K >, A > &str, T &&pattern, const E &repl, size_t offset=0, size_t max_count=-1)
 A function for searching for substrings in a standard string and replacing the found occurrences with a string expression.
 
template<typename K, typename A>
std::basic_string< K, std::char_traits< K >, A > & replace (std::basic_string< K, std::char_traits< K >, A > &str, str_src< K > pattern, str_src< K > repl, size_t offset=0, size_t max_count=-1)
 Function for searching for substrings in a standard string and replacing found occurrences with another substring.
 

Detailed Description

Small namespace for standard string methods.

Function Documentation

◆ append()

template<typename K, typename A, StrExprForType< K > E>
std::basic_string< K, std::char_traits< K >, A > & simstr::str::append ( std::basic_string< K, std::char_traits< K >, A > & str,
const E & expr )

Append a string expression to a standard string.

Template Parameters
K- character type.
A- allocator type.
E- string expression type.
Parameters
str- string.
expr- string expression to insert.
Returns
std::basic_string<K, std::char_traits<K>, A>& - reference to the passed string.

The method gets the length of the string expression, increases the size of the string, and materializes the string expression beyond the end of the string, without using intermediate buffers. Before C++23, resize was used; since C++23, resize_and_overwrite was used.

IMPORTANT!!! Parts of a string expression must not reference the string itself, otherwise the result is undefined!!!

◆ change()

template<typename K, typename A, StrExprForType< K > E>
std::basic_string< K, std::char_traits< K >, A > & simstr::str::change ( std::basic_string< K, std::char_traits< K >, A > & str,
size_t from,
size_t count,
const E & expr )

Replace a portion of a standard string with the given string expression.

Template Parameters
K- character type.
A- allocator type.
E- string expression type.
Parameters
str- string.
from- starting position of replacement.
count- number of characters to replace.
expr- string expression to replace.
Returns
std::basic_string<K, std::char_traits<K>, A>& - reference to the passed string.

The method gets the length of the string expression, increases the string size if necessary, and materializes the string expression at the desired location, without using intermediate buffers. When increasing the string size, resize is used before C++23; resize_and_overwrite is used starting with C++23.

IMPORTANT!!! Parts of a string expression must not reference the string itself, otherwise the result is undefined!!!

◆ insert()

template<typename K, typename A, StrExprForType< K > E>
std::basic_string< K, std::char_traits< K >, A > & simstr::str::insert ( std::basic_string< K, std::char_traits< K >, A > & str,
size_t from,
const E & expr )

Insert a string expression at the specified position in a standard string.

Template Parameters
K- character type.
A- allocator type.
E- string expression type.
Parameters
str- string.
from- insertion position.
expr- string expression to insert.
Returns
std::basic_string<K, std::char_traits<K>, A>& - a reference to the passed string.

The method gets the length of the string expression, increases the size of the string, shifts its contents and materializes the string expression to the desired location, without using intermediate buffers. Before C++23, resize was used; since C++23, resize_and_overwrite was used.

IMPORTANT!!! Parts of a string expression must not reference the string itself, otherwise the result is undefined!!!

◆ prepend()

template<typename K, typename A, StrExprForType< K > E>
std::basic_string< K, std::char_traits< K >, A > & simstr::str::prepend ( std::basic_string< K, std::char_traits< K >, A > & str,
const E & expr )

Insert a string expression at the beginning of a standard string.

Template Parameters
K- character type.
A- allocator type.
E- string expression type.
Parameters
str- string.
expr- string expression to insert.
Returns
std::basic_string<K, std::char_traits<K>, A>& - reference to the passed string.

The method gets the length of the string expression, increases the size of the string, shifts its contents and materializes the string expression at the beginning of the string, without using intermediate buffers. Before C++23, resize was used; since C++23, resize_and_overwrite was used.

IMPORTANT!!! Parts of a string expression must not reference the string itself, otherwise the result is undefined!!!

◆ replace() [1/2]

template<typename K, typename A>
std::basic_string< K, std::char_traits< K >, A > & simstr::str::replace ( std::basic_string< K, std::char_traits< K >, A > & str,
str_src< K > pattern,
str_src< K > repl,
size_t offset = 0,
size_t max_count = -1 )

Function for searching for substrings in a standard string and replacing found occurrences with another substring.

Template Parameters
K- the character type of the string.
A- the type of the string allocator.
Parameters
str- the string in which we replace occurrences of substrings.
pattern- the searched substring (any type that converts to simple_str).
repl- replacement string (any type convertible to simple_str).
offset- the starting offset for the search.
max_count- maximum number of replacements.
Returns
std::basic_string<K, std::char_traits<K>, A>& - reference to the modified string.

◆ replace() [2/2]

template<typename K, typename A, StrExprForType< K > E, typename T>
requires (std::is_constructible_v<str_src<K>, T>)
std::basic_string< K, std::char_traits< K >, A > & simstr::str::replace ( std::basic_string< K, std::char_traits< K >, A > & str,
T && pattern,
const E & repl,
size_t offset = 0,
size_t max_count = -1 )

A function for searching for substrings in a standard string and replacing the found occurrences with a string expression.

Template Parameters
K- the string character type.
A- the string allocator type.
E- the string expression type.
T- the search substring type.
Parameters
str- the string in which to replace substring occurrences.
pattern- the search substring (any type convertible to simple_str).
repl- the string expression to replace.
offset- the starting offset for the search.
max_count- the maximum number of replacements.
Returns
std::basic_string<K, std::char_traits<K>, A>& - a reference to the string being modified.

Parts of the string expression must not reference the string being modified itself. If the search substring is not found, the string expression is not even evaluated. Then, when performing a replacement, the string expression is evaluated only once at the first replacement location, and characters from the first location are simply copied to subsequent replacement locations. This saves memory and time if you need to replace with some kind of "composite" string.