]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
llama : better replace_all (#8852)
authorGeorgi Gerganov <redacted>
Mon, 5 Aug 2024 05:53:39 +0000 (08:53 +0300)
committerGitHub <redacted>
Mon, 5 Aug 2024 05:53:39 +0000 (08:53 +0300)
src/llama.cpp

index e6f303d31b3bff99fa28c78c1818e0d0d1f23aeb..ff234565d076f731e4edf48dde8f58841e768f81 100644 (file)
@@ -122,17 +122,14 @@ static std::string trim(const std::string & str) {
 }
 
 static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
-    std::string result;
-    for (size_t pos = 0; ; pos += search.length()) {
-        auto new_pos = s.find(search, pos);
-        if (new_pos == std::string::npos) {
-            result += s.substr(pos, s.size() - pos);
-            break;
-        }
-        result += s.substr(pos, new_pos - pos) + replace;
-        pos = new_pos;
+    if (search.empty()) {
+        return; // Avoid infinite loop if 'search' is an empty string
+    }
+    size_t pos = 0;
+    while ((pos = s.find(search, pos)) != std::string::npos) {
+        s.replace(pos, search.length(), replace);
+        pos += replace.length();
     }
-    s = std::move(result);
 }
 
 static bool is_float_close(float a, float b, float abs_tol) {