From 1b789c983ac34679de67174662b25fb1227cebf2 Mon Sep 17 00:00:00 2001 From: Kawrakow Date: Sat, 28 Sep 2024 17:59:47 +0300 Subject: Time to fix replace_all (#68) Co-authored-by: Iwan Kawrakow --- src/llama-impl.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/llama-impl.h') 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); } -- cgit v1.2.3