20 #include "benchmark/benchmark.h" 25 std::string* big_string;
26 std::string* after_replacing_the;
27 std::string* after_replacing_many;
31 const char* replacement;
35 {
"jumped",
"liquored"},
53 if (big_string ==
nullptr) {
55 big_string =
new std::string(1000 * 1000,
' ');
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());
67 after_replacing_the =
new std::string(*big_string);
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);
95 void BM_StrReplaceAllOneReplacement(benchmark::State& state) {
97 std::string src = *big_string;
98 for (
auto _ : state) {
101 "not benchmarking intended behavior");
104 BENCHMARK(BM_StrReplaceAllOneReplacement);
106 void BM_StrReplaceAll(benchmark::State& state) {
108 std::string src = *big_string;
109 for (
auto _ : state) {
112 {
"jumped",
"liquored"},
115 {
"liquor",
"shakes"}});
117 "not benchmarking intended behavior");
120 BENCHMARK(BM_StrReplaceAll);
#define ABSL_RAW_CHECK(condition, message)
std::string StrReplaceAll(absl::string_view s, strings_internal::FixedMapping replacements)