HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
string_view Class Reference

#include <string_view.h>

Public Types

typedef char charT
 
typedef charT value_type
 
typedef const charTpointer
 
typedef const charTconst_pointer
 
typedef const charTreference
 
typedef const charTconst_reference
 
typedef const_pointer const_iterator
 
typedef const_iterator iterator
 
typedef std::reverse_iterator
< const_iterator
const_reverse_iterator
 
typedef const_reverse_iterator reverse_iterator
 
typedef size_t size_type
 
typedef ptrdiff_t difference_type
 
typedef std::char_traits< charTtraits
 

Public Member Functions

constexpr string_view () noexcept
 Default ctr. More...
 
 string_view (const string_view &copy) noexcept
 Copy ctr. More...
 
 string_view (const charT *chars, size_t len)
 Construct from char* and length. More...
 
 string_view (const charT *chars)
 Construct from char*, use strlen to determine length. More...
 
 string_view (const std::string &str) noexcept
 
std::string str () const
 Convert a string_view to a std::string. More...
 
const char * c_str () const
 
string_viewoperator= (const string_view &copy) noexcept
 
 operator std::string () const
 Convert a string_view to a std::string. More...
 
const_iterator begin () const noexcept
 
const_iterator end () const noexcept
 
const_iterator cbegin () const noexcept
 
const_iterator cend () const noexcept
 
const_reverse_iterator rbegin () const noexcept
 
const_reverse_iterator rend () const noexcept
 
const_reverse_iterator crbegin () const noexcept
 
const_reverse_iterator crend () const noexcept
 
constexpr size_type size () const noexcept
 
constexpr size_type length () const noexcept
 
constexpr size_type max_size () const noexcept
 
constexpr bool empty () const noexcept
 Is the string_view empty, containing no characters? More...
 
const charToperator[] (size_type pos) const
 
const charTat (size_t pos) const
 Element access with bounds checking and exception if out of bounds. More...
 
const charTfront () const
 
const charTback () const
 
const charTdata () const noexcept
 
void clear () noexcept
 
void remove_prefix (size_type n) noexcept
 
void remove_suffix (size_type n) noexcept
 
string_view substr (size_type pos, size_type n=npos) const noexcept
 
int compare (string_view x) const noexcept
 
size_type find (string_view s, size_t pos=0) const noexcept
 
size_type find (charT c, size_t pos=0) const noexcept
 
size_type rfind (string_view s, size_t pos=npos) const noexcept
 
size_type rfind (charT c, size_t pos=npos) const noexcept
 
size_type find_first_of (charT c, size_t pos=0) const noexcept
 
size_type find_last_of (charT c, size_t pos=npos) const noexcept
 
size_type find_first_of (string_view s, size_t pos=0) const noexcept
 
size_type find_last_of (string_view s, size_t pos=npos) const noexcept
 
size_type find_first_not_of (string_view s, size_t pos=0) const noexcept
 
size_type find_first_not_of (charT c, size_t pos=0) const noexcept
 
size_type find_last_not_of (string_view s, size_t pos=npos) const noexcept
 
size_type find_last_not_of (charT c, size_t pos=npos) const noexcept
 

Static Public Attributes

static const size_type npos = ~size_type(0)
 

Detailed Description

A string_view is a non-owning, non-copying, non-allocating reference to a sequence of characters. It encapsulates both a character pointer and a length.

A function that takes a string input (but does not need to alter the string in place) may use a string_view parameter and accept input that is any of char* (C string), string literal (constant char array), a std::string (C++ string), or OIIO ustring. For all of these cases, no extra allocations are performed, and no extra copies of the string contents are performed (as they would be, for example, if the function took a const std::string& argument but was passed a char* or string literal).

Furthermore, a function that returns a copy or a substring of one of its inputs (for example, a substr()-like function) may return a string_view rather than a std::string, and thus generate its return value without any allocation or copying. Upon assignment to a std::string or ustring, it will properly auto-convert.

There are two important caveats to using this class:

  1. The string_view merely refers to characters owned by another string, so the string_view may not be used outside the lifetime of the string it refers to. Thus, string_view is great for parameter passing, but it's not a good idea to use a string_view to store strings in a data structure (unless you are really sure you know what you're doing).
  2. Because the run of characters that the string_view refers to may not be 0-terminated, it is important to distinguish between the data() method, which returns the pointer to the characters, and the c_str() method, which is guaranteed to return a valid C string that is 0-terminated. Thus, if you want to pass the contents of a string_view to a function that expects a 0-terminated string (say, fopen), you must call fopen(my_string_view.c_str()). Note that the usual case is that the string_view does refer to a 0-terminated string, and in that case c_str() returns the same thing as data() without any extra expense; but in the rare case that it is not 0-terminated, c_str() will incur extra expense to internally allocate a valid C string.

Definition at line 63 of file string_view.h.

Member Typedef Documentation

typedef char string_view::charT

Definition at line 65 of file string_view.h.

typedef const_pointer string_view::const_iterator

Definition at line 71 of file string_view.h.

typedef const charT* string_view::const_pointer

Definition at line 68 of file string_view.h.

typedef const charT& string_view::const_reference

Definition at line 70 of file string_view.h.

typedef std::reverse_iterator<const_iterator> string_view::const_reverse_iterator

Definition at line 73 of file string_view.h.

typedef ptrdiff_t string_view::difference_type

Definition at line 76 of file string_view.h.

Definition at line 72 of file string_view.h.

typedef const charT* string_view::pointer

Definition at line 67 of file string_view.h.

typedef const charT& string_view::reference

Definition at line 69 of file string_view.h.

typedef const_reverse_iterator string_view::reverse_iterator

Definition at line 74 of file string_view.h.

typedef size_t string_view::size_type

Definition at line 75 of file string_view.h.

typedef std::char_traits<charT> string_view::traits

Definition at line 77 of file string_view.h.

Definition at line 66 of file string_view.h.

Constructor & Destructor Documentation

constexpr string_view::string_view ( )
inlinenoexcept

Default ctr.

Definition at line 81 of file string_view.h.

string_view::string_view ( const string_view copy)
inlinenoexcept

Copy ctr.

Definition at line 83 of file string_view.h.

string_view::string_view ( const charT chars,
size_t  len 
)
inline

Construct from char* and length.

Definition at line 85 of file string_view.h.

string_view::string_view ( const charT chars)
inline

Construct from char*, use strlen to determine length.

Definition at line 87 of file string_view.h.

string_view::string_view ( const std::string str)
inlinenoexcept

Construct from std::string. Remember that a string_view doesn't have its own copy of the characters, so don't use the string_view after the original string has been destroyed or altered.

Definition at line 91 of file string_view.h.

Member Function Documentation

const charT& string_view::at ( size_t  pos) const
inline

Element access with bounds checking and exception if out of bounds.

Definition at line 152 of file string_view.h.

const charT& string_view::back ( void  ) const
inline

Definition at line 159 of file string_view.h.

const_iterator string_view::begin ( void  ) const
inlinenoexcept

Definition at line 130 of file string_view.h.

const char* string_view::c_str ( ) const

Explicitly request a 0-terminated string. USUALLY, this turns out to be just data(), with no significant added expense (because most uses of string_view are simple wrappers of C strings, C++ std::string, or ustring – all of which are 0-terminated). But in the more rare case that the string_view represents a non-0-terminated substring, it will force an allocation and copy underneath.

Caveats:

  1. This is NOT going to be part of the C++17 std::string_view, so it's probably best to avoid this method if you want to have 100% drop-in compatibility with std::string_view.
  2. It is NOT SAFE to use c_str() on a string_view whose last char is the end of an allocation – because that next char may only coincidentally be a '\0', which will cause c_str() to return the string start (thinking it's a valid C string, so why not just return its address?), if there's any chance that the subsequent char could change from 0 to non-zero during the use of the result of c_str(), and thus break the assumption that it's a valid C str.
const_iterator string_view::cbegin ( ) const
inlinenoexcept

Definition at line 132 of file string_view.h.

const_iterator string_view::cend ( ) const
inlinenoexcept

Definition at line 133 of file string_view.h.

void string_view::clear ( )
inlinenoexcept

Definition at line 163 of file string_view.h.

int string_view::compare ( string_view  x) const
inlinenoexcept

Definition at line 187 of file string_view.h.

const_reverse_iterator string_view::crbegin ( ) const
inlinenoexcept

Definition at line 136 of file string_view.h.

const_reverse_iterator string_view::crend ( ) const
inlinenoexcept

Definition at line 137 of file string_view.h.

const charT* string_view::data ( ) const
inlinenoexcept

Definition at line 160 of file string_view.h.

constexpr bool string_view::empty ( void  ) const
inlinenoexcept

Is the string_view empty, containing no characters?

Definition at line 146 of file string_view.h.

const_iterator string_view::end ( void  ) const
inlinenoexcept

Definition at line 131 of file string_view.h.

size_type string_view::find ( string_view  s,
size_t  pos = 0 
) const
inlinenoexcept

Find the first occurrence of substring s in *this, starting at position pos.

Definition at line 203 of file string_view.h.

size_type string_view::find ( charT  c,
size_t  pos = 0 
) const
inlinenoexcept

Find the first occurrence of character c in *this, starting at position pos.

Definition at line 214 of file string_view.h.

size_type string_view::find_first_not_of ( string_view  s,
size_t  pos = 0 
) const
inlinenoexcept

Definition at line 265 of file string_view.h.

size_type string_view::find_first_not_of ( charT  c,
size_t  pos = 0 
) const
inlinenoexcept

Definition at line 272 of file string_view.h.

size_type string_view::find_first_of ( charT  c,
size_t  pos = 0 
) const
inlinenoexcept

Definition at line 244 of file string_view.h.

size_type string_view::find_first_of ( string_view  s,
size_t  pos = 0 
) const
inlinenoexcept

Definition at line 248 of file string_view.h.

size_type string_view::find_last_not_of ( string_view  s,
size_t  pos = npos 
) const
inlinenoexcept

Definition at line 281 of file string_view.h.

size_type string_view::find_last_not_of ( charT  c,
size_t  pos = npos 
) const
inlinenoexcept

Definition at line 289 of file string_view.h.

size_type string_view::find_last_of ( charT  c,
size_t  pos = npos 
) const
inlinenoexcept

Definition at line 246 of file string_view.h.

size_type string_view::find_last_of ( string_view  s,
size_t  pos = npos 
) const
inlinenoexcept

Definition at line 256 of file string_view.h.

const charT& string_view::front ( void  ) const
inline

Definition at line 158 of file string_view.h.

constexpr size_type string_view::length ( void  ) const
inlinenoexcept

Definition at line 141 of file string_view.h.

constexpr size_type string_view::max_size ( void  ) const
inlinenoexcept

Definition at line 142 of file string_view.h.

string_view::operator std::string ( ) const
inline

Convert a string_view to a std::string.

Definition at line 127 of file string_view.h.

string_view& string_view::operator= ( const string_view copy)
inlinenoexcept

Definition at line 120 of file string_view.h.

const charT& string_view::operator[] ( size_type  pos) const
inline

Element access of an individual character (beware: no bounds checking!).

Definition at line 150 of file string_view.h.

const_reverse_iterator string_view::rbegin ( ) const
inlinenoexcept

Definition at line 134 of file string_view.h.

void string_view::remove_prefix ( size_type  n)
inlinenoexcept

Definition at line 164 of file string_view.h.

void string_view::remove_suffix ( size_type  n)
inlinenoexcept

Definition at line 171 of file string_view.h.

const_reverse_iterator string_view::rend ( ) const
inlinenoexcept

Definition at line 135 of file string_view.h.

size_type string_view::rfind ( string_view  s,
size_t  pos = npos 
) const
inlinenoexcept

Find the last occurrence of substring s *this, but only those occurrences earlier than position pos.

Definition at line 224 of file string_view.h.

size_type string_view::rfind ( charT  c,
size_t  pos = npos 
) const
inlinenoexcept

Find the last occurrence of character c in *this, but only those occurrences earlier than position pos.

Definition at line 235 of file string_view.h.

constexpr size_type string_view::size ( void  ) const
inlinenoexcept

Definition at line 140 of file string_view.h.

std::string string_view::str ( ) const
inline

Convert a string_view to a std::string.

Definition at line 94 of file string_view.h.

string_view string_view::substr ( size_type  pos,
size_type  n = npos 
) const
inlinenoexcept

Definition at line 178 of file string_view.h.

Member Data Documentation

const size_type string_view::npos = ~size_type(0)
static

Definition at line 78 of file string_view.h.


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