Skip to content

Commit a95fe78

Browse files
author
ochafik
committed
renaming: string_find_partial_stop (moved to common.cpp)
1 parent 6dcff43 commit a95fe78

File tree

4 files changed

+24
-25
lines changed

4 files changed

+24
-25
lines changed

common/common.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,26 @@ void string_replace_all(std::string & s, const std::string & search, const std::
482482
s = std::move(builder);
483483
}
484484

485+
bool string_ends_with(const std::string & str, const std::string & suffix) {
486+
return str.size() >= suffix.size() && str.compare(str.size()-suffix.size(), suffix.size(), suffix) == 0;
487+
}
488+
489+
size_t string_find_partial_stop(const std::string &str, const std::string &stop) {
490+
if (!str.empty() && !stop.empty()) {
491+
const char text_last_char = str.back();
492+
for (int64_t char_index = stop.size() - 1; char_index >= 0; char_index--) {
493+
if (stop[char_index] == text_last_char) {
494+
const std::string current_partial = stop.substr(0, char_index + 1);
495+
if (string_ends_with(str, current_partial)) {
496+
return str.size() - char_index - 1;
497+
}
498+
}
499+
}
500+
}
501+
502+
return std::string::npos;
503+
}
504+
485505
std::string regex_escape(const std::string & s) {
486506
static const std::regex special_chars("[.^$|()*+?\\[\\]{}\\\\]");
487507
return std::regex_replace(s, special_chars, "\\$0");

common/common.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,9 @@ static bool string_starts_with(const std::string & str,
505505
return str.rfind(prefix, 0) == 0;
506506
}
507507

508-
static bool string_ends_with(const std::string & str,
509-
const std::string & suffix) { // While we wait for C++20's std::string::ends_with...
510-
return str.size() >= suffix.size() && str.compare(str.size()-suffix.size(), suffix.size(), suffix) == 0;
511-
}
508+
// While we wait for C++20's std::string::ends_with...
509+
bool string_ends_with(const std::string & str, const std::string & suffix);
510+
size_t string_find_partial_stop(const std::string &str, const std::string &stop);
512511

513512
bool string_parse_kv_override(const char * data, std::vector<llama_model_kv_override> & overrides);
514513
void string_process_escapes(std::string & input);

examples/server/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ struct server_slot {
13921392
pos = text.find(word, from_pos);
13931393
} else {
13941394
// otherwise, partial stop
1395-
pos = find_partial_stop_string(word, text);
1395+
pos = string_find_partial_stop(text, word);
13961396
}
13971397

13981398
if (pos != std::string::npos && (stop_pos == std::string::npos || pos < stop_pos)) {

examples/server/utils.hpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -443,26 +443,6 @@ static std::string gen_tool_call_id() {
443443
// other common utils
444444
//
445445

446-
static bool ends_with(const std::string & str, const std::string & suffix) {
447-
return str.size() >= suffix.size() && 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
448-
}
449-
450-
static size_t find_partial_stop_string(const std::string &stop, const std::string &text) {
451-
if (!text.empty() && !stop.empty()) {
452-
const char text_last_char = text.back();
453-
for (int64_t char_index = stop.size() - 1; char_index >= 0; char_index--) {
454-
if (stop[char_index] == text_last_char) {
455-
const std::string current_partial = stop.substr(0, char_index + 1);
456-
if (ends_with(text, current_partial)) {
457-
return text.size() - char_index - 1;
458-
}
459-
}
460-
}
461-
}
462-
463-
return std::string::npos;
464-
}
465-
466446
// TODO: reuse llama_detokenize
467447
template <class Iter>
468448
static std::string tokens_to_str(llama_context * ctx, Iter begin, Iter end) {

0 commit comments

Comments
 (0)