ESPHome 2026.2.0
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
86 size_type find(const char *s, size_type pos = 0) const {
87 if (pos >= len_)
88 return std::string::npos;
89 const char *result = std::strstr(base_ + pos, s);
90 // Verify entire match is within bounds (strstr searches to null terminator)
91 if (result && result + std::strlen(s) <= base_ + len_)
92 return static_cast<size_type>(result - base_);
93 return std::string::npos;
94 }
95 size_type find(char c, size_type pos = 0) const {
96 if (pos >= len_)
97 return std::string::npos;
98 const void *result = std::memchr(base_ + pos, static_cast<unsigned char>(c), len_ - pos);
99 return result ? static_cast<size_type>(static_cast<const char *>(result) - base_) : std::string::npos;
100 }
101
103 std::string substr(size_type pos = 0, size_type count = std::string::npos) const {
104 if (pos >= len_)
105 return std::string();
106 size_type actual_count = (count == std::string::npos || pos + count > len_) ? len_ - pos : count;
107 return std::string(base_ + pos, actual_count);
108 }
109
110 private:
111 const char *base_;
112 size_type len_;
113};
114
115inline bool operator==(const StringRef &lhs, const StringRef &rhs) {
116 return lhs.size() == rhs.size() && std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
117}
118
119inline bool operator==(const StringRef &lhs, const std::string &rhs) {
120 return lhs.size() == rhs.size() && std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
121}
122
123inline bool operator==(const std::string &lhs, const StringRef &rhs) { return rhs == lhs; }
124
125inline bool operator==(const StringRef &lhs, const char *rhs) {
126 return lhs.size() == strlen(rhs) && std::equal(std::begin(lhs), std::end(lhs), rhs);
127}
128
129inline bool operator==(const char *lhs, const StringRef &rhs) { return rhs == lhs; }
130
131inline bool operator!=(const StringRef &lhs, const StringRef &rhs) { return !(lhs == rhs); }
132
133inline bool operator!=(const StringRef &lhs, const std::string &rhs) { return !(lhs == rhs); }
134
135inline bool operator!=(const std::string &lhs, const StringRef &rhs) { return !(rhs == lhs); }
136
137inline bool operator!=(const StringRef &lhs, const char *rhs) { return !(lhs == rhs); }
138
139inline bool operator!=(const char *lhs, const StringRef &rhs) { return !(rhs == lhs); }
140
141#ifdef USE_ESP8266
142inline bool operator==(const StringRef &lhs, const __FlashStringHelper *rhs) {
143 PGM_P p = reinterpret_cast<PGM_P>(rhs);
144 size_t rhs_len = strlen_P(p);
145 if (lhs.size() != rhs_len) {
146 return false;
147 }
148 return memcmp_P(lhs.c_str(), p, rhs_len) == 0;
149}
150
151inline bool operator==(const __FlashStringHelper *lhs, const StringRef &rhs) { return rhs == lhs; }
152
153inline bool operator!=(const StringRef &lhs, const __FlashStringHelper *rhs) { return !(lhs == rhs); }
154
155inline bool operator!=(const __FlashStringHelper *lhs, const StringRef &rhs) { return !(rhs == lhs); }
156#endif // USE_ESP8266
157
158inline bool operator<(const StringRef &lhs, const StringRef &rhs) {
159 return std::lexicographical_compare(std::begin(lhs), std::end(lhs), std::begin(rhs), std::end(rhs));
160}
161
162inline std::string &operator+=(std::string &lhs, const StringRef &rhs) {
163 lhs.append(rhs.c_str(), rhs.size());
164 return lhs;
165}
166
167inline std::string operator+(const char *lhs, const StringRef &rhs) {
168 auto str = std::string(lhs);
169 str.append(rhs.c_str(), rhs.size());
170 return str;
171}
172
173inline std::string operator+(const StringRef &lhs, const char *rhs) {
174 auto str = lhs.str();
175 str.append(rhs);
176 return str;
177}
178
179inline std::string operator+(const StringRef &lhs, const std::string &rhs) {
180 auto str = lhs.str();
181 str.append(rhs);
182 return str;
183}
184
185inline std::string operator+(const std::string &lhs, const StringRef &rhs) {
186 std::string str(lhs);
187 str.append(rhs.c_str(), rhs.size());
188 return str;
189}
190// String conversion functions for ADL compatibility (allows stoi(x) where x is StringRef)
191// Must be in esphome namespace for ADL to find them. Uses strtol/strtod directly to avoid heap allocation.
192namespace internal {
193// NOLINTBEGIN(google-runtime-int)
194template<typename R, typename F> inline R parse_number(const StringRef &str, size_t *pos, F conv) {
195 char *end;
196 R result = conv(str.c_str(), &end);
197 // Set pos to 0 on conversion failure (when no characters consumed), otherwise index after number
198 if (pos)
199 *pos = (end == str.c_str()) ? 0 : static_cast<size_t>(end - str.c_str());
200 return result;
201}
202template<typename R, typename F> inline R parse_number(const StringRef &str, size_t *pos, int base, F conv) {
203 char *end;
204 R result = conv(str.c_str(), &end, base);
205 // Set pos to 0 on conversion failure (when no characters consumed), otherwise index after number
206 if (pos)
207 *pos = (end == str.c_str()) ? 0 : static_cast<size_t>(end - str.c_str());
208 return result;
209}
210// NOLINTEND(google-runtime-int)
211} // namespace internal
212// NOLINTBEGIN(readability-identifier-naming,google-runtime-int)
213inline int stoi(const StringRef &str, size_t *pos = nullptr, int base = 10) {
214 return static_cast<int>(internal::parse_number<long>(str, pos, base, std::strtol));
215}
216inline long stol(const StringRef &str, size_t *pos = nullptr, int base = 10) {
217 return internal::parse_number<long>(str, pos, base, std::strtol);
218}
219inline float stof(const StringRef &str, size_t *pos = nullptr) {
220 return internal::parse_number<float>(str, pos, std::strtof);
221}
222inline double stod(const StringRef &str, size_t *pos = nullptr) {
223 return internal::parse_number<double>(str, pos, std::strtod);
224}
225// NOLINTEND(readability-identifier-naming,google-runtime-int)
226
227#ifdef USE_JSON
228// NOLINTNEXTLINE(readability-identifier-naming)
229inline void convertToJson(const StringRef &src, JsonVariant dst) { dst.set(src.c_str()); }
230#endif // USE_JSON
231
232} // 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:95
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
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:86
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
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:103
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
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:194
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:167
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:222
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:229
std::string & operator+=(std::string &lhs, const StringRef &rhs)
Definition string_ref.h:162
size_t size_t pos
Definition helpers.h:729
float stof(const StringRef &str, size_t *pos=nullptr)
Definition string_ref.h:219
int stoi(const StringRef &str, size_t *pos=nullptr, int base=10)
Definition string_ref.h:213
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:216
uint8_t end[39]
Definition sun_gtil2.cpp:17