summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKawrakow <iwankawrakow@gmail.com>2024-09-28 17:59:47 +0300
committerGitHub <noreply@github.com>2024-09-28 17:59:47 +0300
commit1b789c983ac34679de67174662b25fb1227cebf2 (patch)
tree005edec88a275621abc41bcee7ca26d5fbd31469
parent7abcc6cc0b0a48b780bb0877e4720c46a7e3c255 (diff)
Time to fix replace_all (#68)
Co-authored-by: Iwan Kawrakow <iwan.kawrakow@gmail.com>
-rw-r--r--src/llama-impl.h14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/llama-impl.h b/src/llama-impl.h
index 399b134a..95277409 100644
--- a/src/llama-impl.h
+++ b/src/llama-impl.h
@@ -31,11 +31,17 @@ void llama_log_callback_default(ggml_log_level level, const char * text, void *
static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
if (search.empty()) {
- return; // Avoid infinite loop if 'search' is an empty string
+ return;
}
+ std::string builder;
+ builder.reserve(s.length());
size_t pos = 0;
- while ((pos = s.find(search, pos)) != std::string::npos) {
- s.replace(pos, search.length(), replace);
- pos += replace.length();
+ size_t last_pos = 0;
+ while ((pos = s.find(search, last_pos)) != std::string::npos) {
+ builder.append(s, last_pos, pos - last_pos);
+ builder.append(replace);
+ last_pos = pos + search.length();
}
+ builder.append(s, last_pos, std::string::npos);
+ s = std::move(builder);
}