Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 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 | // {"<", "<"}, |
| 35 | // {">", ">"}, |
| 36 | // {"\"", """}, |
| 37 | // {"'", "'"}}); |
| 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 { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 49 | ABSL_NAMESPACE_BEGIN |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 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); |
| 68 | ABSL_MUST_USE_RESULT std::string StrReplaceAll( |
| 69 | absl::string_view 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({"&", "&"}); |
| 91 | // replacements.push_back({"<", "<"}); |
| 92 | // replacements.push_back({">", ">"}); |
| 93 | // std::string s = absl::StrReplaceAll("if (ptr < &foo)", |
| 94 | // replacements); |
| 95 | // EXPECT_EQ("if (ptr < &foo)", s); |
| 96 | template <typename StrToStrMapping> |
| 97 | std::string StrReplaceAll(absl::string_view s, |
| 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, |
| 116 | std::string* target); |
| 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({{"&", "&"}, |
| 126 | // {"<", "<"}, |
| 127 | // {">", ">"}}, &s); |
| 128 | // EXPECT_EQ(count, 2); |
| 129 | // EXPECT_EQ("if (ptr < &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 | |
| 136 | struct ViableSubstitution { |
| 137 | absl::string_view old; |
| 138 | absl::string_view replacement; |
| 139 | size_t offset; |
| 140 | |
| 141 | ViableSubstitution(absl::string_view old_str, |
| 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 | |
| 187 | int ApplySubstitutions(absl::string_view s, |
| 188 | std::vector<ViableSubstitution>* subs_ptr, |
| 189 | std::string* result_ptr); |
| 190 | |
| 191 | } // namespace strings_internal |
| 192 | |
| 193 | template <typename StrToStrMapping> |
| 194 | std::string StrReplaceAll(absl::string_view s, |
| 195 | const StrToStrMapping& replacements) { |
| 196 | auto subs = strings_internal::FindSubstitutions(s, replacements); |
| 197 | std::string result; |
| 198 | result.reserve(s.size()); |
| 199 | strings_internal::ApplySubstitutions(s, &subs, &result); |
| 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 | |
| 208 | std::string result; |
| 209 | result.reserve(target->size()); |
| 210 | int substitutions = |
| 211 | strings_internal::ApplySubstitutions(*target, &subs, &result); |
| 212 | target->swap(result); |
| 213 | return substitutions; |
| 214 | } |
| 215 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 216 | ABSL_NAMESPACE_END |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 217 | } // namespace absl |
| 218 | |
| 219 | #endif // ABSL_STRINGS_STR_REPLACE_H_ |