Skip to content

Commit f1ea514

Browse files
authored
llama : better replace_all (#8852)
1 parent 064cdc2 commit f1ea514

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/llama.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,14 @@ static std::string trim(const std::string & str) {
122122
}
123123

124124
static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
125-
std::string result;
126-
for (size_t pos = 0; ; pos += search.length()) {
127-
auto new_pos = s.find(search, pos);
128-
if (new_pos == std::string::npos) {
129-
result += s.substr(pos, s.size() - pos);
130-
break;
131-
}
132-
result += s.substr(pos, new_pos - pos) + replace;
133-
pos = new_pos;
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();
134132
}
135-
s = std::move(result);
136133
}
137134

138135
static bool is_float_close(float a, float b, float abs_tol) {

0 commit comments

Comments
 (0)