15 #include "absl/strings/str_replace.h"
20 #include "benchmark/benchmark.h"
21 #include "absl/base/internal/raw_logging.h"
31 const char* replacement;
35 {
"jumped",
"liquored"},
53 if (big_string ==
nullptr) {
56 for (
std::string phrase : {
"the quick brown fox jumped over the lazy dogs",
57 "pack my box with the five dozen liquor jugs"}) {
58 for (
int i = 0;
i < 10 * 1000; ++
i) {
60 memcpy(&(*big_string)[
r % (big_string->size() - phrase.size())],
61 phrase.data(), phrase.size());
69 (
pos = after_replacing_the->find(
"the",
pos)) != std::string::npos;) {
70 memcpy(&(*after_replacing_the)[
pos],
"box", 3);
73 after_replacing_many =
new std::string(*big_string);
74 for (
size_t pos = 0;;) {
75 size_t next_pos =
static_cast<size_t>(-1);
76 const char* needle_string =
nullptr;
77 const char* replacement_string =
nullptr;
78 for (
const auto&
r : replacements) {
79 auto needlepos = after_replacing_many->find(
r.needle,
pos);
80 if (needlepos != std::string::npos && needlepos < next_pos) {
82 needle_string =
r.needle;
83 replacement_string =
r.replacement;
86 if (next_pos > after_replacing_many->size())
break;
87 after_replacing_many->replace(next_pos, strlen(needle_string),
89 next_pos += strlen(replacement_string);
101 "not benchmarking intended behavior");
104 BENCHMARK(BM_StrReplaceAllOneReplacement);
112 {
"jumped",
"liquored"},
115 {
"liquor",
"shakes"}});
117 "not benchmarking intended behavior");