refactor: buff string utils

This commit is contained in:
6arelyFuture 2022-11-25 20:24:57 +00:00
parent 3a699f14c4
commit 51dc00790b
No known key found for this signature in database
GPG Key ID: 22F9079C86CFAB31
2 changed files with 17 additions and 14 deletions

View File

@ -30,22 +30,24 @@ std::vector<std::string> split(const std::string& s, const char delim) {
return elems; return elems;
} }
std::string to_lower(std::string text) { std::string to_lower(const std::string& text) {
std::transform(text.begin(), text.end(), text.begin(), std::string result;
std::ranges::transform(text, std::back_inserter(result),
[](unsigned char input) { [](unsigned char input) {
return static_cast<char>(std::tolower(input)); return static_cast<char>(std::tolower(input));
}); });
return text; return result;
} }
std::string to_upper(std::string text) { std::string to_upper(const std::string& text) {
std::transform(text.begin(), text.end(), text.begin(), std::string result;
std::ranges::transform(text, std::back_inserter(result),
[](unsigned char input) { [](unsigned char input) {
return static_cast<char>(std::toupper(input)); return static_cast<char>(std::toupper(input));
}); });
return text; return result;
} }
bool starts_with(const std::string& text, const std::string& substring) { bool starts_with(const std::string& text, const std::string& substring) {

View File

@ -76,8 +76,9 @@ const char* va(const char* fmt, ...);
std::vector<std::string> split(const std::string& s, char delim); std::vector<std::string> split(const std::string& s, char delim);
std::string to_lower(std::string text); std::string to_lower(const std::string& text);
std::string to_upper(std::string text); std::string to_upper(const std::string& text);
bool starts_with(const std::string& text, const std::string& substring); bool starts_with(const std::string& text, const std::string& substring);
bool ends_with(const std::string& text, const std::string& substring); bool ends_with(const std::string& text, const std::string& substring);