mirror of
https://github.com/esphome/esphome.git
synced 2024-11-26 08:55:22 +01:00
Introduce str_lower_case() and str_upper_case() helpers (#3008)
This commit is contained in:
parent
5c339d4597
commit
640142fc0c
2 changed files with 15 additions and 0 deletions
|
@ -2,6 +2,7 @@
|
||||||
#include "esphome/core/defines.h"
|
#include "esphome/core/defines.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
@ -335,6 +336,16 @@ std::string str_until(const char *str, char ch) {
|
||||||
return pos == nullptr ? std::string(str) : std::string(str, pos - str);
|
return pos == nullptr ? std::string(str) : std::string(str, pos - str);
|
||||||
}
|
}
|
||||||
std::string str_until(const std::string &str, char ch) { return str.substr(0, str.find(ch)); }
|
std::string str_until(const std::string &str, char ch) { return str.substr(0, str.find(ch)); }
|
||||||
|
// wrapper around std::transform to run safely on functions from the ctype.h header
|
||||||
|
// see https://en.cppreference.com/w/cpp/string/byte/toupper#Notes
|
||||||
|
template<int (*fn)(int)> std::string str_ctype_transform(const std::string &str) {
|
||||||
|
std::string result;
|
||||||
|
result.resize(str.length());
|
||||||
|
std::transform(str.begin(), str.end(), result.begin(), [](unsigned char ch) { return fn(ch); });
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
std::string str_lower_case(const std::string &str) { return str_ctype_transform<std::toupper>(str); }
|
||||||
|
std::string str_upper_case(const std::string &str) { return str_ctype_transform<std::tolower>(str); }
|
||||||
std::string str_snake_case(const std::string &str) {
|
std::string str_snake_case(const std::string &str) {
|
||||||
std::string result;
|
std::string result;
|
||||||
result.resize(str.length());
|
result.resize(str.length());
|
||||||
|
|
|
@ -385,6 +385,10 @@ std::string str_until(const char *str, char ch);
|
||||||
/// Extract the part of the string until either the first occurence of the specified character, or the end.
|
/// Extract the part of the string until either the first occurence of the specified character, or the end.
|
||||||
std::string str_until(const std::string &str, char ch);
|
std::string str_until(const std::string &str, char ch);
|
||||||
|
|
||||||
|
/// Convert the string to lower case.
|
||||||
|
std::string str_lower_case(const std::string &str);
|
||||||
|
/// Convert the string to upper case.
|
||||||
|
std::string str_upper_case(const std::string &str);
|
||||||
/// Convert the string to snake case (lowercase with underscores).
|
/// Convert the string to snake case (lowercase with underscores).
|
||||||
std::string str_snake_case(const std::string &str);
|
std::string str_snake_case(const std::string &str);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue