Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/strings/str_replace.h"
00016
00017 #include "absl/strings/str_cat.h"
00018
00019 namespace absl {
00020 namespace strings_internal {
00021
00022 using FixedMapping =
00023 std::initializer_list<std::pair<absl::string_view, absl::string_view>>;
00024
00025
00026
00027
00028 int ApplySubstitutions(
00029 absl::string_view s,
00030 std::vector<strings_internal::ViableSubstitution>* subs_ptr,
00031 std::string* result_ptr) {
00032 auto& subs = *subs_ptr;
00033 int substitutions = 0;
00034 size_t pos = 0;
00035 while (!subs.empty()) {
00036 auto& sub = subs.back();
00037 if (sub.offset >= pos) {
00038 if (pos <= s.size()) {
00039 StrAppend(result_ptr, s.substr(pos, sub.offset - pos), sub.replacement);
00040 }
00041 pos = sub.offset + sub.old.size();
00042 substitutions += 1;
00043 }
00044 sub.offset = s.find(sub.old, pos);
00045 if (sub.offset == s.npos) {
00046 subs.pop_back();
00047 } else {
00048
00049
00050 size_t index = subs.size();
00051 while (--index && subs[index - 1].OccursBefore(subs[index])) {
00052 std::swap(subs[index], subs[index - 1]);
00053 }
00054 }
00055 }
00056 result_ptr->append(s.data() + pos, s.size() - pos);
00057 return substitutions;
00058 }
00059
00060 }
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 std::string StrReplaceAll(absl::string_view s,
00071 strings_internal::FixedMapping replacements) {
00072 return StrReplaceAll<strings_internal::FixedMapping>(s, replacements);
00073 }
00074
00075 int StrReplaceAll(strings_internal::FixedMapping replacements,
00076 std::string* target) {
00077 return StrReplaceAll<strings_internal::FixedMapping>(replacements, target);
00078 }
00079
00080 }