1
0
mirror of https://github.com/esphome/esphome.git synced 2026-02-08 08:41:59 +00:00

avoid breaking

This commit is contained in:
J. Nick Koston
2026-01-17 08:32:31 -10:00
parent 37025d62e0
commit 65cdb97f06

View File

@@ -72,6 +72,7 @@ class StringRef {
constexpr const char *c_str() const { return base_; }
constexpr size_type size() const { return len_; }
constexpr size_type length() const { return len_; }
constexpr bool empty() const { return len_ == 0; }
constexpr const_reference operator[](size_type pos) const { return *(base_ + pos); }
@@ -80,6 +81,29 @@ class StringRef {
operator std::string() const { return str(); }
/// Find first occurrence of substring, returns npos if not found
static constexpr size_type npos = static_cast<size_type>(-1);
size_type find(const char *s, size_type pos = 0) const {
if (pos >= len_)
return npos;
const char *result = std::strstr(base_ + pos, s);
return result ? static_cast<size_type>(result - base_) : npos;
}
size_type find(char c, size_type pos = 0) const {
if (pos >= len_)
return npos;
const char *result = std::strchr(base_ + pos, c);
return (result && result < base_ + len_) ? static_cast<size_type>(result - base_) : npos;
}
/// Return substring as std::string
std::string substr(size_type pos = 0, size_type count = npos) const {
if (pos >= len_)
return std::string();
size_type actual_count = (count == npos || pos + count > len_) ? len_ - pos : count;
return std::string(base_ + pos, actual_count);
}
private:
const char *base_;
size_type len_;