From 7782bc11c6a08a35d401e9264e2818a72a0ab02b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 27 Aug 2026 15:29:43 -0500 Subject: [PATCH] [core] Remove make_name_with_suffix std::string overloads (#18828) --- esphome/components/mqtt/mqtt_client.cpp | 6 +++++- esphome/config_validation.py | 2 +- esphome/core/helpers.cpp | 14 -------------- esphome/core/helpers.h | 24 +++--------------------- 4 files changed, 9 insertions(+), 37 deletions(-) diff --git a/esphome/components/mqtt/mqtt_client.cpp b/esphome/components/mqtt/mqtt_client.cpp index 1127c36dc6..2ecab47904 100644 --- a/esphome/components/mqtt/mqtt_client.cpp +++ b/esphome/components/mqtt/mqtt_client.cpp @@ -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 diff --git a/esphome/config_validation.py b/esphome/config_validation.py index 904cbd1919..aff39201e8 100644 --- a/esphome/config_validation.py +++ b/esphome/config_validation.py @@ -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: diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 71e3c87e1e..6bfe5c9e3c 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -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) { diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 9fdc088ecb..1ccc833048 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -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