[core] Stop re-exporting alloc_helpers.h from helpers.h (#20389)

This commit is contained in:
J. Nick Koston
2026-10-09 09:21:16 -10:00
committed by GitHub
parent 5cd7bf5993
commit 23d13d3a6e
5 changed files with 33 additions and 79 deletions
@@ -1,6 +1,7 @@
#include "debug_component.h"
#ifdef USE_ZEPHYR
#include <climits>
#include "esphome/core/alloc_helpers.h"
#include "esphome/core/log.h"
#include <esphome/components/zephyr/reset_reason.h>
#include <zephyr/drivers/hwinfo.h>
+32
View File
@@ -10,12 +10,16 @@
/// Prefer the stack-based alternatives documented on each function instead.
/// New code should avoid using these functions.
#include <array>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <string>
#include <type_traits>
#include <vector>
#include "esphome/core/helpers.h"
namespace esphome {
// --- String helpers (allocating) ---
@@ -70,6 +74,19 @@ std::string format_hex(const uint8_t *data, size_t length);
/// @warning Allocates heap memory. Use format_hex_to() with a stack buffer instead.
std::string format_hex(const std::vector<uint8_t> &data);
/// Format an unsigned integer in lowercased hex, starting with the most significant byte.
/// @warning Allocates heap memory. Use format_hex_to() with a stack buffer instead.
template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> std::string format_hex(T val) {
val = convert_big_endian(val);
return format_hex(reinterpret_cast<uint8_t *>(&val), sizeof(T));
}
/// Format the std::array \p data in lowercased hex.
/// @warning Allocates heap memory. Use format_hex_to() with a stack buffer instead.
template<std::size_t N> std::string format_hex(const std::array<uint8_t, N> &data) {
return format_hex(data.data(), data.size());
}
/// Format a byte array in pretty-printed, human-readable hex format.
/// @warning Allocates heap memory. Use format_hex_pretty_to() with a stack buffer instead.
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator = '.', bool show_length = true);
@@ -90,10 +107,25 @@ std::string format_hex_pretty(const std::vector<uint16_t> &data, char separator
/// @warning Allocates heap memory. Use format_hex_pretty_to() with a stack buffer instead.
std::string format_hex_pretty(const std::string &data, char separator = '.', bool show_length = true);
/// Format an unsigned integer in pretty-printed, human-readable hex format.
/// @warning Allocates heap memory. Use format_hex_pretty_to() with a stack buffer instead.
template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
std::string format_hex_pretty(T val, char separator = '.', bool show_length = true) {
val = convert_big_endian(val);
return format_hex_pretty(reinterpret_cast<uint8_t *>(&val), sizeof(T), separator, show_length);
}
/// Format the byte array \p data of length \p len in binary.
/// @warning Allocates heap memory. Use format_bin_to() with a stack buffer instead.
std::string format_bin(const uint8_t *data, size_t length);
/// Format an unsigned integer in binary, starting with the most significant byte.
/// @warning Allocates heap memory. Use format_bin_to() with a stack buffer instead.
template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> std::string format_bin(T val) {
val = convert_big_endian(val);
return format_bin(reinterpret_cast<uint8_t *>(&val), sizeof(T));
}
// --- Base64 helpers (allocating) ---
/// Encode a byte buffer to base64 string.
-19
View File
@@ -261,7 +261,6 @@ bool str_contains_ignore_case_p(const char *haystack, PGM_P needle) {
}
#endif // USE_ESP8266
// str_truncate, str_until, str_lower_case, str_upper_case, str_snake_case moved to alloc_helpers.cpp
char *str_sanitize_to(char *buffer, size_t buffer_size, const char *str) {
if (buffer_size == 0) {
return buffer;
@@ -274,8 +273,6 @@ char *str_sanitize_to(char *buffer, size_t buffer_size, const char *str) {
return buffer;
}
// str_sanitize, str_snprintf, str_sprintf moved to alloc_helpers.cpp
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;
@@ -309,8 +306,6 @@ size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count) {
return chars;
}
// format_mac_address_pretty moved to alloc_helpers.cpp
// Internal helper for hex formatting - base is 'a' for lowercase or 'A' for uppercase.
// When separator is set, it is written unconditionally after each byte and the last
// one is overwritten with '\0', eliminating the per-byte `i < length - 1` check.
@@ -428,8 +423,6 @@ const char *json_escape_into_buffer(std::span<char> buf, StringRef value, bool s
return buf.data();
}
// format_hex (std::string returning overloads) moved to alloc_helpers.cpp
char *format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator) {
return format_hex_internal(buffer, buffer_size, data, length, separator, 'A');
}
@@ -465,8 +458,6 @@ char *format_hex_pretty_to(char *buffer, size_t buffer_size, const uint16_t *dat
return buffer;
}
// format_hex_pretty (all std::string returning overloads) moved to alloc_helpers.cpp
char *format_bin_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length) {
if (buffer_size == 0) {
return buffer;
@@ -488,8 +479,6 @@ char *format_bin_to(char *buffer, size_t buffer_size, const uint8_t *data, size_
return buffer;
}
// format_bin moved to alloc_helpers.cpp
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off) {
if (on == nullptr && ESPHOME_strcasecmp_P(str, ESPHOME_PSTR("on")) == 0)
return PARSE_ON;
@@ -537,8 +526,6 @@ static inline void normalize_accuracy_decimals(float &value, int8_t &accuracy_de
}
}
// value_accuracy_to_string moved to alloc_helpers.cpp
// Fast float-to-string for accuracy_decimals 0-3 (covers virtually all sensor usage).
// Avoids snprintf("%.*f") which pulls in heavy float formatting machinery.
// Caller must guarantee value is finite and |value| * mult fits in uint32_t.
@@ -639,8 +626,6 @@ static constexpr uint8_t base64_char_value(uint8_t c) {
return INVALID_BASE64_CHAR;
}
// base64_encode (both overloads) moved to alloc_helpers.cpp
size_t base64_decode(const std::string &encoded_string, uint8_t *buf, size_t buf_len) {
return base64_decode(reinterpret_cast<const uint8_t *>(encoded_string.data()), encoded_string.size(), buf, buf_len);
}
@@ -668,8 +653,6 @@ size_t base64_decode(const uint8_t *encoded_data, size_t encoded_len, uint8_t *b
return out;
}
// base64_decode (vector-returning overload) moved to alloc_helpers.cpp
/// Decode base64/base64url string directly into vector of little-endian int32 values
/// @param base64 Base64 or base64url encoded string (both +/ and -_ accepted)
/// @param out Output vector (cleared and filled with decoded int32 values)
@@ -790,8 +773,6 @@ void HighFrequencyLoopRequester::stop() {
this->started_ = false;
}
// get_mac_address, get_mac_address_pretty moved to alloc_helpers.cpp
void get_mac_address_into_buffer(std::span<char, MAC_ADDRESS_BUFFER_SIZE> buf) {
uint8_t mac[MAC_ADDRESS_SIZE];
get_mac_address_raw(mac);
-56
View File
@@ -26,12 +26,6 @@
#include "esphome/core/optional.h"
#include "esphome/core/time_conversion.h"
// Backward compatibility re-export of heap-allocating helpers.
// These functions have moved to alloc_helpers.h. External components should
// update their includes to use #include "esphome/core/alloc_helpers.h" directly.
// This re-export will be removed in 2026.11.0.
#include "esphome/core/alloc_helpers.h"
#ifdef USE_ESP8266
#include <Esp.h>
#include <pgmspace.h>
@@ -1105,13 +1099,8 @@ inline bool str_contains_ignore_case(const char *haystack, const char *needle) {
}
#endif // USE_ESP8266
// str_truncate moved to alloc_helpers.h - remove this include before 2026.11.0
// str_until, str_lower_case, str_upper_case moved to alloc_helpers.h - remove this comment before 2026.11.0
/// Convert a single char to snake_case: lowercase and space to underscore.
constexpr char to_snake_case_char(char c) { return (c == ' ') ? '_' : (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c; }
// str_snake_case moved to alloc_helpers.h - remove this comment before 2026.11.0
/// Sanitize a single char: keep alphanumerics, dashes, underscores; replace others with underscore.
constexpr char to_sanitized_char(char c) {
@@ -1134,8 +1123,6 @@ template<size_t N> inline char *str_sanitize_to(char (&buffer)[N], const char *s
return str_sanitize_to(buffer, N, str);
}
// str_sanitize moved to alloc_helpers.h - remove this comment before 2026.11.0
/// Calculate FNV-1 hash of a string while applying snake_case + sanitize transformations.
/// This computes object_id hashes directly from names without creating an intermediate buffer.
/// IMPORTANT: Must match Python fnv1_hash_object_id() in esphome/helpers.py.
@@ -1150,8 +1137,6 @@ inline uint32_t fnv1_hash_object_id(const char *str, size_t len) {
return hash;
}
// str_snprintf, str_sprintf moved to alloc_helpers.h - remove this comment before 2026.11.0
#ifdef USE_ESP8266
// ESP8266: Use vsnprintf_P to keep format strings in flash (PROGMEM)
// Format strings must be wrapped with PSTR() macro
@@ -1605,32 +1590,6 @@ inline void format_mac_addr_lower_no_sep(const uint8_t *mac, char *output) {
format_hex_to(output, MAC_ADDRESS_BUFFER_SIZE, mac, MAC_ADDRESS_SIZE);
}
// format_mac_address_pretty, format_hex (all overloads) moved to alloc_helpers.h
// Remove this comment and the template overloads below before 2026.11.0
/// Format an unsigned integer in lowercased hex, starting with the most significant byte.
/// @warning Allocates heap memory. Use format_hex_to() with a stack buffer instead.
template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> std::string format_hex(T val) {
val = convert_big_endian(val);
return format_hex(reinterpret_cast<uint8_t *>(&val), sizeof(T));
}
/// Format the std::array \p data in lowercased hex.
/// @warning Allocates heap memory. Use format_hex_to() with a stack buffer instead.
template<std::size_t N> std::string format_hex(const std::array<uint8_t, N> &data) {
return format_hex(data.data(), data.size());
}
// format_hex_pretty (all overloads) moved to alloc_helpers.h
// Remove this comment and the template overload below before 2026.11.0
/// Format an unsigned integer in pretty-printed, human-readable hex format.
/// @warning Allocates heap memory. Use format_hex_pretty_to() with a stack buffer instead.
template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
std::string format_hex_pretty(T val, char separator = '.', bool show_length = true) {
val = convert_big_endian(val);
return format_hex_pretty(reinterpret_cast<uint8_t *>(&val), sizeof(T), separator, show_length);
}
/// Calculate buffer size needed for format_bin_to: "01234567...\0" = bytes * 8 + 1
constexpr size_t format_bin_size(size_t byte_count) { return byte_count * 8 + 1; }
@@ -1684,15 +1643,6 @@ inline char *format_bin_to(char (&buffer)[N], T val) {
return format_bin_to(buffer, reinterpret_cast<const uint8_t *>(&val), sizeof(T));
}
// format_bin moved to alloc_helpers.h - remove this comment and template overload before 2026.11.0
/// Format an unsigned integer in binary, starting with the most significant byte.
/// @warning Allocates heap memory. Use format_bin_to() with a stack buffer instead.
template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> std::string format_bin(T val) {
val = convert_big_endian(val);
return format_bin(reinterpret_cast<uint8_t *>(&val), sizeof(T));
}
/// Return values for parse_on_off().
enum ParseOnOffState : uint8_t {
PARSE_NONE = 0,
@@ -1703,8 +1653,6 @@ enum ParseOnOffState : uint8_t {
/// Parse a string that contains either on, off or toggle.
ParseOnOffState parse_on_off(const char *str, const char *on = nullptr, const char *off = nullptr);
// value_accuracy_to_string moved to alloc_helpers.h - remove this comment before 2026.11.0
/// Maximum buffer size for value_accuracy formatting (float ~15 chars + space + UOM ~40 chars + null)
static constexpr size_t VALUE_ACCURACY_MAX_LEN = 64;
@@ -1717,8 +1665,6 @@ size_t value_accuracy_with_uom_to_buf(std::span<char, VALUE_ACCURACY_MAX_LEN> bu
/// Derive accuracy in decimals from an increment step.
int8_t step_to_accuracy_decimals(float step);
// base64_encode (both overloads), base64_decode (vector overload) moved to alloc_helpers.h
// Remove this comment before 2026.11.0
size_t base64_decode(std::string const &encoded_string, uint8_t *buf, size_t buf_len);
size_t base64_decode(const uint8_t *encoded_data, size_t encoded_len, uint8_t *buf, size_t buf_len);
@@ -2177,8 +2123,6 @@ class HighFrequencyLoopRequester {
/// Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
void get_mac_address_raw(uint8_t *mac); // NOLINT(readability-non-const-parameter)
// get_mac_address, get_mac_address_pretty moved to alloc_helpers.h - remove this comment before 2026.11.0
/// Get the device MAC address into the given buffer, in lowercase hex notation.
/// Assumes buffer length is MAC_ADDRESS_BUFFER_SIZE (12 digits for hexadecimal representation followed by null
/// terminator).
-4
View File
@@ -1071,9 +1071,6 @@ HEAP_ALLOCATING_HELPERS = {
# The definitions themselves
"esphome/core/alloc_helpers.h",
"esphome/core/alloc_helpers.cpp",
# Backward compatibility re-exports (remove before 2026.11.0)
"esphome/core/helpers.h",
"esphome/core/helpers.cpp",
# Vendored third-party library
"esphome/components/http_request/httplib.h",
],
@@ -1126,7 +1123,6 @@ def lint_no_sprintf(fname, match):
# Vendored library
"esphome/components/http_request/httplib.h",
# Deprecated helpers that return std::string
"esphome/core/helpers.cpp",
"esphome/core/alloc_helpers.cpp",
# The using declaration itself
"esphome/core/helpers.h",