16 std::pair<std::string, std::string>
19 auto result = str.find(sep);
20 if(result == std::string::npos)
22 return std::make_pair(str,
"");
25 const auto parent = str.substr(0, result);
26 const auto child = str.substr(result, str.length() - parent.length());
27 return std::make_pair(parent, child);
34 if(str.length() < count) {
return str; }
35 else {
return str.substr(0, count); }
42 if (str.length() > count)
44 return str.substr(0, count) +
"...";
54 auto result = str.find(sep);
55 if(result == std::string::npos)
60 return str.substr(0, result);
65 trim_right(
const std::string& string_to_trim, std::string_view trim_characters)
67 return std::string(string_to_trim).erase(string_to_trim.find_last_not_of(trim_characters) + 1);
72 trim_left(
const std::string& string_to_trim, std::string_view trim_characters)
74 return std::string(string_to_trim).erase(0, string_to_trim.find_first_not_of(trim_characters));
79 trim(
const std::string& string_to_trim, std::string_view trim_characters)
87 begins_with(
const std::string& string_to_test,
const std::string& start)
89 const std::string::size_type length = start.length();
90 const std::string::size_type other_length = string_to_test.length();
91 if(other_length < length)
95 const std::string actual_start = string_to_test.substr(0, length);
96 return start == actual_start;
101 ends_with(
const std::string& string_to_test,
const std::string& end)
103 const std::string::size_type length = end.length();
104 const std::string::size_type other_length = string_to_test.length();
105 if(other_length < length)
109 const std::string actual_end
110 = string_to_test.substr(other_length - length, length);
111 return end == actual_end;
118 if(b >=
'A' && b <=
'Z')
120 return static_cast<char>((
static_cast<int>(b) -
'A') +
'a');
131 if(b >=
'a' && b <=
'z')
133 return static_cast<char>(b + (
'A' -
'a'));
145 std::string result = str;
146 std::transform(result.begin(), result.end(), result.begin(),
to_lower_char);
151 std::vector<std::string>
161 std::string result = str;
162 std::transform(result.begin(), result.end(), result.begin(),
to_upper_char);
170 const auto name = [&]() -> std::string
174 case 0:
return "<null>";
175 case '\n':
return "<\\n>";
176 case '\r':
return "<\\r>";
177 case '\t':
return "<tab>";
179 case 1:
return "<start of heading>";
180 case 2:
return "<start of text>";
181 case 3:
return "<end of text>";
182 case 4:
return "<end of transmission>";
183 case 5:
return "<enquiry>";
184 case 6:
return "<acknowledge>";
185 case 7:
return "<bell>";
186 case 8:
return "<backspace>";
189 case 11:
return "<vertical tab>";
190 case 12:
return "<new page>";
192 case 14:
return "<shift out>";
193 case 15:
return "<shift in>";
194 case 16:
return "<data link esqape>";
195 case 17:
return "<device control 1>";
196 case 18:
return "<device control 2>";
197 case 19:
return "<device control 3>";
198 case 20:
return "<device control 4>";
199 case 21:
return "<negative acknowledge>";
200 case 22:
return "<synchronous idle>";
201 case 23:
return "<end of trans. block>";
202 case 24:
return "<cancel>";
203 case 25:
return "<end of medium>";
204 case 26:
return "<substitute>";
205 case 27:
return "<escape>";
206 case 28:
return "<file separator>";
207 case 29:
return "<group separator>";
208 case 30:
return "<record separator>";
209 case 31:
return "<unit separator>";
210 case 127:
return "<DEL>";
211 case ' ':
return "<space>";
221 constexpr std::string_view smart_characters =
222 "abcdefghijklmnopqrstuwxyz"
223 "ABCDEFGHIJKLMNOPQRSTUWXYZ"
234 return fmt::format(
"{}({:#x})", name, c);
243 std::string::size_type
246 const auto end =
std::min(lhs.size(), rhs.size());
248 std::string::size_type index = 0;
249 for(; index < end; index+=1)
251 if(lhs[index]!=rhs[index])
257 if(index >= lhs.size() && index >= rhs.size())
259 return std::string::npos;
269 replace_all(std::string*
string,
const std::string& to_find,
const std::string& to_replace)
271 std::size_t index =
string->find(to_find);
272 const std::size_t find_length = to_find.length();
274 while(index != std::string::npos)
276 string->erase(index, find_length);
277 string->insert(index, to_replace);
278 index =
string->find(to_find, index);
284 replace_all(
const std::string&
string,
const std::string& to_find,
const std::string& to_replace)
286 std::string temp = string;
293 copy(
char* dst,
const std::string& src,
const std::string::size_type& count)
295 strncpy(dst, src.c_str(), count - 1);
303 std::string s = string;
306 std::replace(s.begin(), s.end(), c, to_replace);
317 const auto new_length = str.length() - end.length();
323 return str.substr(0, new_length);
331 strip(
const std::string& str,
const std::string&
ch)
334 for(
const char c: str)
336 if(
ch.find(c) == std::string::npos)
351 for(
const char c: str)
353 if(
ch.find(c) == std::string::npos)
378 enum class AddEmptyLast
385 typename IsDelimFunction
387 std::vector<std::string>
388 split_base(
const std::string& str, AddEmpty add_empty, AddEmptyLast add_empty_last, IsDelimFunction&& is_delim)
390 std::vector<std::string> ret;
391 if (str.empty()) {
return ret; }
393 auto buffer = StringBuilder{};
401 ret.emplace_back(
buffer.to_string());
404 else if (add_empty == AddEmpty::yes)
406 ret.emplace_back(
"");
417 ret.emplace_back(
buffer.to_string());
420 else if(add_empty_last == AddEmptyLast::yes)
422 ret.emplace_back(
"");
430 std::vector<std::string>
431 split(
const std::string& s,
char delim)
435 s, AddEmpty::yes, AddEmptyLast::yes, [&](
char c)
443 std::vector<std::string>
448 string, AddEmpty::no, AddEmptyLast::no, [](
char c)
473 return b >=
'0' && b <=
'9';
480 const char*& a = *aa;
482 int result = *a -
'0';
500 const char* a = lhs.c_str();
501 const char* b = rhs.c_str();
503 if(a == b) {
return 0; }
504 if(a ==
nullptr) {
return -1; }
505 if(b ==
nullptr) {
return 1; }
507 while(*a != 0 && *b != 0)
513 if(a0 < b0) {
return -1; }
514 if(a0 > b0) {
return 1; }
520 if(*a != 0) {
return 1; }
521 if(*b != 0) {
return -1; }
std::string get_first_chars(const std::string &str, std::size_t count)
std::string remove_from_end(const std::string &str, const std::string &end)
std::string trim_left(const std::string &string_to_trim, std::string_view trim_characters)
Remove characters from the left, stops at invalid character.
std::string remove_consecutive(const std::string &str, const std::string &ch)
std::string to_lower(const std::string &str)
Generate a string containing only lower characters.
void copy(char *dst, const std::string &src, const std::string::size_type &count)
Copy a string to a character buffer, adding null terminator at the end.
std::vector< std::string > split_on_spaces(const std::string &string)
std::string get_first_chars_with_ellipsis(const std::string &str, unsigned int count)
constexpr std::string_view space_characters
Space characters.
std::string from_char_to_string(char c, CharToStringStyle style)
std::vector< std::string > to_string_vector(const std::map< TKey, TValue > &map)
std::vector< std::string > split(const std::string &s, char delim)
std::string replace_with_character(const std::string &string, const std::string &to_find, char to_replace)
void replace_all(std::string *string, const std::string &to_find, const std::string &to_replace)
Replace all occurrences in a string.
char to_upper_char(char b)
std::string trim_right(const std::string &string_to_trim, std::string_view trim_characters)
Remove characters from the right, stops at a invalid character.
int compare_string(const std::string &lhs, const std::string &rhs)
std::string to_upper(const std::string &str)
std::string get_string_or_empty(bool b, const std::string &str)
std::string::size_type find_first_index_of_mismatch(const std::string &lhs, const std::string &rhs)
std::pair< std::string, std::string > get_last_string(const std::string &str, char sep)
for hello.dog gets .dog
std::string strip(const std::string &str, const std::string &ch)
std::string trim(const std::string &string_to_trim, std::string_view trim_characters)
Remove characters from both the start and the end.
char to_lower_char(char b)
std::string strip_last_string(const std::string &str, char sep)
for hello.dog and . gets hello
bool begins_with(const std::string &string_to_test, const std::string &start)
Tests if a string starts with another string.
int parse_number(const char **aa)
bool ends_with(const std::string &str, char c)
std::string to_string(const Aabb &a)
size2f min(const size2f lhs, const size2f rhs)
String utility functions.
std::string to_string()
Complete the builder and return the resulting string.
StringBuilder & add_char(char c)