mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 22:48:10 +01:00
Ensure names containing characters other than a-z
A-Z
0-9
or _
are unique (#5810)
This commit is contained in:
parent
9f8a896e13
commit
5c31bec8c2
3 changed files with 14 additions and 8 deletions
|
@ -278,10 +278,13 @@ std::string str_snake_case(const std::string &str) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
std::string str_sanitize(const std::string &str) {
|
std::string str_sanitize(const std::string &str) {
|
||||||
std::string out;
|
std::string out = str;
|
||||||
std::copy_if(str.begin(), str.end(), std::back_inserter(out), [](const char &c) {
|
std::replace_if(
|
||||||
return c == '-' || c == '_' || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
out.begin(), out.end(),
|
||||||
});
|
[](const char &c) {
|
||||||
|
return !(c == '-' || c == '_' || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
|
||||||
|
},
|
||||||
|
'_');
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
std::string str_snprintf(const char *fmt, size_t len, ...) {
|
std::string str_snprintf(const char *fmt, size_t len, ...) {
|
||||||
|
|
|
@ -357,6 +357,9 @@ def snake_case(value):
|
||||||
return value.replace(" ", "_").lower()
|
return value.replace(" ", "_").lower()
|
||||||
|
|
||||||
|
|
||||||
|
_DISALLOWED_CHARS = re.compile(r"[^a-zA-Z0-9_]")
|
||||||
|
|
||||||
|
|
||||||
def sanitize(value):
|
def sanitize(value):
|
||||||
"""Same behaviour as `helpers.cpp` method `str_sanitize`."""
|
"""Same behaviour as `helpers.cpp` method `str_sanitize`."""
|
||||||
return re.sub("[^-_0-9a-zA-Z]", r"", value)
|
return _DISALLOWED_CHARS.sub("_", value)
|
||||||
|
|
|
@ -258,9 +258,9 @@ def test_snake_case(text, expected):
|
||||||
"text, expected",
|
"text, expected",
|
||||||
(
|
(
|
||||||
("foo_bar", "foo_bar"),
|
("foo_bar", "foo_bar"),
|
||||||
('!"§$%&/()=?foo_bar', "foo_bar"),
|
('!"§$%&/()=?foo_bar', "___________foo_bar"),
|
||||||
('foo_!"§$%&/()=?bar', "foo_bar"),
|
('foo_!"§$%&/()=?bar', "foo____________bar"),
|
||||||
('foo_bar!"§$%&/()=?', "foo_bar"),
|
('foo_bar!"§$%&/()=?', "foo_bar___________"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
def test_sanitize(text, expected):
|
def test_sanitize(text, expected):
|
||||||
|
|
Loading…
Reference in a new issue