[core] Refactor str_snake_case and str_sanitize to use constexpr helpers (#12454)
This commit is contained in:
@@ -200,22 +200,27 @@ template<int (*fn)(int)> std::string str_ctype_transform(const std::string &str)
|
||||
}
|
||||
std::string str_lower_case(const std::string &str) { return str_ctype_transform<std::tolower>(str); }
|
||||
std::string str_upper_case(const std::string &str) { return str_ctype_transform<std::toupper>(str); }
|
||||
// Convert char to snake_case: lowercase and spaces to underscores
|
||||
static constexpr char to_snake_case_char(char c) {
|
||||
return (c == ' ') ? '_' : (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c;
|
||||
}
|
||||
// Sanitize char: keep alphanumerics, dashes, underscores; replace others with underscore
|
||||
static constexpr char to_sanitized_char(char c) {
|
||||
return (c == '-' || c == '_' || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) ? c : '_';
|
||||
}
|
||||
std::string str_snake_case(const std::string &str) {
|
||||
std::string result;
|
||||
result.resize(str.length());
|
||||
std::transform(str.begin(), str.end(), result.begin(), ::tolower);
|
||||
std::replace(result.begin(), result.end(), ' ', '_');
|
||||
std::string result = str;
|
||||
for (char &c : result) {
|
||||
c = to_snake_case_char(c);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
std::string str_sanitize(const std::string &str) {
|
||||
std::string out = str;
|
||||
std::replace_if(
|
||||
out.begin(), out.end(),
|
||||
[](const char &c) {
|
||||
return c != '-' && c != '_' && (c < '0' || c > '9') && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z');
|
||||
},
|
||||
'_');
|
||||
return out;
|
||||
std::string result = str;
|
||||
for (char &c : result) {
|
||||
c = to_sanitized_char(c);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
std::string str_snprintf(const char *fmt, size_t len, ...) {
|
||||
std::string str;
|
||||
|
||||
Reference in New Issue
Block a user