diff --git a/esphome/core/string_ref.h b/esphome/core/string_ref.h index 7501d06ce4..3b209a7c7f 100644 --- a/esphome/core/string_ref.h +++ b/esphome/core/string_ref.h @@ -81,7 +81,8 @@ class StringRef { operator std::string() const { return str(); } - /// Find first occurrence of substring, returns std::string::npos if not found + /// Find first occurrence of substring, returns std::string::npos if not found. + /// Note: Requires the underlying string to be null-terminated. size_type find(const char *s, size_type pos = 0) const { if (pos >= len_) return std::string::npos; @@ -91,8 +92,8 @@ class StringRef { size_type find(char c, size_type pos = 0) const { if (pos >= len_) return std::string::npos; - const char *result = std::strchr(base_ + pos, c); - return (result && result < base_ + len_) ? static_cast(result - base_) : std::string::npos; + const void *result = std::memchr(base_ + pos, static_cast(c), len_ - pos); + return result ? static_cast(static_cast(result) - base_) : std::string::npos; } /// Return substring as std::string