str_replace.cc 2.7 KB

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