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

#include <string_view.h>

Public Types

using charT = char
 
using Traits = std::char_traits< charT >
 
using traits_type = Traits
 
using value_type = charT
 
using pointer = const charT *
 
using const_pointer = const charT *
 
using reference = const charT &
 
using const_reference = const charT &
 
using const_iterator = const_pointer
 
using iterator = const_iterator
 
using const_reverse_iterator = std::reverse_iterator< const_iterator >
 
using reverse_iterator = const_reverse_iterator
 
using size_type = size_t
 
using difference_type = ptrdiff_t
 
using traits = std::char_traits< charT >
 

Public Member Functions

constexpr string_view () noexcept
 Default ctr. More...
 
constexpr string_view (const string_view &copy)
 Copy ctr. More...
 
constexpr string_view (const charT *chars, size_t len)
 Construct from char* and length. More...
 
OIIO_CONSTEXPR17 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
 
OIIO_CONSTEXPR14 string_viewoperator= (const string_view &copy) noexcept=default
 
 operator std::string () const
 Convert a string_view to a std::string. More...
 
constexpr iterator begin () const noexcept
 
constexpr iterator end () const noexcept
 
constexpr const_iterator cbegin () const noexcept
 
constexpr const_iterator cend () const noexcept
 
OIIO_CONSTEXPR17 reverse_iterator rbegin () const noexcept
 
OIIO_CONSTEXPR17 reverse_iterator rend () const noexcept
 
OIIO_CONSTEXPR17
const_reverse_iterator 
crbegin () const noexcept
 
OIIO_CONSTEXPR17
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...
 
constexpr const_reference operator[] (size_type pos) const
 
OIIO_CONSTEXPR17 const_reference at (size_t pos) const
 Element access with bounds checking and exception if out of bounds. More...
 
constexpr const_reference front () const
 
constexpr const_reference back () const
 
constexpr const_pointer data () const noexcept
 
OIIO_CONSTEXPR14 void clear () noexcept
 
OIIO_CONSTEXPR14 void remove_prefix (size_type n) noexcept
 
OIIO_CONSTEXPR14 void remove_suffix (size_type n) noexcept
 
OIIO_CONSTEXPR14 string_view substr (size_type pos, size_type n=npos) const noexcept
 
OIIO_CONSTEXPR17 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 64 of file string_view.h.

Member Typedef Documentation

using string_view::charT = char

Definition at line 66 of file string_view.h.

using string_view::const_iterator = const_pointer

Definition at line 74 of file string_view.h.

using string_view::const_pointer = const charT*

Definition at line 71 of file string_view.h.

using string_view::const_reference = const charT&

Definition at line 73 of file string_view.h.

using string_view::const_reverse_iterator = std::reverse_iterator<const_iterator>

Definition at line 76 of file string_view.h.

using string_view::difference_type = ptrdiff_t

Definition at line 79 of file string_view.h.

Definition at line 75 of file string_view.h.

using string_view::pointer = const charT*

Definition at line 70 of file string_view.h.

using string_view::reference = const charT&

Definition at line 72 of file string_view.h.

using string_view::reverse_iterator = const_reverse_iterator

Definition at line 77 of file string_view.h.

using string_view::size_type = size_t

Definition at line 78 of file string_view.h.

using string_view::Traits = std::char_traits<charT>

Definition at line 67 of file string_view.h.

using string_view::traits = std::char_traits<charT>

Definition at line 80 of file string_view.h.

using string_view::traits_type = Traits

Definition at line 68 of file string_view.h.

Definition at line 69 of file string_view.h.

Constructor & Destructor Documentation

constexpr string_view::string_view ( )
inlinenoexcept

Default ctr.

Definition at line 84 of file string_view.h.

constexpr string_view::string_view ( const string_view copy)
inline

Copy ctr.

Definition at line 87 of file string_view.h.

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

Construct from char* and length.

Definition at line 91 of file string_view.h.

OIIO_CONSTEXPR17 string_view::string_view ( const charT chars)
inline

Construct from char*, use strlen to determine length.

Definition at line 95 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 102 of file string_view.h.

Member Function Documentation

OIIO_CONSTEXPR17 const_reference string_view::at ( size_t  pos) const
inline

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

Definition at line 162 of file string_view.h.

constexpr const_reference string_view::back ( void  ) const
inline

Definition at line 169 of file string_view.h.

constexpr iterator string_view::begin ( void  ) const
inlinenoexcept

Definition at line 140 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.
constexpr const_iterator string_view::cbegin ( ) const
inlinenoexcept

Definition at line 142 of file string_view.h.

constexpr const_iterator string_view::cend ( ) const
inlinenoexcept

Definition at line 143 of file string_view.h.

OIIO_CONSTEXPR14 void string_view::clear ( )
inlinenoexcept

Definition at line 173 of file string_view.h.

OIIO_CONSTEXPR17 int string_view::compare ( string_view  x) const
inlinenoexcept

Definition at line 197 of file string_view.h.

OIIO_CONSTEXPR17 const_reverse_iterator string_view::crbegin ( ) const
inlinenoexcept

Definition at line 146 of file string_view.h.

OIIO_CONSTEXPR17 const_reverse_iterator string_view::crend ( ) const
inlinenoexcept

Definition at line 147 of file string_view.h.

constexpr const_pointer string_view::data ( ) const
inlinenoexcept

Definition at line 170 of file string_view.h.

constexpr bool string_view::empty ( void  ) const
inlinenoexcept

Is the string_view empty, containing no characters?

Definition at line 156 of file string_view.h.

constexpr iterator string_view::end ( void  ) const
inlinenoexcept

Definition at line 141 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 214 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 225 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 276 of file string_view.h.

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

Definition at line 283 of file string_view.h.

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

Definition at line 255 of file string_view.h.

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

Definition at line 259 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 292 of file string_view.h.

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

Definition at line 300 of file string_view.h.

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

Definition at line 257 of file string_view.h.

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

Definition at line 267 of file string_view.h.

constexpr const_reference string_view::front ( void  ) const
inline

Definition at line 168 of file string_view.h.

constexpr size_type string_view::length ( void  ) const
inlinenoexcept

Definition at line 151 of file string_view.h.

constexpr size_type string_view::max_size ( void  ) const
inlinenoexcept

Definition at line 152 of file string_view.h.

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

Convert a string_view to a std::string.

Definition at line 137 of file string_view.h.

OIIO_CONSTEXPR14 string_view& string_view::operator= ( const string_view copy)
defaultnoexcept
constexpr const_reference string_view::operator[] ( size_type  pos) const
inline

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

Definition at line 160 of file string_view.h.

OIIO_CONSTEXPR17 reverse_iterator string_view::rbegin ( ) const
inlinenoexcept

Definition at line 144 of file string_view.h.

OIIO_CONSTEXPR14 void string_view::remove_prefix ( size_type  n)
inlinenoexcept

Definition at line 174 of file string_view.h.

OIIO_CONSTEXPR14 void string_view::remove_suffix ( size_type  n)
inlinenoexcept

Definition at line 181 of file string_view.h.

OIIO_CONSTEXPR17 reverse_iterator string_view::rend ( ) const
inlinenoexcept

Definition at line 145 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 235 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 246 of file string_view.h.

constexpr size_type string_view::size ( void  ) const
inlinenoexcept

Definition at line 150 of file string_view.h.

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

Convert a string_view to a std::string.

Definition at line 107 of file string_view.h.

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

Definition at line 188 of file string_view.h.

Member Data Documentation

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

Definition at line 81 of file string_view.h.


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