mirror of
https://github.com/esphome/esphome.git
synced 2026-08-29 01:03:29 +00:00
[core] Remove make_name_with_suffix std::string overloads (#18828)
This commit is contained in:
@@ -41,7 +41,11 @@ MQTTClientComponent::MQTTClientComponent() {
|
||||
global_mqtt_client = this;
|
||||
char mac_addr[MAC_ADDRESS_BUFFER_SIZE];
|
||||
get_mac_address_into_buffer(mac_addr);
|
||||
this->credentials_.client_id = make_name_with_suffix(App.get_name(), '-', mac_addr, MAC_ADDRESS_BUFFER_SIZE - 1);
|
||||
const StringRef &name = App.get_name();
|
||||
char client_id[MAX_NAME_WITH_SUFFIX_SIZE];
|
||||
size_t len = make_name_with_suffix_to(client_id, sizeof(client_id), name.c_str(), name.size(), '-', mac_addr,
|
||||
MAC_ADDRESS_BUFFER_SIZE - 1);
|
||||
this->credentials_.client_id.assign(client_id, len);
|
||||
}
|
||||
|
||||
// Connection
|
||||
|
||||
@@ -1462,7 +1462,7 @@ def hostname(value):
|
||||
Maximum length is 63 characters per RFC 1035.
|
||||
|
||||
Note: If this limit is changed, update MAX_NAME_WITH_SUFFIX_SIZE in
|
||||
esphome/core/helpers.cpp to accommodate the new maximum length.
|
||||
esphome/core/helpers.h to accommodate the new maximum length.
|
||||
"""
|
||||
value = string(value)
|
||||
if re.match(r"^[a-z0-9-]{1,63}$", value, re.IGNORECASE) is not None:
|
||||
|
||||
@@ -268,9 +268,6 @@ char *str_sanitize_to(char *buffer, size_t buffer_size, const char *str) {
|
||||
|
||||
// str_sanitize, str_snprintf, str_sprintf moved to alloc_helpers.cpp
|
||||
|
||||
// Maximum size for name with suffix: 120 (max friendly name) + 1 (separator) + 6 (MAC suffix) + 1 (null term)
|
||||
static constexpr size_t MAX_NAME_WITH_SUFFIX_SIZE = 128;
|
||||
|
||||
size_t make_name_with_suffix_to(char *buffer, size_t buffer_size, const char *name, size_t name_len, char sep,
|
||||
const char *suffix_ptr, size_t suffix_len) {
|
||||
size_t total_len = name_len + 1 + suffix_len;
|
||||
@@ -291,17 +288,6 @@ size_t make_name_with_suffix_to(char *buffer, size_t buffer_size, const char *na
|
||||
return total_len;
|
||||
}
|
||||
|
||||
std::string make_name_with_suffix(const char *name, size_t name_len, char sep, const char *suffix_ptr,
|
||||
size_t suffix_len) {
|
||||
char buffer[MAX_NAME_WITH_SUFFIX_SIZE];
|
||||
size_t len = make_name_with_suffix_to(buffer, sizeof(buffer), name, name_len, sep, suffix_ptr, suffix_len);
|
||||
return std::string(buffer, len);
|
||||
}
|
||||
|
||||
std::string make_name_with_suffix(const std::string &name, char sep, const char *suffix_ptr, size_t suffix_len) {
|
||||
return make_name_with_suffix(name.c_str(), name.size(), sep, suffix_ptr, suffix_len);
|
||||
}
|
||||
|
||||
// Parsing & formatting
|
||||
|
||||
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count) {
|
||||
|
||||
+3
-21
@@ -1153,28 +1153,10 @@ inline size_t buf_append_str(char *buf, size_t size, size_t pos, const char *str
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Concatenate a name with a separator and suffix using an efficient stack-based approach.
|
||||
/// This avoids multiple heap allocations during string construction.
|
||||
/// Maximum name length supported is 120 characters for friendly names.
|
||||
/// @param name The base name string
|
||||
/// @param sep The separator character (e.g., '-', ' ', or '.')
|
||||
/// @param suffix_ptr Pointer to the suffix characters
|
||||
/// @param suffix_len Length of the suffix
|
||||
/// @return The concatenated string: name + sep + suffix
|
||||
std::string make_name_with_suffix(const std::string &name, char sep, const char *suffix_ptr, size_t suffix_len);
|
||||
/// Maximum size for name with suffix: 120 (max friendly name) + 1 (separator) + 6 (MAC suffix) + 1 (null term)
|
||||
static constexpr size_t MAX_NAME_WITH_SUFFIX_SIZE = 128;
|
||||
|
||||
/// Optimized string concatenation: name + separator + suffix (const char* overload)
|
||||
/// Uses a fixed stack buffer to avoid heap allocations.
|
||||
/// @param name The base name string
|
||||
/// @param name_len Length of the name
|
||||
/// @param sep Single character separator
|
||||
/// @param suffix_ptr Pointer to the suffix characters
|
||||
/// @param suffix_len Length of the suffix
|
||||
/// @return The concatenated string: name + sep + suffix
|
||||
std::string make_name_with_suffix(const char *name, size_t name_len, char sep, const char *suffix_ptr,
|
||||
size_t suffix_len);
|
||||
|
||||
/// Zero-allocation version: format name + separator + suffix directly into buffer.
|
||||
/// Format name + separator + suffix directly into buffer without heap allocation.
|
||||
/// @param buffer Output buffer (must have space for result + null terminator)
|
||||
/// @param buffer_size Size of the output buffer
|
||||
/// @param name The base name string
|
||||
|
||||
Reference in New Issue
Block a user