ESPHome 2026.2.1
Loading...
Searching...
No Matches
esphome::StringRef Class Reference

StringRef is a reference to a string owned by something else. More...

#include <string_ref.h>

Public Types

using traits_type = std::char_traits<char>
 
using value_type = traits_type::char_type
 
using allocator_type = std::allocator<char>
 
using size_type = std::allocator_traits<allocator_type>::size_type
 
using difference_type = std::allocator_traits<allocator_type>::difference_type
 
using const_reference = const value_type &
 
using const_pointer = const value_type *
 
using const_iterator = const_pointer
 
using const_reverse_iterator = std::reverse_iterator<const_iterator>
 

Public Member Functions

constexpr StringRef ()
 
 StringRef (const std::string &s)
 
 StringRef (const char *s)
 
constexpr StringRef (const char *s, size_t n)
 
template<typename CharT >
constexpr StringRef (const CharT *s, size_t n)
 
template<typename InputIt >
 StringRef (InputIt first, InputIt last)
 
template<typename InputIt >
 StringRef (InputIt *first, InputIt *last)
 
constexpr const_iterator begin () const
 
constexpr const_iterator cbegin () const
 
constexpr const_iterator end () const
 
constexpr const_iterator cend () const
 
const_reverse_iterator rbegin () const
 
const_reverse_iterator crbegin () const
 
const_reverse_iterator rend () const
 
const_reverse_iterator crend () const
 
constexpr const char * c_str () const
 
constexpr size_type size () const
 
constexpr size_type length () const
 
constexpr bool empty () const
 
constexpr const_reference operator[] (size_type pos) const
 
std::string str () const
 
const uint8_t * byte () const
 
 operator std::string () const
 
size_type find (const char *s, size_type pos=0) const
 Find first occurrence of substring, returns std::string::npos if not found.
 
size_type find (char c, size_type pos=0) const
 
std::string substr (size_type pos=0, size_type count=std::string::npos) const
 Return substring as std::string.
 

Static Public Member Functions

template<typename CharT , size_t N>
static constexpr StringRef from_lit (const CharT(&s)[N])
 
static StringRef from_maybe_nullptr (const char *s)
 

Detailed Description

StringRef is a reference to a string owned by something else.

So it behaves like simple string, but it does not own pointer. When it is default constructed, it has empty string. You can freely copy or move around this struct, but never free its pointer. str() function can be used to export the content as std::string. StringRef is adopted from https://github.com/nghttp2/nghttp2/blob/29cbf8b83ff78faf405d1086b16adc09a8772eca/src/template.h#L376

Definition at line 26 of file string_ref.h.

Member Typedef Documentation

◆ allocator_type

using esphome::StringRef::allocator_type = std::allocator<char>

Definition at line 30 of file string_ref.h.

◆ const_iterator

Definition at line 35 of file string_ref.h.

◆ const_pointer

Definition at line 34 of file string_ref.h.

◆ const_reference

Definition at line 33 of file string_ref.h.

◆ const_reverse_iterator

Definition at line 36 of file string_ref.h.

◆ difference_type

using esphome::StringRef::difference_type = std::allocator_traits<allocator_type>::difference_type

Definition at line 32 of file string_ref.h.

◆ size_type

using esphome::StringRef::size_type = std::allocator_traits<allocator_type>::size_type

Definition at line 31 of file string_ref.h.

◆ traits_type

using esphome::StringRef::traits_type = std::char_traits<char>

Definition at line 28 of file string_ref.h.

◆ value_type

using esphome::StringRef::value_type = traits_type::char_type

Definition at line 29 of file string_ref.h.

Constructor & Destructor Documentation

◆ StringRef() [1/7]

esphome::StringRef::StringRef ( )
inlineconstexpr

Definition at line 38 of file string_ref.h.

◆ StringRef() [2/7]

esphome::StringRef::StringRef ( const std::string & s)
inlineexplicit

Definition at line 39 of file string_ref.h.

◆ StringRef() [3/7]

esphome::StringRef::StringRef ( const char * s)
inlineexplicit

Definition at line 40 of file string_ref.h.

◆ StringRef() [4/7]

esphome::StringRef::StringRef ( const char * s,
size_t n )
inlineconstexpr

Definition at line 41 of file string_ref.h.

◆ StringRef() [5/7]

template<typename CharT >
esphome::StringRef::StringRef ( const CharT * s,
size_t n )
inlineconstexpr

Definition at line 43 of file string_ref.h.

◆ StringRef() [6/7]

template<typename InputIt >
esphome::StringRef::StringRef ( InputIt first,
InputIt last )
inline

Definition at line 45 of file string_ref.h.

◆ StringRef() [7/7]

template<typename InputIt >
esphome::StringRef::StringRef ( InputIt * first,
InputIt * last )
inline

Definition at line 48 of file string_ref.h.

Member Function Documentation

◆ begin()

const_iterator esphome::StringRef::begin ( ) const
inlineconstexpr

Definition at line 61 of file string_ref.h.

◆ byte()

const uint8_t * esphome::StringRef::byte ( ) const
inline

Definition at line 80 of file string_ref.h.

◆ c_str()

const char * esphome::StringRef::c_str ( ) const
inlineconstexpr

Definition at line 73 of file string_ref.h.

◆ cbegin()

const_iterator esphome::StringRef::cbegin ( ) const
inlineconstexpr

Definition at line 62 of file string_ref.h.

◆ cend()

const_iterator esphome::StringRef::cend ( ) const
inlineconstexpr

Definition at line 65 of file string_ref.h.

◆ crbegin()

const_reverse_iterator esphome::StringRef::crbegin ( ) const
inline

Definition at line 68 of file string_ref.h.

◆ crend()

const_reverse_iterator esphome::StringRef::crend ( ) const
inline

Definition at line 71 of file string_ref.h.

◆ empty()

bool esphome::StringRef::empty ( ) const
inlineconstexpr

Definition at line 76 of file string_ref.h.

◆ end()

const_iterator esphome::StringRef::end ( ) const
inlineconstexpr

Definition at line 64 of file string_ref.h.

◆ find() [1/2]

size_type esphome::StringRef::find ( char c,
size_type pos = 0 ) const
inline

Definition at line 95 of file string_ref.h.

◆ find() [2/2]

size_type esphome::StringRef::find ( const char * s,
size_type pos = 0 ) const
inline

Find first occurrence of substring, returns std::string::npos if not found.

Note: Requires the underlying string to be null-terminated.

Definition at line 86 of file string_ref.h.

◆ from_lit()

template<typename CharT , size_t N>
static constexpr StringRef esphome::StringRef::from_lit ( const CharT(&) s[N])
inlinestaticconstexpr

Definition at line 50 of file string_ref.h.

◆ from_maybe_nullptr()

static StringRef esphome::StringRef::from_maybe_nullptr ( const char * s)
inlinestatic

Definition at line 53 of file string_ref.h.

◆ length()

size_type esphome::StringRef::length ( ) const
inlineconstexpr

Definition at line 75 of file string_ref.h.

◆ operator std::string()

esphome::StringRef::operator std::string ( ) const
inline

Definition at line 82 of file string_ref.h.

◆ operator[]()

const_reference esphome::StringRef::operator[] ( size_type pos) const
inlineconstexpr

Definition at line 77 of file string_ref.h.

◆ rbegin()

const_reverse_iterator esphome::StringRef::rbegin ( ) const
inline

Definition at line 67 of file string_ref.h.

◆ rend()

const_reverse_iterator esphome::StringRef::rend ( ) const
inline

Definition at line 70 of file string_ref.h.

◆ size()

size_type esphome::StringRef::size ( ) const
inlineconstexpr

Definition at line 74 of file string_ref.h.

◆ str()

std::string esphome::StringRef::str ( ) const
inline

Definition at line 79 of file string_ref.h.

◆ substr()

std::string esphome::StringRef::substr ( size_type pos = 0,
size_type count = std::string::npos ) const
inline

Return substring as std::string.

Definition at line 103 of file string_ref.h.


The documentation for this class was generated from the following file: