ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
string_ref.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cstring>
5#include <iterator>
6#include <memory>
7#include <string>
9
10#ifdef USE_JSON
12#endif // USE_JSON
13
14#ifdef USE_ESP8266
15#include <pgmspace.h>
16#endif // USE_ESP8266
17
18namespace esphome {
19
26class StringRef {
27 public:
28 using traits_type = std::char_traits<char>;
29 using value_type = traits_type::char_type;
30 using allocator_type = std::allocator<char>;
31 using size_type = std::allocator_traits<allocator_type>::size_type;
32 using difference_type = std::allocator_traits<allocator_type>::difference_type;
33 using const_reference = const value_type &;
34 using const_pointer = const value_type *;
36 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
37
38 constexpr StringRef() : base_(""), len_(0) {}
39 explicit StringRef(const std::string &s) : base_(s.c_str()), len_(s.size()) {}
40 explicit StringRef(const char *s) : base_(s), len_(strlen(s)) {}
41 constexpr StringRef(const char *s, size_t n) : base_(s), len_(n) {}
42 template<typename CharT>
43 constexpr StringRef(const CharT *s, size_t n) : base_(reinterpret_cast<const char *>(s)), len_(n) {}
44 template<typename InputIt>
45 StringRef(InputIt first, InputIt last)
46 : base_(reinterpret_cast<const char *>(&*first)), len_(std::distance(first, last)) {}
47 template<typename InputIt>
48 StringRef(InputIt *first, InputIt *last)
49 : base_(reinterpret_cast<const char *>(first)), len_(std::distance(first, last)) {}
50 template<typename CharT, size_t N> constexpr static StringRef from_lit(const CharT (&s)[N]) {
51 return StringRef{s, N - 1};
52 }
53 static StringRef from_maybe_nullptr(const char *s) {
54 if (s == nullptr) {
55 return StringRef();
56 }
57
58 return StringRef(s);
59 }
60
61 constexpr const_iterator begin() const { return base_; };
62 constexpr const_iterator cbegin() const { return base_; };
63
64 constexpr const_iterator end() const { return base_ + len_; };
65 constexpr const_iterator cend() const { return base_ + len_; };
66
67 const_reverse_iterator rbegin() const { return const_reverse_iterator{base_ + len_}; }
68 const_reverse_iterator crbegin() const { return const_reverse_iterator{base_ + len_}; }
69
72
73 constexpr const char *c_str() const { return base_; }
74 constexpr size_type size() const { return len_; }
75 constexpr size_type length() const { return len_; }
76 constexpr bool empty() const { return len_ == 0; }
77 constexpr const_reference operator[](size_type pos) const { return *(base_ + pos); }
78
79 std::string str() const { return std::string(base_, len_); }
80 const uint8_t *byte() const { return reinterpret_cast<const uint8_t *>(base_); }
81
82 operator std::string() const { return str(); }
83
85 int compare(const StringRef &other) const {
86 int result = std::memcmp(base_, other.base_, std::min(len_, other.len_));
87 if (result != 0)
88 return result;
89 if (len_ < other.len_)
90 return -1;
91 if (len_ > other.len_)
92 return 1;
93 return 0;
94 }
95 int compare(const char *s) const { return compare(StringRef(s)); }
96 int compare(const std::string &s) const { return compare(StringRef(s)); }
97
100 size_type find(const char *s, size_type pos = 0) const {
101 if (pos >= len_)
102 return std::string::npos;
103 const char *result = std::strstr(base_ + pos, s);
104 // Verify entire match is within bounds (strstr searches to null terminator)
105 if (result && result + std::strlen(s) <= base_ + len_)
106 return static_cast<size_type>(result - base_);
107 return std::string::npos;
108 }
109 size_type find(char c, size_type pos = 0) const {
110 if (pos >= len_)
111 return std::string::npos;
112 const void *result = std::memchr(base_ + pos, static_cast<unsigned char>(c), len_ - pos);
113 return result ? static_cast<size_type>(static_cast<const char *>(result) - base_) : std::string::npos;
114 }
115
117 std::string substr(size_type pos = 0, size_type count = std::string::npos) const {
118 if (pos >= len_)
119 return std::string();
120 size_type actual_count = (count == std::string::npos || pos + count > len_) ? len_ - pos : count;
121 return std::string(base_ + pos, actual_count);
122 }
123
124 private:
125 const char *base_;
126 size_type len_;
127};
128
129inline bool operator==(const StringRef &lhs, const StringRef &rhs) {
130 return lhs.size() == rhs.size() && std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
131}
132
133inline bool operator==(const StringRef &lhs, const std::string &rhs) {
134 return lhs.size() == rhs.size() && std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
135}
136
137inline bool operator==(const std::string &lhs, const StringRef &rhs) { return rhs == lhs; }
138
139inline bool operator==(const StringRef &lhs, const char *rhs) {
140 return lhs.size() == strlen(rhs) && std::equal(std::begin(lhs), std::end(lhs), rhs);
141}
142
143inline bool operator==(const char *lhs, const StringRef &rhs) { return rhs == lhs; }
144
145inline bool operator!=(const StringRef &lhs, const StringRef &rhs) { return !(lhs == rhs); }
146
147inline bool operator!=(const StringRef &lhs, const std::string &rhs) { return !(lhs == rhs); }
148
149inline bool operator!=(const std::string &lhs, const StringRef &rhs) { return !(rhs == lhs); }
150
151inline bool operator!=(const StringRef &lhs, const char *rhs) { return !(lhs == rhs); }
152
153inline bool operator!=(const char *lhs, const StringRef &rhs) { return !(rhs == lhs); }
154
155#ifdef USE_ESP8266
156inline bool operator==(const StringRef &lhs, const __FlashStringHelper *rhs) {
157 PGM_P p = reinterpret_cast<PGM_P>(rhs);
158 size_t rhs_len = strlen_P(p);
159 if (lhs.size() != rhs_len) {
160 return false;
161 }
162 return memcmp_P(lhs.c_str(), p, rhs_len) == 0;
163}
164
165inline bool operator==(const __FlashStringHelper *lhs, const StringRef &rhs) { return rhs == lhs; }
166
167inline bool operator!=(const StringRef &lhs, const __FlashStringHelper *rhs) { return !(lhs == rhs); }
168
169inline bool operator!=(const __FlashStringHelper *lhs, const StringRef &rhs) { return !(rhs == lhs); }
170#endif // USE_ESP8266
171
172inline bool operator<(const StringRef &lhs, const StringRef &rhs) {
173 return std::lexicographical_compare(std::begin(lhs), std::end(lhs), std::begin(rhs), std::end(rhs));
174}
175
176inline std::string &operator+=(std::string &lhs, const StringRef &rhs) {
177 lhs.append(rhs.c_str(), rhs.size());
178 return lhs;
179}
180
181inline std::string operator+(const char *lhs, const StringRef &rhs) {
182 auto str = std::string(lhs);
183 str.append(rhs.c_str(), rhs.size());
184 return str;
185}
186
187inline std::string operator+(const StringRef &lhs, const char *rhs) {
188 auto str = lhs.str();
189 str.append(rhs);
190 return str;
191}
192
193inline std::string operator+(const StringRef &lhs, const std::string &rhs) {
194 auto str = lhs.str();
195 str.append(rhs);
196 return str;
197}
198
199inline std::string operator+(const std::string &lhs, const StringRef &rhs) {
200 std::string str(lhs);
201 str.append(rhs.c_str(), rhs.size());
202 return str;
203}
204// String conversion functions for ADL compatibility (allows stoi(x) where x is StringRef)
205// Must be in esphome namespace for ADL to find them. Uses strtol/strtod directly to avoid heap allocation.
206namespace internal {
207// NOLINTBEGIN(google-runtime-int)
208template<typename R, typename F> inline R parse_number(const StringRef &str, size_t *pos, F conv) {
209 char *end;
210 R result = conv(str.c_str(), &end);
211 // Set pos to 0 on conversion failure (when no characters consumed), otherwise index after number
212 if (pos)
213 *pos = (end == str.c_str()) ? 0 : static_cast<size_t>(end - str.c_str());
214 return result;
215}
216template<typename R, typename F> inline R parse_number(const StringRef &str, size_t *pos, int base, F conv) {
217 char *end;
218 R result = conv(str.c_str(), &end, base);
219 // Set pos to 0 on conversion failure (when no characters consumed), otherwise index after number
220 if (pos)
221 *pos = (end == str.c_str()) ? 0 : static_cast<size_t>(end - str.c_str());
222 return result;
223}
224// NOLINTEND(google-runtime-int)
225} // namespace internal
226// NOLINTBEGIN(readability-identifier-naming,google-runtime-int)
227inline int stoi(const StringRef &str, size_t *pos = nullptr, int base = 10) {
228 return static_cast<int>(internal::parse_number<long>(str, pos, base, std::strtol));
229}
230inline long stol(const StringRef &str, size_t *pos = nullptr, int base = 10) {
231 return internal::parse_number<long>(str, pos, base, std::strtol);
232}
233inline float stof(const StringRef &str, size_t *pos = nullptr) {
234 return internal::parse_number<float>(str, pos, std::strtof);
235}
236inline double stod(const StringRef &str, size_t *pos = nullptr) {
237 return internal::parse_number<double>(str, pos, std::strtod);
238}
239// NOLINTEND(readability-identifier-naming,google-runtime-int)
240
241#ifdef USE_JSON
242// NOLINTNEXTLINE(readability-identifier-naming)
243inline void convertToJson(const StringRef &src, JsonVariant dst) { dst.set(src.c_str()); }
244#endif // USE_JSON
245
246} // namespace esphome
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
StringRef(const std::string &s)
Definition string_ref.h:39
std::allocator< char > allocator_type
Definition string_ref.h:30
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition string_ref.h:36
size_type find(char c, size_type pos=0) const
Definition string_ref.h:109
const_reverse_iterator crend() const
Definition string_ref.h:71
constexpr const char * c_str() const
Definition string_ref.h:73
constexpr StringRef(const CharT *s, size_t n)
Definition string_ref.h:43
std::char_traits< char > traits_type
Definition string_ref.h:28
const value_type * const_pointer
Definition string_ref.h:34
constexpr size_type length() const
Definition string_ref.h:75
int compare(const StringRef &other) const
Compare (compatible with std::string::compare)
Definition string_ref.h:85
size_type find(const char *s, size_type pos=0) const
Find first occurrence of substring, returns std::string::npos if not found.
Definition string_ref.h:100
traits_type::char_type value_type
Definition string_ref.h:29
constexpr const_iterator cend() const
Definition string_ref.h:65
constexpr const_reference operator[](size_type pos) const
Definition string_ref.h:77
constexpr const_iterator begin() const
Definition string_ref.h:61
int compare(const char *s) const
Definition string_ref.h:95
constexpr StringRef(const char *s, size_t n)
Definition string_ref.h:41
StringRef(const char *s)
Definition string_ref.h:40
constexpr bool empty() const
Definition string_ref.h:76
const_reverse_iterator rbegin() const
Definition string_ref.h:67
constexpr size_type size() const
Definition string_ref.h:74
static constexpr StringRef from_lit(const CharT(&s)[N])
Definition string_ref.h:50
constexpr const_iterator end() const
Definition string_ref.h:64
std::string str() const
Definition string_ref.h:79
std::allocator_traits< allocator_type >::size_type size_type
Definition string_ref.h:31
std::string substr(size_type pos=0, size_type count=std::string::npos) const
Return substring as std::string.
Definition string_ref.h:117
constexpr StringRef()
Definition string_ref.h:38
std::allocator_traits< allocator_type >::difference_type difference_type
Definition string_ref.h:32
StringRef(InputIt first, InputIt last)
Definition string_ref.h:45
static StringRef from_maybe_nullptr(const char *s)
Definition string_ref.h:53
int compare(const std::string &s) const
Definition string_ref.h:96
const value_type & const_reference
Definition string_ref.h:33
const_reverse_iterator rend() const
Definition string_ref.h:70
constexpr const_iterator cbegin() const
Definition string_ref.h:62
const_reverse_iterator crbegin() const
Definition string_ref.h:68
const_pointer const_iterator
Definition string_ref.h:35
const uint8_t * byte() const
Definition string_ref.h:80
StringRef(InputIt *first, InputIt *last)
Definition string_ref.h:48
R parse_number(const StringRef &str, size_t *pos, F conv)
Definition string_ref.h:208
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string operator+(const char *lhs, const StringRef &rhs)
Definition string_ref.h:181
bool operator==(optional< T > const &x, optional< U > const &y)
Definition optional.h:118
double stod(const StringRef &str, size_t *pos=nullptr)
Definition string_ref.h:236
bool operator!=(optional< T > const &x, optional< U > const &y)
Definition optional.h:122
void convertToJson(const StringRef &src, JsonVariant dst)
Definition string_ref.h:243
std::string & operator+=(std::string &lhs, const StringRef &rhs)
Definition string_ref.h:176
size_t size_t pos
Definition helpers.h:854
float stof(const StringRef &str, size_t *pos=nullptr)
Definition string_ref.h:233
int stoi(const StringRef &str, size_t *pos=nullptr, int base=10)
Definition string_ref.h:227
bool operator<(optional< T > const &x, optional< U > const &y)
Definition optional.h:126
long stol(const StringRef &str, size_t *pos=nullptr, int base=10)
Definition string_ref.h:230
uint8_t end[39]
Definition sun_gtil2.cpp:17