abseil-cpp/absl/strings/str_replace.h
Go to the documentation of this file.
1 //
2 // Copyright 2017 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // -----------------------------------------------------------------------------
17 // File: str_replace.h
18 // -----------------------------------------------------------------------------
19 //
20 // This file defines `absl::StrReplaceAll()`, a general-purpose string
21 // replacement function designed for large, arbitrary text substitutions,
22 // especially on strings which you are receiving from some other system for
23 // further processing (e.g. processing regular expressions, escaping HTML
24 // entities, etc.). `StrReplaceAll` is designed to be efficient even when only
25 // one substitution is being performed, or when substitution is rare.
26 //
27 // If the string being modified is known at compile-time, and the substitutions
28 // vary, `absl::Substitute()` may be a better choice.
29 //
30 // Example:
31 //
32 // std::string html_escaped = absl::StrReplaceAll(user_input, {
33 // {"&", "&"},
34 // {"<", "&lt;"},
35 // {">", "&gt;"},
36 // {"\"", "&quot;"},
37 // {"'", "&#39;"}});
38 #ifndef ABSL_STRINGS_STR_REPLACE_H_
39 #define ABSL_STRINGS_STR_REPLACE_H_
40 
41 #include <string>
42 #include <utility>
43 #include <vector>
44 
45 #include "absl/base/attributes.h"
46 #include "absl/strings/string_view.h"
47 
48 namespace absl {
50 
51 // StrReplaceAll()
52 //
53 // Replaces character sequences within a given string with replacements provided
54 // within an initializer list of key/value pairs. Candidate replacements are
55 // considered in order as they occur within the string, with earlier matches
56 // taking precedence, and longer matches taking precedence for candidates
57 // starting at the same position in the string. Once a substitution is made, the
58 // replaced text is not considered for any further substitutions.
59 //
60 // Example:
61 //
62 // std::string s = absl::StrReplaceAll(
63 // "$who bought $count #Noun. Thanks $who!",
64 // {{"$count", absl::StrCat(5)},
65 // {"$who", "Bob"},
66 // {"#Noun", "Apples"}});
67 // EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
70  std::initializer_list<std::pair<absl::string_view, absl::string_view>>
71  replacements);
72 
73 // Overload of `StrReplaceAll()` to accept a container of key/value replacement
74 // pairs (typically either an associative map or a `std::vector` of `std::pair`
75 // elements). A vector of pairs is generally more efficient.
76 //
77 // Examples:
78 //
79 // std::map<const absl::string_view, const absl::string_view> replacements;
80 // replacements["$who"] = "Bob";
81 // replacements["$count"] = "5";
82 // replacements["#Noun"] = "Apples";
83 // std::string s = absl::StrReplaceAll(
84 // "$who bought $count #Noun. Thanks $who!",
85 // replacements);
86 // EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
87 //
88 // // A std::vector of std::pair elements can be more efficient.
89 // std::vector<std::pair<const absl::string_view, std::string>> replacements;
90 // replacements.push_back({"&", "&amp;"});
91 // replacements.push_back({"<", "&lt;"});
92 // replacements.push_back({">", "&gt;"});
93 // std::string s = absl::StrReplaceAll("if (ptr < &foo)",
94 // replacements);
95 // EXPECT_EQ("if (ptr &lt; &amp;foo)", s);
96 template <typename StrToStrMapping>
98  const StrToStrMapping& replacements);
99 
100 // Overload of `StrReplaceAll()` to replace character sequences within a given
101 // output string *in place* with replacements provided within an initializer
102 // list of key/value pairs, returning the number of substitutions that occurred.
103 //
104 // Example:
105 //
106 // std::string s = std::string("$who bought $count #Noun. Thanks $who!");
107 // int count;
108 // count = absl::StrReplaceAll({{"$count", absl::StrCat(5)},
109 // {"$who", "Bob"},
110 // {"#Noun", "Apples"}}, &s);
111 // EXPECT_EQ(count, 4);
112 // EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
113 int StrReplaceAll(
114  std::initializer_list<std::pair<absl::string_view, absl::string_view>>
115  replacements,
117 
118 // Overload of `StrReplaceAll()` to replace patterns within a given output
119 // string *in place* with replacements provided within a container of key/value
120 // pairs.
121 //
122 // Example:
123 //
124 // std::string s = std::string("if (ptr < &foo)");
125 // int count = absl::StrReplaceAll({{"&", "&amp;"},
126 // {"<", "&lt;"},
127 // {">", "&gt;"}}, &s);
128 // EXPECT_EQ(count, 2);
129 // EXPECT_EQ("if (ptr &lt; &amp;foo)", s);
130 template <typename StrToStrMapping>
131 int StrReplaceAll(const StrToStrMapping& replacements, std::string* target);
132 
133 // Implementation details only, past this point.
134 namespace strings_internal {
135 
139  size_t offset;
140 
142  absl::string_view replacement_str, size_t offset_val)
143  : old(old_str), replacement(replacement_str), offset(offset_val) {}
144 
145  // One substitution occurs "before" another (takes priority) if either
146  // it has the lowest offset, or it has the same offset but a larger size.
147  bool OccursBefore(const ViableSubstitution& y) const {
148  if (offset != y.offset) return offset < y.offset;
149  return old.size() > y.old.size();
150  }
151 };
152 
153 // Build a vector of ViableSubstitutions based on the given list of
154 // replacements. subs can be implemented as a priority_queue. However, it turns
155 // out that most callers have small enough a list of substitutions that the
156 // overhead of such a queue isn't worth it.
157 template <typename StrToStrMapping>
158 std::vector<ViableSubstitution> FindSubstitutions(
159  absl::string_view s, const StrToStrMapping& replacements) {
160  std::vector<ViableSubstitution> subs;
161  subs.reserve(replacements.size());
162 
163  for (const auto& rep : replacements) {
164  using std::get;
165  absl::string_view old(get<0>(rep));
166 
167  size_t pos = s.find(old);
168  if (pos == s.npos) continue;
169 
170  // Ignore attempts to replace "". This condition is almost never true,
171  // but above condition is frequently true. That's why we test for this
172  // now and not before.
173  if (old.empty()) continue;
174 
175  subs.emplace_back(old, get<1>(rep), pos);
176 
177  // Insertion sort to ensure the last ViableSubstitution comes before
178  // all the others.
179  size_t index = subs.size();
180  while (--index && subs[index - 1].OccursBefore(subs[index])) {
181  std::swap(subs[index], subs[index - 1]);
182  }
183  }
184  return subs;
185 }
186 
188  std::vector<ViableSubstitution>* subs_ptr,
189  std::string* result_ptr);
190 
191 } // namespace strings_internal
192 
193 template <typename StrToStrMapping>
195  const StrToStrMapping& replacements) {
196  auto subs = strings_internal::FindSubstitutions(s, replacements);
198  result.reserve(s.size());
200  return result;
201 }
202 
203 template <typename StrToStrMapping>
204 int StrReplaceAll(const StrToStrMapping& replacements, std::string* target) {
205  auto subs = strings_internal::FindSubstitutions(*target, replacements);
206  if (subs.empty()) return 0;
207 
209  result.reserve(target->size());
210  int substitutions =
212  target->swap(result);
213  return substitutions;
214 }
215 
217 } // namespace absl
218 
219 #endif // ABSL_STRINGS_STR_REPLACE_H_
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
pos
int pos
Definition: libuv/docs/code/tty-gravity/main.c:11
get
absl::string_view get(const Cont &c)
Definition: abseil-cpp/absl/strings/str_replace_test.cc:185
absl::strings_internal::FindSubstitutions
std::vector< ViableSubstitution > FindSubstitutions(absl::string_view s, const StrToStrMapping &replacements)
Definition: abseil-cpp/absl/strings/str_replace.h:158
y
const double y
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3611
absl::strings_internal::ViableSubstitution::OccursBefore
bool OccursBefore(const ViableSubstitution &y) const
Definition: abseil-cpp/absl/strings/str_replace.h:147
absl::strings_internal::ViableSubstitution
Definition: abseil-cpp/absl/strings/str_replace.h:136
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::FormatConversionChar::s
@ s
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
ABSL_MUST_USE_RESULT
#define ABSL_MUST_USE_RESULT
Definition: abseil-cpp/absl/base/attributes.h:441
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::string_view::size
constexpr size_type size() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:277
absl::strings_internal::ViableSubstitution::offset
size_t offset
Definition: abseil-cpp/absl/strings/str_replace.h:139
absl::strings_internal::ViableSubstitution::ViableSubstitution
ViableSubstitution(absl::string_view old_str, absl::string_view replacement_str, size_t offset_val)
Definition: abseil-cpp/absl/strings/str_replace.h:141
std::swap
void swap(Json::Value &a, Json::Value &b)
Specialize std::swap() for Json::Value.
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:1226
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
absl::StrReplaceAll
std::string StrReplaceAll(absl::string_view s, strings_internal::FixedMapping replacements)
Definition: abseil-cpp/absl/strings/str_replace.cc:71
rep
const CordRep * rep
Definition: cord_analysis.cc:53
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::strings_internal::ViableSubstitution::replacement
absl::string_view replacement
Definition: abseil-cpp/absl/strings/str_replace.h:138
subs
template_param_type subs
Definition: cxa_demangle.cpp:4906
absl::strings_internal::ApplySubstitutions
int ApplySubstitutions(absl::string_view s, std::vector< strings_internal::ViableSubstitution > *subs_ptr, std::string *result_ptr)
Definition: abseil-cpp/absl/strings/str_replace.cc:29
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
binary_size.old
string old
Definition: binary_size.py:128
offset
voidpf uLong offset
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:142
absl::strings_internal::ViableSubstitution::old
absl::string_view old
Definition: abseil-cpp/absl/strings/str_replace.h:137


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:20