1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-13 00:32:20 +01:00

Clean-up string sanitation helpers (#2660)

This commit is contained in:
Oxan van Leeuwen
2021-11-10 19:42:41 +01:00
committed by GitHub
parent 92321e219a
commit 4d43396835
4 changed files with 37 additions and 44 deletions

View File

@@ -25,9 +25,6 @@
namespace esphome {
/// The characters that are allowed in a hostname.
extern const char *const HOSTNAME_CHARACTER_ALLOWLIST;
/// Read the raw MAC address into the provided byte array (6 bytes).
void get_mac_address_raw(uint8_t *mac);
@@ -55,14 +52,6 @@ std::string to_string(double val);
std::string to_string(long double val);
optional<int> parse_hex(const std::string &str, size_t start, size_t length);
optional<int> parse_hex(char chr);
/// Sanitize the hostname by removing characters that are not in the allowlist and truncating it to 63 chars.
std::string sanitize_hostname(const std::string &hostname);
/// Truncate a string to a specific length
std::string truncate_string(const std::string &s, size_t length);
/// Convert the string to lowercase_underscore.
std::string to_lowercase_underscore(std::string s);
/// Compare string a to string b (ignoring case) and return whether they are equal.
bool str_equals_case_insensitive(const std::string &a, const std::string &b);
@@ -145,9 +134,6 @@ std::string uint64_to_string(uint64_t num);
/// Convert a uint32_t to a hex string
std::string uint32_to_string(uint32_t num);
/// Sanitizes the input string with the allowlist.
std::string sanitize_string_allowlist(const std::string &s, const std::string &allowlist);
uint8_t reverse_bits_8(uint8_t x);
uint16_t reverse_bits_16(uint16_t x);
uint32_t reverse_bits_32(uint32_t x);
@@ -331,6 +317,20 @@ template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> constexpr
///@}
/// @name Strings
///@{
/// Truncate a string to a specific length.
std::string str_truncate(const std::string &str, size_t length);
/// Convert the string to snake case (lowercase with underscores).
std::string str_snake_case(const std::string &str);
/// Sanitizes the input string by removing all characters but alphanumerics, dashes and underscores.
std::string str_sanitize(const std::string &str);
///@}
/// @name Parsing & formatting
///@{