HDK
|
#include <ustring.h>
Classes | |
struct | TableRep |
Public Types | |
typedef char | value_type |
typedef value_type * | pointer |
typedef value_type & | reference |
typedef const value_type & | const_reference |
typedef size_t | size_type |
typedef std::string::const_iterator | const_iterator |
typedef std::string::const_reverse_iterator | const_reverse_iterator |
Public Member Functions | |
ustring (void) noexcept | |
Default ctr for ustring – make an empty string. More... | |
ustring (const char *str) | |
Construct a ustring from a null-terminated C string (char *). More... | |
ustring (string_view str) | |
ustring (const char *str, size_type pos, size_type n) | |
ustring (const char *str, size_type n) | |
Construct a ustring from the first n characters of str. More... | |
ustring (size_type n, char c) | |
Construct a ustring from n copies of character c. More... | |
ustring (const std::string &str, size_type pos, size_type n=npos) | |
Construct a ustring from an indexed substring of a std::string. More... | |
ustring (const ustring &str) noexcept | |
Copy construct a ustring from another ustring. More... | |
ustring (const ustring &str, size_type pos, size_type n=npos) | |
Construct a ustring from an indexed substring of a ustring. More... | |
~ustring () noexcept | |
ustring destructor. More... | |
operator string_view () const noexcept | |
Conversion to string_view. More... | |
operator std::string () const noexcept | |
Conversion to std::string (explicit only!). More... | |
const ustring & | assign (const ustring &str) |
Assign a ustring to *this. More... | |
const ustring & | assign (const ustring &str, size_type pos, size_type n=npos) |
Assign a substring of a ustring to *this. More... | |
const ustring & | assign (const std::string &str) |
Assign a std::string to *this. More... | |
const ustring & | assign (const std::string &str, size_type pos, size_type n=npos) |
Assign a substring of a std::string to *this. More... | |
const ustring & | assign (const char *str) |
Assign a null-terminated C string (char*) to *this. More... | |
const ustring & | assign (const char *str, size_type n) |
Assign the first n characters of str to *this. More... | |
const ustring & | assign (size_type n, char c) |
Assign n copies of c to *this. More... | |
const ustring & | assign (string_view str) |
Assign a string_view to *this. More... | |
const ustring & | operator= (const ustring &str) |
Assign a ustring to another ustring. More... | |
const ustring & | operator= (const char *str) |
Assign a null-terminated C string (char *) to a ustring. More... | |
const ustring & | operator= (const std::string &str) |
Assign a C++ std::string to a ustring. More... | |
const ustring & | operator= (string_view str) |
Assign a string_view to a ustring. More... | |
const ustring & | operator= (char c) |
Assign a single char to a ustring. More... | |
const char * | c_str () const noexcept |
Return a C string representation of a ustring. More... | |
const char * | data () const noexcept |
Return a C string representation of a ustring. More... | |
const std::string & | string () const noexcept |
Return a C++ std::string representation of a ustring. More... | |
void | clear (void) noexcept |
Reset to an empty string. More... | |
size_t | length (void) const noexcept |
Return the number of characters in the string. More... | |
size_t | hash (void) const noexcept |
Return a hashed version of the string. More... | |
size_t | size (void) const noexcept |
Return the number of characters in the string. More... | |
bool | empty (void) const noexcept |
const_iterator | begin () const noexcept |
const_iterator | end () const noexcept |
const_reverse_iterator | rbegin () const noexcept |
const_reverse_iterator | rend () const noexcept |
const_reference | operator[] (size_type pos) const noexcept |
size_type | copy (char *s, size_type n, size_type pos=0) const |
ustring | substr (size_type pos=0, size_type n=npos) const |
size_type | find (const ustring &str, size_type pos=0) const noexcept |
size_type | find (const std::string &str, size_type pos=0) const noexcept |
size_type | find (const char *s, size_type pos, size_type n) const |
size_type | find (const char *s, size_type pos=0) const |
size_type | find (char c, size_type pos=0) const noexcept |
size_type | rfind (const ustring &str, size_type pos=npos) const noexcept |
size_type | rfind (const std::string &str, size_type pos=npos) const noexcept |
size_type | rfind (const char *s, size_type pos, size_type n) const |
size_type | rfind (const char *s, size_type pos=npos) const |
size_type | rfind (char c, size_type pos=npos) const noexcept |
size_type | find_first_of (const ustring &str, size_type pos=0) const noexcept |
size_type | find_first_of (const std::string &str, size_type pos=0) const noexcept |
size_type | find_first_of (const char *s, size_type pos, size_type n) const |
size_type | find_first_of (const char *s, size_type pos=0) const |
size_type | find_first_of (char c, size_type pos=0) const noexcept |
size_type | find_last_of (const ustring &str, size_type pos=npos) const noexcept |
size_type | find_last_of (const std::string &str, size_type pos=npos) const noexcept |
size_type | find_last_of (const char *s, size_type pos, size_type n) const |
size_type | find_last_of (const char *s, size_type pos=npos) const |
size_type | find_last_of (char c, size_type pos=npos) const noexcept |
size_type | find_first_not_of (const ustring &str, size_type pos=0) const noexcept |
size_type | find_first_not_of (const std::string &str, size_type pos=0) const noexcept |
size_type | find_first_not_of (const char *s, size_type pos, size_type n) const |
size_type | find_first_not_of (const char *s, size_type pos=0) const |
size_type | find_first_not_of (char c, size_type pos=0) const noexcept |
size_type | find_last_not_of (const ustring &str, size_type pos=npos) const noexcept |
size_type | find_last_not_of (const std::string &str, size_type pos=npos) const noexcept |
size_type | find_last_not_of (const char *s, size_type pos, size_type n) const |
size_type | find_last_not_of (const char *s, size_type pos=npos) const |
size_type | find_last_not_of (char c, size_type pos=npos) const noexcept |
int | compare (string_view str) const noexcept |
int | compare (const char *str) const noexcept |
bool | operator== (const ustring &str) const noexcept |
bool | operator!= (const ustring &str) const noexcept |
bool | operator== (const std::string &x) const noexcept |
bool | operator== (string_view x) const noexcept |
bool | operator== (const char *x) const noexcept |
Test a ustring (this) for lexicographic equality with char x. More... | |
bool | operator!= (const std::string &x) const noexcept |
bool | operator!= (string_view x) const noexcept |
bool | operator!= (const char *x) const noexcept |
bool | operator< (const ustring &x) const noexcept |
Static Public Member Functions | |
template<typename... Args> | |
static ustring | sprintf (const char *fmt, const Args &...args) |
template<typename... Args> | |
static ustring | fmtformat (const char *fmt, const Args &...args) |
template<typename... Args> | |
static OIIO_FORMAT_DEPRECATED ustring | format (const char *fmt, const Args &...args) |
static ustring | concat (string_view s, string_view t) |
static std::string | getstats (bool verbose=true) |
Return the statistics output as a string. More... | |
static size_t | memory () |
Return the amount of memory consumed by the ustring table. More... | |
static size_t | total_ustrings () |
Return the total number of ustrings in the internal table. More... | |
static size_t | hash_collisions (std::vector< ustring > *collisions=nullptr) |
static const char * | make_unique (string_view str) |
static bool | is_unique (const char *str) |
static ustring | from_unique (const char *unique) |
Static Public Attributes | |
static const size_type | npos = static_cast<size_type>(-1) |
Friends | |
int | compare (const std::string &a, const ustring &b) noexcept |
bool | operator== (const std::string &a, const ustring &b) noexcept |
bool | operator== (string_view a, const ustring &b) noexcept |
bool | operator== (const char *a, const ustring &b) noexcept |
bool | operator!= (const std::string &a, const ustring &b) noexcept |
bool | operator!= (string_view a, const ustring &b) noexcept |
bool | operator!= (const char *a, const ustring &b) noexcept |
std::ostream & | operator<< (std::ostream &out, const ustring &str) |
Generic stream output of a ustring. More... | |
A ustring is an alternative to char* or std::string for storing strings, in which the character sequence is unique (allowing many speed advantages for assignment, equality testing, and inequality testing).
The implementation is that behind the scenes there is a hash set of allocated strings, so the characters of each string are unique. A ustring itself is a pointer to the characters of one of these canonical strings. Therefore, assignment and equality testing is just a single 32- or 64-bit int operation, the only mutex is when a ustring is created from raw characters, and the only malloc is the first time each canonical ustring is created.
The internal table also contains a std::string version and the length of the string, so converting a ustring to a std::string (via ustring::string()) or querying the number of characters (via ustring::size() or ustring::length()) is extremely inexpensive, and does not involve creation/allocation of a new std::string or a call to strlen.
We try very hard to completely mimic the API of std::string, including all the constructors, comparisons, iterations, etc. Of course, the charaters of a ustring are non-modifiable, so we do not replicate any of the non-const methods of std::string. But in most other ways it looks and acts like a std::string and so most templated algorthms that would work on a "const std::string &" will also work on a ustring.
Usage guidelines:
Compared to standard strings, ustrings have several advantages:
But there are some problems, too. Canonical strings are never freed from the table. So in some sense all the strings "leak", but they only leak one copy for each unique string that the program ever comes across. Also, creation of unique strings from raw characters is more expensive than for standard strings, due to hashing, table queries, and other overhead.
On the whole, ustrings are a really great string representation
ustrings are not so hot
typedef std::string::const_iterator ustring::const_iterator |
typedef const value_type& ustring::const_reference |
typedef std::string::const_reverse_iterator ustring::const_reverse_iterator |
typedef value_type* ustring::pointer |
typedef value_type& ustring::reference |
typedef size_t ustring::size_type |
typedef char ustring::value_type |
|
inlinenoexcept |
|
inlineexplicit |
|
inlineexplicit |
Construct a ustring from a string_view, which can be auto-converted from either a null-terminated C string (char *) or a C++ std::string.
|
inline |
|
inline |
|
inline |
|
inlinenoexcept |
|
inline |
|
inline |
|
inline |
|
inline |
Assign a string_view to *this.
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
static |
Concatenate two strings, returning a ustring, implemented carefully to not perform any redundant copies or allocations. This is semantically equivalent to ustring::sprintf("%s%s", s, t)
, but is more efficient.
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinestatic |
Construct a ustring in a fmt::format-like fashion. In other words, something like: ustring s = ustring::fmtformat("blah {} {}", (int)foo, (float)bar); The argument list is fully typesafe. The formatting of the string will always use the classic "C" locale conventions (in particular, '.' as decimal separator for float values).
|
inlinestatic |
|
inlinestatic |
|
static |
Return the statistics output as a string.
|
inlinenoexcept |
|
static |
Return the total number ustrings that have the exact hash as another ustring. If collisions
is passed, store all the colliding ustrings in the vector.
|
inlinestatic |
|
inlinenoexcept |
|
static |
Given a string_view, return a pointer to the unique version kept in the internal table (creating a new table entry if we haven't seen this sequence of characters before). N.B.: this is equivalent to ustring(str).c_str(). It's also the routine that is used directly by ustring's internals to generate the canonical unique copy of the characters.
|
static |
Return the amount of memory consumed by the ustring table.
|
inlineexplicitnoexcept |
|
inlinenoexcept |
Conversion to string_view.
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
Test a ustring (*this) for lexicographic inequality with string_view x.
|
inlinenoexcept |
|
inlinenoexcept |
|
inline |
|
inline |
|
inline |
Assign a string_view to a ustring.
|
inline |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
Test a ustring (*this) for lexicographic equality with string_view x.
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinestatic |
Construct a ustring in a printf-like fashion. In other words, something like: ustring s = ustring::sprintf("blah %d %g", (int)foo, (float)bar); The argument list is fully typesafe. The formatting of the string will always use the classic "C" locale conventions (in particular, '.' as decimal separator for float values).
|
inlinenoexcept |
|
static |
Return the total number of ustrings in the internal table.
|
friend |
|
friend |
|
friend |
Test for lexicographic inequality between string_view a and ustring b.
|
friend |
|
friend |
|
friend |
Test for lexicographic equality between string_view a and ustring b.
|
friend |