ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
alloc_helpers.h
Go to the documentation of this file.
1#pragma once
2
12
13#include <array>
14#include <cstdarg>
15#include <cstdint>
16#include <cstdio>
17#include <string>
18#include <type_traits>
19#include <vector>
20
22
23namespace esphome {
24
25// --- String helpers (allocating) ---
26
29std::string str_truncate(const std::string &str, size_t length);
30
34std::string str_until(const char *str, char ch);
37std::string str_until(const std::string &str, char ch);
38
41std::string str_lower_case(const std::string &str);
42
45std::string str_upper_case(const std::string &str);
46
49std::string str_snake_case(const std::string &str);
50
53std::string str_sanitize(const std::string &str);
54
57std::string __attribute__((format(printf, 1, 3))) str_snprintf(const char *fmt, size_t len, ...);
58
61std::string __attribute__((format(printf, 1, 2))) str_sprintf(const char *fmt, ...);
62
63// --- Hex/binary formatting helpers (allocating) ---
64
67std::string format_mac_address_pretty(const uint8_t mac[6]);
68
71std::string format_hex(const uint8_t *data, size_t length);
72
75std::string format_hex(const std::vector<uint8_t> &data);
76
79template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> std::string format_hex(T val) {
81 return format_hex(reinterpret_cast<uint8_t *>(&val), sizeof(T));
82}
83
86template<std::size_t N> std::string format_hex(const std::array<uint8_t, N> &data) {
87 return format_hex(data.data(), data.size());
88}
89
92std::string format_hex_pretty(const uint8_t *data, size_t length, char separator = '.', bool show_length = true);
93
96std::string format_hex_pretty(const uint16_t *data, size_t length, char separator = '.', bool show_length = true);
97
100std::string format_hex_pretty(const std::vector<uint8_t> &data, char separator = '.', bool show_length = true);
101
104std::string format_hex_pretty(const std::vector<uint16_t> &data, char separator = '.', bool show_length = true);
105
108std::string format_hex_pretty(const std::string &data, char separator = '.', bool show_length = true);
109
112template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
113std::string format_hex_pretty(T val, char separator = '.', bool show_length = true) {
115 return format_hex_pretty(reinterpret_cast<uint8_t *>(&val), sizeof(T), separator, show_length);
116}
117
120std::string format_bin(const uint8_t *data, size_t length);
121
124template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> std::string format_bin(T val) {
126 return format_bin(reinterpret_cast<uint8_t *>(&val), sizeof(T));
127}
128
129// --- Base64 helpers (allocating) ---
130
133std::string base64_encode(const uint8_t *buf, size_t buf_len);
136std::string base64_encode(const std::vector<uint8_t> &buf);
137
140std::vector<uint8_t> base64_decode(const std::string &encoded_string);
141
142// --- MAC address helpers (allocating) ---
143
146std::string get_mac_address();
147
150std::string get_mac_address_pretty();
151
152} // namespace esphome
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:45
const char * format
mopeka_std_values val[3]
constexpr T convert_big_endian(T val)
Convert a value between host byte order and big endian (most significant byte first) order.
Definition helpers.h:1036
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
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.
const void size_t len
Definition hal.h:68
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:1180
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.
STL namespace.
uint16_t length
Definition tt21100.cpp:0