Skip to content

Commit efea348

Browse files
ggerganovarthw
authored andcommitted
llama : better replace_all (cont) (ggml-org#8926)
* llama : better replace_all (cont) ggml-ci * code : deduplicate replace_all ggml-ci
1 parent 74f8661 commit efea348

File tree

7 files changed

+35
-49
lines changed

7 files changed

+35
-49
lines changed

common/common.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,17 @@ std::string string_get_sortable_timestamp() {
17771777
return std::string(timestamp_no_ns) + "." + std::string(timestamp_ns);
17781778
}
17791779

1780+
void string_replace_all(std::string & s, const std::string & search, const std::string & replace) {
1781+
if (search.empty()) {
1782+
return; // Avoid infinite loop if 'search' is an empty string
1783+
}
1784+
size_t pos = 0;
1785+
while ((pos = s.find(search, pos)) != std::string::npos) {
1786+
s.replace(pos, search.length(), replace);
1787+
pos += replace.length();
1788+
}
1789+
}
1790+
17801791
void string_process_escapes(std::string & input) {
17811792
std::size_t input_len = input.length();
17821793
std::size_t output_idx = 0;

common/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ std::vector<std::string> string_split(std::string input, char separator);
286286
std::string string_strip(const std::string & str);
287287
std::string string_get_sortable_timestamp();
288288

289+
void string_replace_all(std::string & s, const std::string & search, const std::string & replace);
290+
289291
template<class T>
290292
static std::vector<T> string_split(const std::string & str, char delim) {
291293
std::vector<T> values;

examples/export-lora/export-lora.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,6 @@ static struct gguf_context * load_gguf(std::string & fname, struct ggml_context
5050
return ctx_gguf;
5151
}
5252

53-
static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
54-
std::string result;
55-
for (size_t pos = 0; ; pos += search.length()) {
56-
auto new_pos = s.find(search, pos);
57-
if (new_pos == std::string::npos) {
58-
result += s.substr(pos, s.size() - pos);
59-
break;
60-
}
61-
result += s.substr(pos, new_pos - pos) + replace;
62-
pos = new_pos;
63-
}
64-
s = std::move(result);
65-
}
66-
6753
struct file_input {
6854
struct ggml_context * ctx_meta = nullptr;
6955
struct gguf_context * ctx_gguf = nullptr;

examples/llava/clip.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,14 @@ static std::string gguf_data_to_str(enum gguf_type type, const void * data, int
210210
}
211211

212212
static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
213-
std::string result;
214-
for (size_t pos = 0; ; pos += search.length()) {
215-
auto new_pos = s.find(search, pos);
216-
if (new_pos == std::string::npos) {
217-
result += s.substr(pos, s.size() - pos);
218-
break;
219-
}
220-
result += s.substr(pos, new_pos - pos) + replace;
221-
pos = new_pos;
213+
if (search.empty()) {
214+
return; // Avoid infinite loop if 'search' is an empty string
215+
}
216+
size_t pos = 0;
217+
while ((pos = s.find(search, pos)) != std::string::npos) {
218+
s.replace(pos, search.length(), replace);
219+
pos += replace.length();
222220
}
223-
s = std::move(result);
224221
}
225222

226223
static std::string gguf_kv_to_str(const struct gguf_context * ctx_gguf, int i) {

src/llama-impl.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,18 @@ void llama_log_callback_default(ggml_log_level level, const char * text, void *
2424
#define LLAMA_LOG_INFO(...) llama_log_internal(GGML_LOG_LEVEL_INFO , __VA_ARGS__)
2525
#define LLAMA_LOG_WARN(...) llama_log_internal(GGML_LOG_LEVEL_WARN , __VA_ARGS__)
2626
#define LLAMA_LOG_ERROR(...) llama_log_internal(GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
27+
28+
//
29+
// helpers
30+
//
31+
32+
static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
33+
if (search.empty()) {
34+
return; // Avoid infinite loop if 'search' is an empty string
35+
}
36+
size_t pos = 0;
37+
while ((pos = s.find(search, pos)) != std::string::npos) {
38+
s.replace(pos, search.length(), replace);
39+
pos += replace.length();
40+
}
41+
}

src/llama-vocab.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@
1616
// helpers
1717
//
1818

19-
static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
20-
std::string result;
21-
for (size_t pos = 0; ; pos += search.length()) {
22-
auto new_pos = s.find(search, pos);
23-
if (new_pos == std::string::npos) {
24-
result += s.substr(pos, s.size() - pos);
25-
break;
26-
}
27-
result += s.substr(pos, new_pos - pos) + replace;
28-
pos = new_pos;
29-
}
30-
s = std::move(result);
31-
}
32-
3319
LLAMA_ATTRIBUTE_FORMAT(1, 2)
3420
static std::string format(const char * fmt, ...) {
3521
va_list ap;

src/llama.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,6 @@ static std::string trim(const std::string & str) {
121121
return str.substr(start, end - start);
122122
}
123123

124-
static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
125-
if (search.empty()) {
126-
return; // Avoid infinite loop if 'search' is an empty string
127-
}
128-
size_t pos = 0;
129-
while ((pos = s.find(search, pos)) != std::string::npos) {
130-
s.replace(pos, search.length(), replace);
131-
pos += replace.length();
132-
}
133-
}
134-
135124
static bool is_float_close(float a, float b, float abs_tol) {
136125
// Check for non-negative tolerance
137126
if (abs_tol < 0.0) {

0 commit comments

Comments
 (0)