forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_utils.h
More file actions
203 lines (173 loc) · 5.55 KB
/
string_utils.h
File metadata and controls
203 lines (173 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#pragma once
#include <algorithm>
#include <cctype>
#include <chrono>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
namespace string_utils {
struct ParsePromptResult {
std::string user_prompt;
std::string system_prompt;
std::string ai_prompt;
};
inline std::string RTrim(const std::string& str) {
size_t end = str.find_last_not_of("\n\t ");
return (end == std::string::npos) ? "" : str.substr(0, end + 1);
}
inline void Trim(std::string& s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
s.erase(std::find_if(s.rbegin(), s.rend(),
[](unsigned char ch) { return !std::isspace(ch); })
.base(),
s.end());
}
inline std::string RemoveSubstring(std::string_view full_str,
std::string_view to_remove) {
if (to_remove.empty()) {
return std::string(full_str);
}
std::string result;
result.reserve(full_str.length());
size_t pos = 0;
size_t prev = 0;
// Find each occurrence and copy only the parts we want to keep
while ((pos = full_str.find(to_remove, prev)) != std::string_view::npos) {
result.append(full_str.substr(prev, pos - prev));
prev = pos + to_remove.length();
}
// Append the remaining part
result.append(full_str.substr(prev));
return result;
}
inline bool StringContainsIgnoreCase(const std::string& haystack,
const std::string& needle) {
if (needle.empty()) {
return true;
}
if (haystack.length() < needle.length()) {
return false;
}
auto it =
std::search(haystack.begin(), haystack.end(), needle.begin(),
needle.end(), [](char ch1, char ch2) {
return std::tolower(static_cast<unsigned char>(ch1)) ==
std::tolower(static_cast<unsigned char>(ch2));
});
return it != haystack.end();
}
inline bool EqualsIgnoreCase(const std::string& a, const std::string& b) {
return std::equal(a.begin(), a.end(), b.begin(), b.end(),
[](char a, char b) { return tolower(a) == tolower(b); });
}
inline ParsePromptResult ParsePrompt(const std::string& prompt) {
auto& pt = prompt;
ParsePromptResult result;
result.user_prompt =
pt.substr(pt.find_first_of('}') + 1,
pt.find_last_of('{') - pt.find_first_of('}') - 1);
result.ai_prompt = pt.substr(pt.find_last_of('}') + 1);
result.system_prompt = pt.substr(0, pt.find_first_of('{'));
return result;
}
inline bool StartsWith(const std::string& str, const std::string& prefix) {
return str.rfind(prefix, 0) == 0;
}
inline void SortStrings(std::vector<std::string>& strings) {
std::sort(strings.begin(), strings.end());
}
inline bool EndsWith(const std::string& str, const std::string& suffix) {
if (str.length() >= suffix.length()) {
return (0 == str.compare(str.length() - suffix.length(), suffix.length(),
suffix));
}
return false;
}
inline std::vector<std::string> SplitBy(const std::string& str,
const std::string&& delimiter) {
std::vector<std::string> tokens;
size_t prev = 0, pos = 0;
do {
pos = str.find(delimiter, prev);
if (pos == std::string::npos)
pos = str.length();
std::string token = str.substr(prev, pos - prev);
if (!token.empty())
tokens.push_back(token);
prev = pos + delimiter.length();
} while (pos < str.length() && prev < str.length());
return tokens;
}
inline uint64_t getCurrentTimeInMilliseconds() {
using namespace std::chrono;
return duration_cast<milliseconds>(system_clock::now().time_since_epoch())
.count();
}
inline std::string FormatTimeElapsed(uint64_t pastTimestamp) {
uint64_t currentTimestamp = getCurrentTimeInMilliseconds();
uint64_t milliseconds = currentTimestamp - pastTimestamp;
// Constants for time units
const uint64_t millisInSecond = 1000;
const uint64_t millisInMinute = millisInSecond * 60;
const uint64_t millisInHour = millisInMinute * 60;
const uint64_t millisInDay = millisInHour * 24;
uint64_t days = milliseconds / millisInDay;
milliseconds %= millisInDay;
uint64_t hours = milliseconds / millisInHour;
milliseconds %= millisInHour;
uint64_t minutes = milliseconds / millisInMinute;
milliseconds %= millisInMinute;
uint64_t seconds = milliseconds / millisInSecond;
std::ostringstream oss;
if (days > 0) {
oss << days << " day" << (days > 1 ? "s" : "") << ", ";
}
if (hours > 0 || days > 0) {
oss << hours << " hour" << (hours > 1 ? "s" : "") << ", ";
}
if (minutes > 0 || hours > 0 || days > 0) {
oss << minutes << " minute" << (minutes > 1 ? "s" : "") << ", ";
}
oss << seconds << " second" << (seconds > 1 ? "s" : "");
return oss.str();
}
inline std::string EscapeJson(const std::string& s) {
std::ostringstream o;
for (auto c = s.cbegin(); c != s.cend(); c++) {
switch (*c) {
case '"':
o << "\\\"";
break;
case '\\':
o << "\\\\";
break;
case '\b':
o << "\\b";
break;
case '\f':
o << "\\f";
break;
case '\n':
o << "\\n";
break;
case '\r':
o << "\\r";
break;
case '\t':
o << "\\t";
break;
default:
if ('\x00' <= *c && *c <= '\x1f') {
o << "\\u" << std::hex << std::setw(4) << std::setfill('0')
<< static_cast<int>(*c);
} else {
o << *c;
}
}
}
return o.str();
}
} // namespace string_utils