simstr 1.2.4
Yet another strings library
 
Loading...
Searching...
No Matches
simstr::expr_replace_symbols< K, UseVectorForReplace > Struct Template Reference

A type for a string expression that generates a string in which the given characters are replaced by the given strings. More...

#include <sstring.h>

Public Member Functions

constexpr expr_replace_symbols (simple_str< K > source, const std::vector< std::pair< K, simple_str< K > > > &repl)
 Expression constructor.
 

Detailed Description

template<typename K, bool UseVectorForReplace = false>
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.

Template Parameters
K- symbol type.
UseVectorForReplace- use a vector to remember the results of searching for occurrences of characters.

This type is used when the composition of symbols or their corresponding replacements is not known at compile time, and is defined at runtime. A vector of character - replacement string pairs is passed to the constructor. The UseVectorForReplace parameter specifies the implementation strategy. The point is that the work of any string expressions is divided into two phases - the length() call, which counts the number of characters in the result, and a call to place(), which places the result in the provided buffer. When UseVectorForReplace == true during the phase of counting the number of characters, the position of the found occurrences are stored in the vector, and during the second phase the search is no longer performed, and the positions are taken from the vector. This, on the one hand, reduces the time in the second phase - there is no need to search again, but it increases time in the first phase - adding elements to the vector is not free, and takes time. When UseVectorForReplace == false during the phase of counting the number of characters, positions in the local array are remembered the first 16 occurrences and their total number, and during the second phase, if there are more than 16 occurrences, then the search is repeated, but only from the position of the 16th occurrence. This may increase the time in the second phase, but reduces the time in the first phase - no need to add elements to the vector, no need for dynamic allocation. In different use cases, one or another strategy may be more optimal, and you can decide for yourself whichever is more suitable in each specific case.

Constructor & Destructor Documentation

◆ expr_replace_symbols()

template<typename K, bool UseVectorForReplace = false>
simstr::expr_replace_symbols< K, UseVectorForReplace >::expr_replace_symbols ( simple_str< K > source,
const std::vector< std::pair< K, simple_str< K > > > & repl )
inlineconstexpr

Expression constructor.

Parameters
source- source string.
repl- a vector of "character->replacement string" pairs.

Example:

stringa result = expr_replace_symbols<u8s, true>{source, {
{'-', ""},
{'<', "&lt;"},
{'>', "&gt;"},
{'\'', "&#39;"},
{'\"', "&quot;"},
{'&', "&amp;"},
}};
constexpr expr_replace_symbols(simple_str< K > source, const std::vector< std::pair< K, simple_str< K > > > &repl)
Expression constructor.
Definition sstring.h:6144

An example is provided for clarity of use. In this case, both the characters to be replaced and the replacement strings known at compile time, in which case it is better to use e_repl_const_symbols, and this class is used when characters or replacements are specified at runtime.


The documentation for this struct was generated from the following file: