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

 string_view ()
 Default ctr. More...
 
 string_view (const string_view &copy)
 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)
 Construct from std::string. More...
 
std::string str () const
 
const char * c_str () const
 
string_viewoperator= (const string_view &copy)
 
 operator std::string () const
 
const_iterator begin () const
 
const_iterator end () const
 
const_iterator cbegin () const
 
const_iterator cend () const
 
const_reverse_iterator rbegin () const
 
const_reverse_iterator rend () const
 
const_reverse_iterator crbegin () const
 
const_reverse_iterator crend () const
 
size_type size () const
 
size_type length () const
 
size_type max_size () const
 
bool empty () const
 
const charToperator[] (size_type pos) const
 
const charTat (size_t pos) const
 
const charTfront () const
 
const charTback () const
 
const charTdata () const
 
void clear ()
 
void remove_prefix (size_type n)
 
void remove_suffix (size_type n)
 
string_view substr (size_type pos, size_type n=npos) const
 
int compare (string_view x) const
 
size_type find (string_view s, size_t pos=0) const
 
size_type find (charT c, size_t pos=0) const
 
size_type rfind (string_view s, size_t pos=npos) const
 
size_type rfind (charT c, size_t pos=npos) const
 
size_type find_first_of (charT c, size_t pos=0) const
 
size_type find_last_of (charT c, size_t pos=npos) const
 
size_type find_first_of (string_view s, size_t pos=0) const
 
size_type find_last_of (string_view s, size_t pos=npos) const
 
size_type find_first_not_of (string_view s, size_t pos=0) const
 
size_type find_first_not_of (charT c, size_t pos=0) const
 
size_type find_last_not_of (string_view s, size_t pos=npos) const
 
size_type find_last_not_of (charT c, size_t pos=npos) const
 

Static Public Attributes

static const size_type npos = ~size_type(0)
 

Detailed Description

string_view : 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 88 of file string_view.h.

Member Typedef Documentation

typedef char string_view::charT

Definition at line 90 of file string_view.h.

typedef const_pointer string_view::const_iterator

Definition at line 96 of file string_view.h.

typedef const charT* string_view::const_pointer

Definition at line 93 of file string_view.h.

typedef const charT& string_view::const_reference

Definition at line 95 of file string_view.h.

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

Definition at line 98 of file string_view.h.

typedef ptrdiff_t string_view::difference_type

Definition at line 101 of file string_view.h.

Definition at line 97 of file string_view.h.

typedef const charT* string_view::pointer

Definition at line 92 of file string_view.h.

typedef const charT& string_view::reference

Definition at line 94 of file string_view.h.

typedef const_reverse_iterator string_view::reverse_iterator

Definition at line 99 of file string_view.h.

typedef size_t string_view::size_type

Definition at line 100 of file string_view.h.

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

Definition at line 102 of file string_view.h.

typedef charT string_view::value_type

Definition at line 91 of file string_view.h.

Constructor & Destructor Documentation

string_view::string_view ( )
inline

Default ctr.

Definition at line 106 of file string_view.h.

string_view::string_view ( const string_view copy)
inline

Copy ctr.

Definition at line 108 of file string_view.h.

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

Construct from char* and length.

Definition at line 110 of file string_view.h.

string_view::string_view ( const charT chars)
inline

Construct from char*, use strlen to determine length.

Definition at line 112 of file string_view.h.

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

Construct from std::string.

Definition at line 114 of file string_view.h.

Member Function Documentation

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

Definition at line 168 of file string_view.h.

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

Definition at line 175 of file string_view.h.

const_iterator string_view::begin ( void  ) const
inline

Definition at line 151 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 represetns 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
inline

Definition at line 153 of file string_view.h.

const_iterator string_view::cend ( ) const
inline

Definition at line 154 of file string_view.h.

void string_view::clear ( )
inline

Definition at line 179 of file string_view.h.

int string_view::compare ( string_view  x) const
inline

Definition at line 203 of file string_view.h.

const_reverse_iterator string_view::crbegin ( ) const
inline

Definition at line 157 of file string_view.h.

const_reverse_iterator string_view::crend ( ) const
inline

Definition at line 158 of file string_view.h.

const charT* string_view::data ( ) const
inline

Definition at line 176 of file string_view.h.

bool string_view::empty ( void  ) const
inline

Definition at line 164 of file string_view.h.

const_iterator string_view::end ( void  ) const
inline

Definition at line 152 of file string_view.h.

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

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

Definition at line 218 of file string_view.h.

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

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

Definition at line 229 of file string_view.h.

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

Definition at line 280 of file string_view.h.

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

Definition at line 287 of file string_view.h.

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

Definition at line 259 of file string_view.h.

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

Definition at line 263 of file string_view.h.

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

Definition at line 296 of file string_view.h.

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

Definition at line 304 of file string_view.h.

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

Definition at line 261 of file string_view.h.

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

Definition at line 271 of file string_view.h.

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

Definition at line 174 of file string_view.h.

size_type string_view::length ( void  ) const
inline

Definition at line 162 of file string_view.h.

size_type string_view::max_size ( void  ) const
inline

Definition at line 163 of file string_view.h.

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

Definition at line 148 of file string_view.h.

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

Definition at line 142 of file string_view.h.

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

Definition at line 167 of file string_view.h.

const_reverse_iterator string_view::rbegin ( ) const
inline

Definition at line 155 of file string_view.h.

void string_view::remove_prefix ( size_type  n)
inline

Definition at line 180 of file string_view.h.

void string_view::remove_suffix ( size_type  n)
inline

Definition at line 187 of file string_view.h.

const_reverse_iterator string_view::rend ( ) const
inline

Definition at line 156 of file string_view.h.

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

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

Definition at line 239 of file string_view.h.

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

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

Definition at line 250 of file string_view.h.

size_type string_view::size ( void  ) const
inline

Definition at line 161 of file string_view.h.

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

Definition at line 116 of file string_view.h.

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

Definition at line 194 of file string_view.h.

Member Data Documentation

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

Definition at line 103 of file string_view.h.


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