llama : better replace_all (cont) (#8926)

* llama : better replace_all (cont)

ggml-ci

* code : deduplicate replace_all

ggml-ci
This commit is contained in:
Georgi Gerganov 2024-08-09 18:23:52 +03:00 committed by GitHub
parent 3071c0a5f2
commit 45a55b91aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 35 additions and 49 deletions

View file

@ -210,17 +210,14 @@ static std::string gguf_data_to_str(enum gguf_type type, const void * data, int
}
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 std::string gguf_kv_to_str(const struct gguf_context * ctx_gguf, int i) {