str_replace.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #include <string>
  41. #include <utility>
  42. #include <vector>
  43. #include "absl/base/attributes.h"
  44. #include "absl/strings/string_view.h"
  45. namespace absl {
  46. ABSL_NAMESPACE_BEGIN
  47. // StrReplaceAll()
  48. //
  49. // Replaces character sequences within a given string with replacements provided
  50. // within an initializer list of key/value pairs. Candidate replacements are
  51. // considered in order as they occur within the string, with earlier matches
  52. // taking precedence, and longer matches taking precedence for candidates
  53. // starting at the same position in the string. Once a substitution is made, the
  54. // replaced text is not considered for any further substitutions.
  55. //
  56. // Example:
  57. //
  58. // std::string s = absl::StrReplaceAll(
  59. // "$who bought $count #Noun. Thanks $who!",
  60. // {{"$count", absl::StrCat(5)},
  61. // {"$who", "Bob"},
  62. // {"#Noun", "Apples"}});
  63. // EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
  64. ABSL_MUST_USE_RESULT std::string StrReplaceAll(
  65. absl::string_view s,
  66. std::initializer_list<std::pair<absl::string_view, absl::string_view>>
  67. replacements);
  68. // Overload of `StrReplaceAll()` to accept a container of key/value replacement
  69. // pairs (typically either an associative map or a `std::vector` of `std::pair`
  70. // elements). A vector of pairs is generally more efficient.
  71. //
  72. // Examples:
  73. //
  74. // std::map<const absl::string_view, const absl::string_view> replacements;
  75. // replacements["$who"] = "Bob";
  76. // replacements["$count"] = "5";
  77. // replacements["#Noun"] = "Apples";
  78. // std::string s = absl::StrReplaceAll(
  79. // "$who bought $count #Noun. Thanks $who!",
  80. // replacements);
  81. // EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
  82. //
  83. // // A std::vector of std::pair elements can be more efficient.
  84. // std::vector<std::pair<const absl::string_view, std::string>> replacements;
  85. // replacements.push_back({"&", "&amp;"});
  86. // replacements.push_back({"<", "&lt;"});
  87. // replacements.push_back({">", "&gt;"});
  88. // std::string s = absl::StrReplaceAll("if (ptr < &foo)",
  89. // replacements);
  90. // EXPECT_EQ("if (ptr &lt; &amp;foo)", s);
  91. template <typename StrToStrMapping>
  92. std::string StrReplaceAll(absl::string_view s,
  93. const StrToStrMapping& replacements);
  94. // Overload of `StrReplaceAll()` to replace character sequences within a given
  95. // output string *in place* with replacements provided within an initializer
  96. // list of key/value pairs, returning the number of substitutions that occurred.
  97. //
  98. // Example:
  99. //
  100. // std::string s = std::string("$who bought $count #Noun. Thanks $who!");
  101. // int count;
  102. // count = absl::StrReplaceAll({{"$count", absl::StrCat(5)},
  103. // {"$who", "Bob"},
  104. // {"#Noun", "Apples"}}, &s);
  105. // EXPECT_EQ(count, 4);
  106. // EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
  107. int StrReplaceAll(
  108. std::initializer_list<std::pair<absl::string_view, absl::string_view>>
  109. replacements,
  110. std::string* target);
  111. // Overload of `StrReplaceAll()` to replace patterns within a given output
  112. // string *in place* with replacements provided within a container of key/value
  113. // pairs.
  114. //
  115. // Example:
  116. //
  117. // std::string s = std::string("if (ptr < &foo)");
  118. // int count = absl::StrReplaceAll({{"&", "&amp;"},
  119. // {"<", "&lt;"},
  120. // {">", "&gt;"}}, &s);
  121. // EXPECT_EQ(count, 2);
  122. // EXPECT_EQ("if (ptr &lt; &amp;foo)", s);
  123. template <typename StrToStrMapping>
  124. int StrReplaceAll(const StrToStrMapping& replacements, std::string* target);
  125. // Implementation details only, past this point.
  126. namespace strings_internal {
  127. struct ViableSubstitution {
  128. absl::string_view old;
  129. absl::string_view replacement;
  130. size_t offset;
  131. ViableSubstitution(absl::string_view old_str,
  132. absl::string_view replacement_str, size_t offset_val)
  133. : old(old_str), replacement(replacement_str), offset(offset_val) {}
  134. // One substitution occurs "before" another (takes priority) if either
  135. // it has the lowest offset, or it has the same offset but a larger size.
  136. bool OccursBefore(const ViableSubstitution& y) const {
  137. if (offset != y.offset) return offset < y.offset;
  138. return old.size() > y.old.size();
  139. }
  140. };
  141. // Build a vector of ViableSubstitutions based on the given list of
  142. // replacements. subs can be implemented as a priority_queue. However, it turns
  143. // out that most callers have small enough a list of substitutions that the
  144. // overhead of such a queue isn't worth it.
  145. template <typename StrToStrMapping>
  146. std::vector<ViableSubstitution> FindSubstitutions(
  147. absl::string_view s, const StrToStrMapping& replacements) {
  148. std::vector<ViableSubstitution> subs;
  149. subs.reserve(replacements.size());
  150. for (const auto& rep : replacements) {
  151. using std::get;
  152. absl::string_view old(get<0>(rep));
  153. size_t pos = s.find(old);
  154. if (pos == s.npos) continue;
  155. // Ignore attempts to replace "". This condition is almost never true,
  156. // but above condition is frequently true. That's why we test for this
  157. // now and not before.
  158. if (old.empty()) continue;
  159. subs.emplace_back(old, get<1>(rep), pos);
  160. // Insertion sort to ensure the last ViableSubstitution comes before
  161. // all the others.
  162. size_t index = subs.size();
  163. while (--index && subs[index - 1].OccursBefore(subs[index])) {
  164. std::swap(subs[index], subs[index - 1]);
  165. }
  166. }
  167. return subs;
  168. }
  169. int ApplySubstitutions(absl::string_view s,
  170. std::vector<ViableSubstitution>* subs_ptr,
  171. std::string* result_ptr);
  172. } // namespace strings_internal
  173. template <typename StrToStrMapping>
  174. std::string StrReplaceAll(absl::string_view s,
  175. const StrToStrMapping& replacements) {
  176. auto subs = strings_internal::FindSubstitutions(s, replacements);
  177. std::string result;
  178. result.reserve(s.size());
  179. strings_internal::ApplySubstitutions(s, &subs, &result);
  180. return result;
  181. }
  182. template <typename StrToStrMapping>
  183. int StrReplaceAll(const StrToStrMapping& replacements, std::string* target) {
  184. auto subs = strings_internal::FindSubstitutions(*target, replacements);
  185. if (subs.empty()) return 0;
  186. std::string result;
  187. result.reserve(target->size());
  188. int substitutions =
  189. strings_internal::ApplySubstitutions(*target, &subs, &result);
  190. target->swap(result);
  191. return substitutions;
  192. }
  193. ABSL_NAMESPACE_END
  194. } // namespace absl
  195. #endif // ABSL_STRINGS_STR_REPLACE_H_