mirror of
https://github.com/esphome/esphome.git
synced 2024-11-14 19:18:09 +01:00
Ensure generated names are unique
Instead of dropping chars in str_sanitize we now replace them with _ to avoid collisions fixes esphome/issues#5135
This commit is contained in:
parent
d462beea6e
commit
cbbceb4d0d
2 changed files with 6 additions and 3 deletions
|
@ -279,9 +279,9 @@ std::string str_snake_case(const std::string &str) {
|
|||
}
|
||||
std::string str_sanitize(const std::string &str) {
|
||||
std::string out;
|
||||
std::copy_if(str.begin(), str.end(), std::back_inserter(out), [](const char &c) {
|
||||
std::replace_if(str.begin(), str.end(), [](const char &c) {
|
||||
return c == '-' || c == '_' || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
||||
});
|
||||
}, '_');
|
||||
return out;
|
||||
}
|
||||
std::string str_snprintf(const char *fmt, size_t len, ...) {
|
||||
|
|
|
@ -357,6 +357,9 @@ def snake_case(value):
|
|||
return value.replace(" ", "_").lower()
|
||||
|
||||
|
||||
_DISALLOWED_CHARS = re.compile(r"[^a-zA-Z0-9_]")
|
||||
|
||||
|
||||
def sanitize(value):
|
||||
"""Same behaviour as `helpers.cpp` method `str_sanitize`."""
|
||||
return re.sub("[^-_0-9a-zA-Z]", r"", value)
|
||||
return _DISALLOWED_CHARS.sub("_", value)
|
||||
|
|
Loading…
Reference in a new issue