Skip to content

Commit 9b620e5

Browse files
author
ochafik
committed
string utils: use string_views
1 parent b275da3 commit 9b620e5

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

common/common.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,16 +443,15 @@ void string_replace_all(std::string & s, const std::string & search, const std::
443443
s = std::move(builder);
444444
}
445445

446-
bool string_ends_with(const std::string & str, const std::string & suffix) {
446+
bool string_ends_with(const std::string_view & str, const std::string_view & suffix) {
447447
return str.size() >= suffix.size() && str.compare(str.size()-suffix.size(), suffix.size(), suffix) == 0;
448448
}
449-
450-
size_t string_find_partial_stop(const std::string & str, const std::string & stop) {
449+
size_t string_find_partial_stop(const std::string_view & str, const std::string_view & stop) {
451450
if (!str.empty() && !stop.empty()) {
452451
const char text_last_char = str.back();
453452
for (int64_t char_index = stop.size() - 1; char_index >= 0; char_index--) {
454453
if (stop[char_index] == text_last_char) {
455-
const std::string current_partial = stop.substr(0, char_index + 1);
454+
const auto current_partial = stop.substr(0, char_index + 1);
456455
if (string_ends_with(str, current_partial)) {
457456
return str.size() - char_index - 1;
458457
}

common/common.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <set>
88
#include <string>
9+
#include <string_view>
910
#include <vector>
1011
#include <sstream>
1112

@@ -500,8 +501,8 @@ static bool string_starts_with(const std::string & str,
500501
}
501502

502503
// While we wait for C++20's std::string::ends_with...
503-
bool string_ends_with(const std::string & str, const std::string & suffix);
504-
size_t string_find_partial_stop(const std::string &str, const std::string &stop);
504+
bool string_ends_with(const std::string_view & str, const std::string_view & suffix);
505+
size_t string_find_partial_stop(const std::string_view & str, const std::string_view & stop);
505506

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

0 commit comments

Comments
 (0)