str_replace.cc 3.0 KB

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