HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
string_view.h File Reference
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <limits>
#include <ostream>
#include <stdexcept>
#include <string>
#include <OpenImageIO/export.h>
#include <OpenImageIO/oiioversion.h>
#include <OpenImageIO/platform.h>
#include <OpenImageIO/detail/fmt.h>
+ Include dependency graph for string_view.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  basic_string_view< Char >
 

Namespaces

 std
 std::hash specialization for UT_IntrusivePtr<T>
 

Macros

#define OIIO_STRING_VIEW_HAS_STD_SIZE   1
 

Typedefs

using string_view = basic_string_view< char >
 
using wstring_view = basic_string_view< wchar_t >
 
typedef string_view string_ref
 

Functions

OIIO_UTIL_API const char * c_str (string_view str)
 
template<class CharT , class Traits = std::char_traits<CharT>>
constexpr size_t std::size (const OIIO::basic_string_view< CharT, Traits > &c)
 
template<class CharT , class Traits = std::char_traits<CharT>>
constexpr ptrdiff_t std::ssize (const OIIO::basic_string_view< CharT, Traits > &c)
 

Macro Definition Documentation

#define OIIO_STRING_VIEW_HAS_STD_SIZE   1

Definition at line 534 of file string_view.h.

Typedef Documentation

Definition at line 466 of file string_view.h.

string_view is an alias for basic_string_view<char>. This is the common use case.

Definition at line 459 of file string_view.h.

using wstring_view = basic_string_view<wchar_t>

Definition at line 460 of file string_view.h.

Function Documentation

OIIO_UTIL_API const char* c_str ( string_view  str)

Return a safe pointer to a null-terminated C string with the contents of the string_view.

ENORMOUS CAVEAT: This nonstandard functionality is only safe if the string_view is a true "subset of a C/C++ string". It will fail in the unfortunate case where the last character of str is the last byte of a readable page of memory and the next character is on a page that's not readable. This can never happen to a string_view that was constructed from a C string, a C++ std::string, an OIIO ustring, or any subset of characters from any of those. But still, watch out if you're constructing a string_view a pointer to some other memory region that's not at all part of a string-like object.

How do we get a safe c_str from a string_view? It's a neat trick! First, we check if str[str.size()] is a null character. If it is – and this is the very common case of the string_view being a way to pass a reference to an entire std::string, a C string (char*), or a ustring – then it just returns str.data(), since that is a totally valid null-terminated C string! On the other hand, if str[str.size()] != 0, then we construct a ustring and return its ustring::c_str(), which is safe because ustring memory is never freed.