abseil-cpp/absl/strings/str_replace.cc
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/strings/str_replace.h"
16 
17 #include "absl/strings/str_cat.h"
18 
19 namespace absl {
21 namespace strings_internal {
22 
23 using FixedMapping =
24  std::initializer_list<std::pair<absl::string_view, absl::string_view>>;
25 
26 // Applies the ViableSubstitutions in subs_ptr to the absl::string_view s, and
27 // stores the result in *result_ptr. Returns the number of substitutions that
28 // occurred.
31  std::vector<strings_internal::ViableSubstitution>* subs_ptr,
32  std::string* result_ptr) {
33  auto& subs = *subs_ptr;
34  int substitutions = 0;
35  size_t pos = 0;
36  while (!subs.empty()) {
37  auto& sub = subs.back();
38  if (sub.offset >= pos) {
39  if (pos <= s.size()) {
40  StrAppend(result_ptr, s.substr(pos, sub.offset - pos), sub.replacement);
41  }
42  pos = sub.offset + sub.old.size();
43  substitutions += 1;
44  }
45  sub.offset = s.find(sub.old, pos);
46  if (sub.offset == s.npos) {
47  subs.pop_back();
48  } else {
49  // Insertion sort to ensure the last ViableSubstitution continues to be
50  // before all the others.
51  size_t index = subs.size();
52  while (--index && subs[index - 1].OccursBefore(subs[index])) {
53  std::swap(subs[index], subs[index - 1]);
54  }
55  }
56  }
57  result_ptr->append(s.data() + pos, s.size() - pos);
58  return substitutions;
59 }
60 
61 } // namespace strings_internal
62 
63 // We can implement this in terms of the generic StrReplaceAll, but
64 // we must specify the template overload because C++ cannot deduce the type
65 // of an initializer_list parameter to a function, and also if we don't specify
66 // the type, we just call ourselves.
67 //
68 // Note that we implement them here, rather than in the header, so that they
69 // aren't inlined.
70 
72  strings_internal::FixedMapping replacements) {
73  return StrReplaceAll<strings_internal::FixedMapping>(s, replacements);
74 }
75 
78  return StrReplaceAll<strings_internal::FixedMapping>(replacements, target);
79 }
80 
82 } // namespace absl
pos
int pos
Definition: libuv/docs/code/tty-gravity/main.c:11
absl::StrAppend
void StrAppend(std::string *dest, const AlphaNum &a)
Definition: abseil-cpp/absl/strings/str_cat.cc:193
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_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
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
absl::strings_internal::FixedMapping
std::initializer_list< std::pair< absl::string_view, absl::string_view > > FixedMapping
Definition: abseil-cpp/absl/strings/str_replace.cc:24
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
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


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