18#define ESPHOME_F(string_literal) F(string_literal)
19#define ESPHOME_PGM_P PGM_P
20#define ESPHOME_PSTR(s) PSTR(s)
21#define ESPHOME_strncpy_P strncpy_P
22#define ESPHOME_strncat_P strncat_P
23#define ESPHOME_snprintf_P snprintf_P
24#define ESPHOME_strcmp_P strcmp_P
25#define ESPHOME_strcasecmp_P strcasecmp_P
26#define ESPHOME_strncmp_P strncmp_P
27#define ESPHOME_strncasecmp_P strncasecmp_P
28#define ESPHOME_strlen_P strlen_P
29#define ESPHOME_strstr_P strstr_P
34#define ESPHOME_PROGMEM_STRING_TABLE_STORAGE PROGMEM
36#define ESPHOME_F(string_literal) (string_literal)
37#define ESPHOME_PGM_P const char *
38#define ESPHOME_PSTR(s) (s)
39#define ESPHOME_strncpy_P strncpy
40#define ESPHOME_strncat_P strncat
41#define ESPHOME_snprintf_P snprintf
42#define ESPHOME_strcmp_P strcmp
43#define ESPHOME_strcasecmp_P strcasecmp
44#define ESPHOME_strncmp_P strncmp
45#define ESPHOME_strncasecmp_P strncasecmp
46#define ESPHOME_strlen_P strlen
47#define ESPHOME_strstr_P strstr
51#define ESPHOME_PROGMEM_STRING_TABLE_STORAGE
67 for (
size_t i = 0; i < N; ++i)
70 constexpr size_t size()
const {
return N - 1; }
83 static constexpr size_t COUNT =
sizeof...(Strs);
84 static constexpr size_t BLOB_SIZE = (0 + ... + (Strs.size() + 1));
86 static constexpr size_t MAX_LENGTH = std::max({Strs.size()...});
93 std::array<char, BLOB_SIZE> result{};
95 auto copy = [&](
const auto &str) {
96 for (
size_t i = 0; i <= str.size(); ++i)
97 result[
pos++] = str.data[i];
105 static_assert(
COUNT > 0,
"PROGMEM_STRING_TABLE must contain at least one string");
106 static_assert(
COUNT <= 255,
"PROGMEM_STRING_TABLE supports at most 255 strings with uint8_t indices");
108 std::array<uint8_t, COUNT> result{};
109 size_t pos = 0, idx = 0;
110 ((result[idx++] =
static_cast<uint8_t
>(
pos),
pos += Strs.size() + 1), ...);
113 return std::array<uint8_t, 0>{};
121 blob += ESPHOME_strlen_P(blob) + 1;
131#define PROGMEM_STRING_TABLE(Name, ...) \
133 using Table = ::esphome::ProgmemStringTable<__VA_ARGS__>; \
134 static constexpr size_t COUNT = Table::COUNT; \
135 static constexpr uint8_t LAST_INDEX = COUNT - 1; \
136 static constexpr size_t BLOB_SIZE = Table::BLOB_SIZE; \
137 static constexpr size_t MAX_LENGTH = Table::MAX_LENGTH; \
138 static constexpr auto BLOB ESPHOME_PROGMEM_STRING_TABLE_STORAGE = Table::make_blob(); \
139 static constexpr auto OFFSETS ESPHOME_PROGMEM_STRING_TABLE_STORAGE = Table::make_offsets(); \
140 static const char *get_(uint8_t idx, uint8_t fallback) { \
145 if constexpr (Table::INDEXED) \
146 return std::launder(&BLOB[::esphome::progmem_read_byte(&OFFSETS[idx])]); \
148 return std::launder(::esphome::progmem_skip_strings(BLOB.data(), idx)); \
150 static ::ProgmemStr get_progmem_str(uint8_t idx, uint8_t fallback) { \
151 return reinterpret_cast<::ProgmemStr>(get_(idx, fallback)); \
153 static const ::esphome::LogString *get_log_str(uint8_t idx, uint8_t fallback) { \
154 return reinterpret_cast<const ::esphome::LogString *>(get_(idx, fallback)); \
std::string progmem_string(ProgmemStr str)
Copies a string stored with ESPHOME_F into a std::string.
const char * progmem_skip_strings(const char *blob, uint8_t idx)
Skip idx strings in a packed NUL-separated blob that may live in flash.
const __FlashStringHelper * ProgmemStr
Helper for C++20 string literal template arguments.
constexpr size_t size() const
constexpr FixedString(const char(&str)[N])
Compile-time string table that packs strings into a single blob with offset lookup.
static constexpr size_t COUNT
static constexpr size_t MAX_LENGTH
Length of the longest string, for sizing a RAM copy on ESP8266.
static constexpr size_t BLOB_SIZE
static constexpr auto make_blob()
Generate packed string blob at compile time.
static constexpr bool INDEXED
One-byte offsets address up to 255 bytes; a larger blob has no offset table and is scanned instead,...
static constexpr auto make_offsets()
Generate offset table at compile time.