refactor: buff string utils

This commit is contained in:
6arelyFuture 2022-11-25 20:03:57 +00:00
parent 5fb61c0f8e
commit 416036d25e
No known key found for this signature in database
GPG Key ID: 22F9079C86CFAB31
2 changed files with 15 additions and 11 deletions

View File

@ -31,20 +31,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(), [](const char input) { std::string result;
return static_cast<char>(tolower(input)); std::ranges::transform(text, std::back_inserter(result),
[](const unsigned char input) {
return static_cast<char>(std::tolower(input));
}); });
return text; return text;
} }
std::string to_upper(std::string text) { std::string to_upper(const std::string text) {
std::transform(text.begin(), text.end(), text.begin(), [](const char input) { std::string result;
return static_cast<char>(toupper(input)); std::ranges::transform(text, std::back_inserter(result),
[](const unsigned char 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,8 @@ 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);