ESPHome  2024.4.1
string_ref.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstring>
4 #include <iterator>
5 #include <memory>
6 #include <string>
7 #include "esphome/core/defines.h"
8 
9 #ifdef USE_JSON
11 #endif // USE_JSON
12 
13 namespace esphome {
14 
21 class StringRef {
22  public:
23  using traits_type = std::char_traits<char>;
24  using value_type = traits_type::char_type;
25  using allocator_type = std::allocator<char>;
26  using size_type = std::allocator_traits<allocator_type>::size_type;
27  using difference_type = std::allocator_traits<allocator_type>::difference_type;
28  using const_reference = const value_type &;
29  using const_pointer = const value_type *;
31  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
32 
33  constexpr StringRef() : base_(""), len_(0) {}
34  explicit StringRef(const std::string &s) : base_(s.c_str()), len_(s.size()) {}
35  explicit StringRef(const char *s) : base_(s), len_(strlen(s)) {}
36  constexpr StringRef(const char *s, size_t n) : base_(s), len_(n) {}
37  template<typename CharT>
38  constexpr StringRef(const CharT *s, size_t n) : base_(reinterpret_cast<const char *>(s)), len_(n) {}
39  template<typename InputIt>
40  StringRef(InputIt first, InputIt last)
41  : base_(reinterpret_cast<const char *>(&*first)), len_(std::distance(first, last)) {}
42  template<typename InputIt>
43  StringRef(InputIt *first, InputIt *last)
44  : base_(reinterpret_cast<const char *>(first)), len_(std::distance(first, last)) {}
45  template<typename CharT, size_t N> constexpr static StringRef from_lit(const CharT (&s)[N]) {
46  return StringRef{s, N - 1};
47  }
48  static StringRef from_maybe_nullptr(const char *s) {
49  if (s == nullptr) {
50  return StringRef();
51  }
52 
53  return StringRef(s);
54  }
55 
56  constexpr const_iterator begin() const { return base_; };
57  constexpr const_iterator cbegin() const { return base_; };
58 
59  constexpr const_iterator end() const { return base_ + len_; };
60  constexpr const_iterator cend() const { return base_ + len_; };
61 
62  const_reverse_iterator rbegin() const { return const_reverse_iterator{base_ + len_}; }
63  const_reverse_iterator crbegin() const { return const_reverse_iterator{base_ + len_}; }
64 
67 
68  constexpr const char *c_str() const { return base_; }
69  constexpr size_type size() const { return len_; }
70  constexpr bool empty() const { return len_ == 0; }
71  constexpr const_reference operator[](size_type pos) const { return *(base_ + pos); }
72 
73  std::string str() const { return std::string(base_, len_); }
74  const uint8_t *byte() const { return reinterpret_cast<const uint8_t *>(base_); }
75 
76  operator std::string() const { return str(); }
77 
78  private:
79  const char *base_;
80  size_type len_;
81 };
82 
83 inline bool operator==(const StringRef &lhs, const StringRef &rhs) {
84  return lhs.size() == rhs.size() && std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
85 }
86 
87 inline bool operator==(const StringRef &lhs, const std::string &rhs) {
88  return lhs.size() == rhs.size() && std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
89 }
90 
91 inline bool operator==(const std::string &lhs, const StringRef &rhs) { return rhs == lhs; }
92 
93 inline bool operator==(const StringRef &lhs, const char *rhs) {
94  return lhs.size() == strlen(rhs) && std::equal(std::begin(lhs), std::end(lhs), rhs);
95 }
96 
97 inline bool operator==(const char *lhs, const StringRef &rhs) { return rhs == lhs; }
98 
99 inline bool operator!=(const StringRef &lhs, const StringRef &rhs) { return !(lhs == rhs); }
100 
101 inline bool operator!=(const StringRef &lhs, const std::string &rhs) { return !(lhs == rhs); }
102 
103 inline bool operator!=(const std::string &lhs, const StringRef &rhs) { return !(rhs == lhs); }
104 
105 inline bool operator!=(const StringRef &lhs, const char *rhs) { return !(lhs == rhs); }
106 
107 inline bool operator!=(const char *lhs, const StringRef &rhs) { return !(rhs == lhs); }
108 
109 inline bool operator<(const StringRef &lhs, const StringRef &rhs) {
110  return std::lexicographical_compare(std::begin(lhs), std::end(lhs), std::begin(rhs), std::end(rhs));
111 }
112 
113 inline std::string &operator+=(std::string &lhs, const StringRef &rhs) {
114  lhs.append(rhs.c_str(), rhs.size());
115  return lhs;
116 }
117 
118 inline std::string operator+(const char *lhs, const StringRef &rhs) {
119  auto str = std::string(lhs);
120  str.append(rhs.c_str(), rhs.size());
121  return str;
122 }
123 
124 inline std::string operator+(const StringRef &lhs, const char *rhs) {
125  auto str = lhs.str();
126  str.append(rhs);
127  return str;
128 }
129 
130 #ifdef USE_JSON
131 // NOLINTNEXTLINE(readability-identifier-naming)
132 void convertToJson(const StringRef &src, JsonVariant dst);
133 #endif // USE_JSON
134 
135 } // namespace esphome
std::string str() const
Definition: string_ref.h:73
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: string_ref.h:31
const value_type * const_pointer
Definition: string_ref.h:29
std::char_traits< char > traits_type
Definition: string_ref.h:23
StringRef(const char *s)
Definition: string_ref.h:35
StringRef is a reference to a string owned by something else.
Definition: string_ref.h:21
std::allocator< char > allocator_type
Definition: string_ref.h:25
constexpr const_reference operator[](size_type pos) const
Definition: string_ref.h:71
STL namespace.
void convertToJson(const StringRef &src, JsonVariant dst)
Definition: string_ref.cpp:8
const value_type & const_reference
Definition: string_ref.h:28
constexpr bool empty() const
Definition: string_ref.h:70
static StringRef from_maybe_nullptr(const char *s)
Definition: string_ref.h:48
const_reverse_iterator rend() const
Definition: string_ref.h:65
StringRef(InputIt *first, InputIt *last)
Definition: string_ref.h:43
const_reverse_iterator crend() const
Definition: string_ref.h:66
static constexpr StringRef from_lit(const CharT(&s)[N])
Definition: string_ref.h:45
constexpr const_iterator cend() const
Definition: string_ref.h:60
constexpr StringRef(const char *s, size_t n)
Definition: string_ref.h:36
StringRef(InputIt first, InputIt last)
Definition: string_ref.h:40
bool operator!=(optional< T > const &x, optional< U > const &y)
Definition: optional.h:118
std::string & operator+=(std::string &lhs, const StringRef &rhs)
Definition: string_ref.h:113
bool operator<(optional< T > const &x, optional< U > const &y)
Definition: optional.h:122
std::string operator+(const char *lhs, const StringRef &rhs)
Definition: string_ref.h:118
constexpr const char * c_str() const
Definition: string_ref.h:68
std::allocator_traits< allocator_type >::size_type size_type
Definition: string_ref.h:26
constexpr const_iterator begin() const
Definition: string_ref.h:56
constexpr const_iterator end() const
Definition: string_ref.h:59
const_reverse_iterator rbegin() const
Definition: string_ref.h:62
bool operator==(optional< T > const &x, optional< U > const &y)
Definition: optional.h:114
constexpr StringRef()
Definition: string_ref.h:33
const uint8_t * byte() const
Definition: string_ref.h:74
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
const_reverse_iterator crbegin() const
Definition: string_ref.h:63
constexpr const_iterator cbegin() const
Definition: string_ref.h:57
uint8_t end[39]
Definition: sun_gtil2.cpp:31
StringRef(const std::string &s)
Definition: string_ref.h:34
constexpr size_type size() const
Definition: string_ref.h:69
std::allocator_traits< allocator_type >::difference_type difference_type
Definition: string_ref.h:27
traits_type::char_type value_type
Definition: string_ref.h:24
const_pointer const_iterator
Definition: string_ref.h:30
constexpr StringRef(const CharT *s, size_t n)
Definition: string_ref.h:38