mirror of
https://github.com/esphome/esphome.git
synced 2026-03-23 16:49:21 +08:00
[core] Add copy() method to StringRef for std::string compatibility (#15028)
This commit is contained in:
@@ -76,6 +76,15 @@ class StringRef {
|
||||
constexpr bool empty() const { return len_ == 0; }
|
||||
constexpr const_reference operator[](size_type pos) const { return *(base_ + pos); }
|
||||
|
||||
/// 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_)
|
||||
return 0;
|
||||
size_type actual = (count > len_ - pos) ? len_ - pos : count;
|
||||
std::memcpy(dest, base_ + pos, actual);
|
||||
return actual;
|
||||
}
|
||||
|
||||
std::string str() const { return std::string(base_, len_); }
|
||||
const uint8_t *byte() const { return reinterpret_cast<const uint8_t *>(base_); }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user