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 
16 
17 #include "absl/strings/str_cat.h"
18 
19 namespace absl {
20 namespace strings_internal {
21 
22 using FixedMapping =
23  std::initializer_list<std::pair<absl::string_view, absl::string_view>>;
24 
25 // Applies the ViableSubstitutions in subs_ptr to the absl::string_view s, and
26 // stores the result in *result_ptr. Returns the number of substitutions that
27 // occurred.
30  std::vector<strings_internal::ViableSubstitution>* subs_ptr,
31  std::string* result_ptr) {
32  auto& subs = *subs_ptr;
33  int substitutions = 0;
34  size_t pos = 0;
35  while (!subs.empty()) {
36  auto& sub = subs.back();
37  if (sub.offset >= pos) {
38  if (pos <= s.size()) {
39  StrAppend(result_ptr, s.substr(pos, sub.offset - pos), sub.replacement);
40  }
41  pos = sub.offset + sub.old.size();
42  substitutions += 1;
43  }
44  sub.offset = s.find(sub.old, pos);
45  if (sub.offset == s.npos) {
46  subs.pop_back();
47  } else {
48  // Insertion sort to ensure the last ViableSubstitution continues to be
49  // before all the others.
50  size_t index = subs.size();
51  while (--index && subs[index - 1].OccursBefore(subs[index])) {
52  std::swap(subs[index], subs[index - 1]);
53  }
54  }
55  }
56  result_ptr->append(s.data() + pos, s.size() - pos);
57  return substitutions;
58 }
59 
60 } // namespace strings_internal
61 
62 // We can implement this in terms of the generic StrReplaceAll, but
63 // we must specify the template overload because C++ cannot deduce the type
64 // of an initializer_list parameter to a function, and also if we don't specify
65 // the type, we just call ourselves.
66 //
67 // Note that we implement them here, rather than in the header, so that they
68 // aren't inlined.
69 
71  strings_internal::FixedMapping replacements) {
72  return StrReplaceAll<strings_internal::FixedMapping>(s, replacements);
73 }
74 
76  std::string* target) {
77  return StrReplaceAll<strings_internal::FixedMapping>(replacements, target);
78 }
79 
80 } // namespace absl
void StrAppend(std::string *dest, const AlphaNum &a)
Definition: str_cat.cc:193
string_view substr(size_type pos, size_type n=npos) const
Definition: string_view.h:354
size_type find(string_view s, size_type pos=0) const noexcept
Definition: string_view.cc:92
static constexpr size_type npos
Definition: string_view.h:159
Definition: algorithm.h:29
constexpr size_type size() const noexcept
Definition: string_view.h:260
std::initializer_list< std::pair< absl::string_view, absl::string_view > > FixedMapping
Definition: str_replace.cc:23
void swap(absl::InlinedVector< T, N, A > &a, absl::InlinedVector< T, N, A > &b) noexcept(noexcept(a.swap(b)))
constexpr const_pointer data() const noexcept
Definition: string_view.h:302
int ApplySubstitutions(absl::string_view s, std::vector< strings_internal::ViableSubstitution > *subs_ptr, std::string *result_ptr)
Definition: str_replace.cc:28
std::string StrReplaceAll(absl::string_view s, strings_internal::FixedMapping replacements)
Definition: str_replace.cc:70


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:20