Skip to content

Commit

Permalink
llama : better replace_all (ggerganov#8852)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggerganov authored and arthw committed Aug 7, 2024
1 parent 98980b8 commit 2c92953
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/llama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 2c92953

Please sign in to comment.