replace¶
- group turbo_strings_replace
This file defines
turbo::str_replace_all(), a general-purpose string replacement function designed for large, arbitrary text substitutions, especially on strings which you are receiving from some other system for further processing (e.g. processing regular expressions, escaping HTML entities, etc.).str_replace_allis designed to be efficient even when only one substitution is being performed, or when substitution is rare. If the string being modified is known at compile-time, and the substitutions vary,turbo::Substitute()may be a better choice. Example:std::string html_escaped = turbo::str_replace_all(user_input, { {"&", "&"}, {"<", "<"}, {">", ">"}, {"\"", """}, {"'", "'"}});
Functions
-
std::string str_replace_all(std::string_view s, std::initializer_list<std::pair<std::string_view, std::string_view>> replacements)¶
replace string
Replaces character sequences within a given string with replacements provided within an initializer list of key/value pairs. Candidate replacements are considered in order as they occur within the string, with earlier matches taking precedence, and longer matches taking precedence for candidates starting at the same position in the string. Once a substitution is made, the replaced text is not considered for any further substitutions. Example:
std::string s = turbo::str_replace_all( "$who bought $count #Noun. Thanks $who!", {{"$count", turbo::format(5)}, {"$who", "Bob"}, {"#Noun", "Apples"}}); EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
- Parameters:
s – The string to be replaced.
replacements – The string to replace.
- Returns:
The replaced string.
-
template<typename StrToStrMapping>
std::string str_replace_all(std::string_view s, const StrToStrMapping &replacements)¶ replace string
Overload of
str_replace_all()to accept a container of key/value replacement pairs (typically either an associative map or astd::vectorofstd::pairelements). A vector of pairs is generally more efficient. Examples:std::map<const std::string_view, const std::string_view> replacements; replacements["$who"] = "Bob"; replacements["$count"] = "5"; replacements["#Noun"] = "Apples"; std::string s = turbo::str_replace_all( "$who bought $count #Noun. Thanks $who!", replacements); EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
// A std::vector of std::pair elements can be more efficient. std::vector<std::pair<const std::string_view, std::string>> replacements; replacements.push_back({”&”, “&”}); replacements.push_back({“<”, “<”}); replacements.push_back({“>”, “>”}); std::string s = turbo::str_replace_all(“if (ptr < &foo)”, replacements); EXPECT_EQ(“if (ptr < &foo)”, s);
- Parameters:
s – The string to be replaced.
replacements – The string to replace.
- Returns:
The replaced string.
-
int str_replace_all(std::initializer_list<std::pair<std::string_view, std::string_view>> replacements, std::string *target)¶
replace string
Overload of
str_replace_all()to replace character sequences within a given output string in place with replacements provided within an initializer list of key/value pairs, returning the number of substitutions that occurred. Example:std::string s = std::string("$who bought $count #Noun. Thanks $who!"); int count; count = turbo::str_replace_all({{"$count", turbo::format(5)}, {"$who", "Bob"}, {"#Noun", "Apples"}}, &s); EXPECT_EQ(count, 4); EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
- Parameters:
replacements – The string to replace.
target – The string to be replaced.
- Returns:
The replaced string.
-
template<typename StrToStrMapping>
int str_replace_all(const StrToStrMapping &replacements, std::string *target)¶ replace string
Overload of
str_replace_all()to replace patterns within a given output string in place with replacements provided within a container of key/value pairs. Example:std::string s = std::string("if (ptr < &foo)"); int count = turbo::str_replace_all({{"&", "&"}, {"<", "<"}, {">", ">"}}, &s); EXPECT_EQ(count, 2); EXPECT_EQ("if (ptr < &foo)", s);
- Parameters:
replacements – The string to replace.
target – The string to be replaced.
- Returns:
The replaced string count.
-
std::string str_replace_all(std::string_view s, std::initializer_list<std::pair<std::string_view, std::string_view>> replacements)¶