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