ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
alloc_helpers.h
Go to the documentation of this file.
1#pragma once
2
12
13#include <cstdarg>
14#include <cstdint>
15#include <cstdio>
16#include <string>
17#include <vector>
18
19namespace esphome {
20
21// --- String helpers (allocating) ---
22
25std::string str_truncate(const std::string &str, size_t length);
26
30std::string str_until(const char *str, char ch);
33std::string str_until(const std::string &str, char ch);
34
37std::string str_lower_case(const std::string &str);
38
41std::string str_upper_case(const std::string &str);
42
45std::string str_snake_case(const std::string &str);
46
49std::string str_sanitize(const std::string &str);
50
53std::string __attribute__((format(printf, 1, 3))) str_snprintf(const char *fmt, size_t len, ...);
54
57std::string __attribute__((format(printf, 1, 2))) str_sprintf(const char *fmt, ...);
58
59// --- Hex/binary formatting helpers (allocating) ---
60
63std::string format_mac_address_pretty(const uint8_t mac[6]);
64
67std::string format_hex(const uint8_t *data, size_t length);
68
71std::string format_hex(const std::vector<uint8_t> &data);
72
75std::string format_hex_pretty(const uint8_t *data, size_t length, char separator = '.', bool show_length = true);
76
79std::string format_hex_pretty(const uint16_t *data, size_t length, char separator = '.', bool show_length = true);
80
83std::string format_hex_pretty(const std::vector<uint8_t> &data, char separator = '.', bool show_length = true);
84
87std::string format_hex_pretty(const std::vector<uint16_t> &data, char separator = '.', bool show_length = true);
88
91std::string format_hex_pretty(const std::string &data, char separator = '.', bool show_length = true);
92
95std::string format_bin(const uint8_t *data, size_t length);
96
97// --- Value formatting helpers (allocating) ---
98
101__attribute__((deprecated("Allocates heap memory. Use value_accuracy_to_buf() instead. Removed in 2026.7.0.")))
102std::string
103value_accuracy_to_string(float value, int8_t accuracy_decimals);
104
105// --- Base64 helpers (allocating) ---
106
109std::string base64_encode(const uint8_t *buf, size_t buf_len);
112std::string base64_encode(const std::vector<uint8_t> &buf);
113
116std::vector<uint8_t> base64_decode(const std::string &encoded_string);
117
118// --- MAC address helpers (allocating) ---
119
122std::string get_mac_address();
123
126std::string get_mac_address_pretty();
127
128} // namespace esphome
struct @65::@66 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:32
const char int const __FlashStringHelper * format
Definition log.h:74
std::string value_accuracy_to_string(float value, int8_t accuracy_decimals)
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
size_t value_accuracy_to_buf(std::span< char, VALUE_ACCURACY_MAX_LEN > buf, float value, int8_t accuracy_decimals)
Format value with accuracy to buffer, returns chars written (excluding null)
Definition helpers.cpp:474
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
std::string format_bin(const uint8_t *data, size_t length)
Format the byte array data of length len in binary.
std::string str_sanitize(const std::string &str)
Sanitizes the input string by removing all characters but alphanumerics, dashes and underscores.
std::string size_t len
std::vector< uint8_t > base64_decode(const std::string &encoded_string)
Decode a base64 string to a byte vector.
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
std::string str_snprintf(const char *fmt, size_t len,...)
std::string str_upper_case(const std::string &str)
Convert the string to upper case.
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
std::string str_until(const char *str, char ch)
Extract the part of the string until either the first occurrence of the specified character,...
std::string format_mac_address_pretty(const uint8_t *mac)
std::string base64_encode(const std::vector< uint8_t > &buf)
Encode a byte vector to base64 string.
std::string str_sprintf(const char *fmt,...)
size_t size_t const char * fmt
Definition helpers.h:1039
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
std::string str_snake_case(const std::string &str)
Convert the string to snake case (lowercase with underscores).
std::string str_truncate(const std::string &str, size_t length)
Truncate a string to a specific length.
uint16_t length
Definition tt21100.cpp:0