mirror of
https://github.com/esphome/esphome.git
synced 2026-10-02 01:10:22 +00:00
[core] Add StringRef::starts_with (#18142)
This commit is contained in:
@@ -76,6 +76,13 @@ class StringRef {
|
||||
constexpr bool empty() const { return len_ == 0; }
|
||||
constexpr const_reference operator[](size_type pos) const { return *(base_ + pos); }
|
||||
|
||||
/// True if the view begins with the given prefix (std::string::starts_with-like)
|
||||
bool starts_with(const StringRef &prefix) const {
|
||||
return len_ >= prefix.len_ && std::memcmp(base_, prefix.base_, prefix.len_) == 0;
|
||||
}
|
||||
bool starts_with(const char *prefix) const { return this->starts_with(StringRef(prefix)); }
|
||||
bool starts_with(const std::string &prefix) const { return this->starts_with(StringRef(prefix)); }
|
||||
|
||||
/// Copy characters to destination buffer (std::string::copy-like, but returns 0 instead of throwing on out-of-range)
|
||||
size_type copy(char *dest, size_type count, size_type pos = 0) const {
|
||||
if (pos >= len_)
|
||||
|
||||
Reference in New Issue
Block a user