llama : better replace_all (cont)
ggml-ci
This commit is contained in:
parent
ebd541a570
commit
59952cba13
3 changed files with 21 additions and 30 deletions
|
@ -51,17 +51,14 @@ static struct gguf_context * load_gguf(std::string & fname, struct ggml_context
|
||||||
}
|
}
|
||||||
|
|
||||||
static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
|
static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
|
||||||
std::string result;
|
if (search.empty()) {
|
||||||
for (size_t pos = 0; ; pos += search.length()) {
|
return; // Avoid infinite loop if 'search' is an empty string
|
||||||
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;
|
size_t pos = 0;
|
||||||
pos = new_pos;
|
while ((pos = s.find(search, pos)) != std::string::npos) {
|
||||||
|
s.replace(pos, search.length(), replace);
|
||||||
|
pos += replace.length();
|
||||||
}
|
}
|
||||||
s = std::move(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct file_input {
|
struct file_input {
|
||||||
|
|
|
@ -200,17 +200,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) {
|
static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
|
||||||
std::string result;
|
if (search.empty()) {
|
||||||
for (size_t pos = 0; ; pos += search.length()) {
|
return; // Avoid infinite loop if 'search' is an empty string
|
||||||
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;
|
size_t pos = 0;
|
||||||
pos = new_pos;
|
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) {
|
static std::string gguf_kv_to_str(const struct gguf_context * ctx_gguf, int i) {
|
||||||
|
|
|
@ -17,17 +17,14 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
|
static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
|
||||||
std::string result;
|
if (search.empty()) {
|
||||||
for (size_t pos = 0; ; pos += search.length()) {
|
return; // Avoid infinite loop if 'search' is an empty string
|
||||||
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;
|
size_t pos = 0;
|
||||||
pos = new_pos;
|
while ((pos = s.find(search, pos)) != std::string::npos) {
|
||||||
|
s.replace(pos, search.length(), replace);
|
||||||
|
pos += replace.length();
|
||||||
}
|
}
|
||||||
s = std::move(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LLAMA_ATTRIBUTE_FORMAT(1, 2)
|
LLAMA_ATTRIBUTE_FORMAT(1, 2)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue