ESPHome 2026.1.1
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 bool empty() const { return len_ == 0; }
76 constexpr const_reference operator[](size_type pos) const { return *(base_ + pos); }
77
78 std::string str() const { return std::string(base_, len_); }
79 const uint8_t *byte() const { return reinterpret_cast<const uint8_t *>(base_); }
80
81 operator std::string() const { return str(); }
82
83 private:
84 const char *base_;
85 size_type len_;
86};
87
88inline bool operator==(const StringRef &lhs, const StringRef &rhs) {
89 return lhs.size() == rhs.size() && std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
90}
91
92inline bool operator==(const StringRef &lhs, const std::string &rhs) {
93 return lhs.size() == rhs.size() && std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
94}
95
96inline bool operator==(const std::string &lhs, const StringRef &rhs) { return rhs == lhs; }
97
98inline bool operator==(const StringRef &lhs, const char *rhs) {
99 return lhs.size() == strlen(rhs) && std::equal(std::begin(lhs), std::end(lhs), rhs);
100}
101
102inline bool operator==(const char *lhs, const StringRef &rhs) { return rhs == lhs; }
103
104inline bool operator!=(const StringRef &lhs, const StringRef &rhs) { return !(lhs == rhs); }
105
106inline bool operator!=(const StringRef &lhs, const std::string &rhs) { return !(lhs == rhs); }
107
108inline bool operator!=(const std::string &lhs, const StringRef &rhs) { return !(rhs == lhs); }
109
110inline bool operator!=(const StringRef &lhs, const char *rhs) { return !(lhs == rhs); }
111
112inline bool operator!=(const char *lhs, const StringRef &rhs) { return !(rhs == lhs); }
113
114#ifdef USE_ESP8266
115inline bool operator==(const StringRef &lhs, const __FlashStringHelper *rhs) {
116 PGM_P p = reinterpret_cast<PGM_P>(rhs);
117 size_t rhs_len = strlen_P(p);
118 if (lhs.size() != rhs_len) {
119 return false;
120 }
121 return memcmp_P(lhs.c_str(), p, rhs_len) == 0;
122}
123
124inline bool operator==(const __FlashStringHelper *lhs, const StringRef &rhs) { return rhs == lhs; }
125
126inline bool operator!=(const StringRef &lhs, const __FlashStringHelper *rhs) { return !(lhs == rhs); }
127
128inline bool operator!=(const __FlashStringHelper *lhs, const StringRef &rhs) { return !(rhs == lhs); }
129#endif // USE_ESP8266
130
131inline bool operator<(const StringRef &lhs, const StringRef &rhs) {
132 return std::lexicographical_compare(std::begin(lhs), std::end(lhs), std::begin(rhs), std::end(rhs));
133}
134
135inline std::string &operator+=(std::string &lhs, const StringRef &rhs) {
136 lhs.append(rhs.c_str(), rhs.size());
137 return lhs;
138}
139
140inline std::string operator+(const char *lhs, const StringRef &rhs) {
141 auto str = std::string(lhs);
142 str.append(rhs.c_str(), rhs.size());
143 return str;
144}
145
146inline std::string operator+(const StringRef &lhs, const char *rhs) {
147 auto str = lhs.str();
148 str.append(rhs);
149 return str;
150}
151
152inline std::string operator+(const StringRef &lhs, const std::string &rhs) {
153 auto str = lhs.str();
154 str.append(rhs);
155 return str;
156}
157
158inline std::string operator+(const std::string &lhs, const StringRef &rhs) {
159 std::string str(lhs);
160 str.append(rhs.c_str(), rhs.size());
161 return str;
162}
163#ifdef USE_JSON
164// NOLINTNEXTLINE(readability-identifier-naming)
165inline void convertToJson(const StringRef &src, JsonVariant dst) { dst.set(src.c_str()); }
166#endif // USE_JSON
167
168} // 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
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
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:76
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:75
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:78
std::allocator_traits< allocator_type >::size_type size_type
Definition string_ref.h:31
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:79
StringRef(InputIt *first, InputIt *last)
Definition string_ref.h:48
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:140
bool operator==(optional< T > const &x, optional< U > const &y)
Definition optional.h:118
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:165
std::string & operator+=(std::string &lhs, const StringRef &rhs)
Definition string_ref.h:135
bool operator<(optional< T > const &x, optional< U > const &y)
Definition optional.h:126