Owned NUL-terminated buffer, safe to use across a DLL boundary. More...
#include <StormByte/safe/cstring.hxx>
Public Member Functions | |
Life | |
| CString () noexcept | |
| Empty (null) buffer. | |
| CString (const char *str) noexcept | |
Copies str. | |
| CString (std::string_view sv) noexcept | |
Copies sv onto Base's heap. | |
| CString (const std::string &str) noexcept | |
Copies str onto Base's heap. | |
| CString (const WCString &text) noexcept | |
Copies text as UTF-8. | |
| CString (const CString &other) noexcept | |
| Copy constructor. | |
| CString (CString &&other) noexcept | |
| Move constructor. | |
| ~CString () noexcept | |
| Releases the buffer. | |
| CString & | operator= (const CString &other) noexcept |
| Copy assignment. | |
| CString & | operator= (CString &&other) noexcept |
| Move assignment. | |
Modifiers | |
| void | Reset (const char *str=nullptr) noexcept |
Replaces the buffer with a copy of str. | |
| void | swap (CString &other) noexcept |
Swaps buffers with other. | |
Observers | |
| Size | Length () const noexcept |
Character count (strlen), or 0 when empty or null. | |
| char | operator[] (const Size &index) const noexcept |
Character at index. | |
| operator bool () const noexcept | |
true when the buffer pointer is not null. | |
Conversions | |
| operator const char * () const noexcept | |
| View of the owned buffer. | |
| operator std::string_view () const noexcept | |
| Non-owning view of the text. | |
| STORMBYTE_FORCE_INLINE | operator std::string () const |
| Copy of the text in the caller’s heap. | |
| STORMBYTE_FORCE_INLINE std::ostream & | operator<< (std::ostream &stream) const |
Writes the text to stream. | |
Comparison | |
| bool | operator== (const CString &other) const noexcept |
| Content equality. | |
| bool | operator!= (const CString &other) const noexcept |
| Content inequality. | |
| bool | operator== (const char *str) const noexcept |
| Content equality with a C string. | |
| bool | operator!= (const char *str) const noexcept |
| Content inequality with a C string. | |
| std::strong_ordering | operator<=> (const CString &other) const noexcept |
| Content order. | |
| std::strong_ordering | operator<=> (const char *str) const noexcept |
| Content order against a C string. | |
Owned NUL-terminated buffer, safe to use across a DLL boundary.
This is not a replacement or reimplementation of std::string. The class is minimal on purpose: copy, move, reset, a C-string view, Length, subscript, equality, ordering, swap and conversions.
operator const char* is the analogue of std::string::c_str(). The pointer is valid only until this object is destroyed, moved from, assigned or Reset. Using it afterwards is use-after-free.
operator std::string_view is explicit and follows the same lifetime. A null buffer yields an empty view. The view covers [0, Length()) and does not include the trailing NUL.
operator bool is true when the pointer is not null. A buffer constructed from "" is empty (Length() == 0) and valid. A default-constructed object is null.
Construction from std::string / std::string_view copies onto Base's heap. It is not a heap steal. An empty source yields "", not a null buffer.
operator[] is an observer. Valid indices are [0, Length()]; Length() is the trailing NUL. A null buffer or an index past Length() is undefined and asserts when assertions are on.
Equality and <=> compare text, not addresses. Two nulls are equal. Null is not equal to "". Null orders before any text.
operator std::string and operator<< are STORMBYTE_FORCE_INLINE so the caller CRT owns the string and the stream buffer. inline on an exported class can still be a call into this DLL.
If the text never leaves the module that created it, or the program is not built for Windows, use std::string.
|
noexcept |
Empty (null) buffer.
|
explicitnoexcept |
Copies str.
| str | Source; may be null. |
|
explicitnoexcept |
Copies sv onto Base's heap.
| sv | Source view. Copied up to the first NUL, then terminated. |
"".
|
explicitnoexcept |
Copies str onto Base's heap.
| str | Source. Remains valid and unchanged. |
"".
|
explicitnoexcept |
Copies text as UTF-8.
| text | Wide text. Null stays null. |
|
noexcept |
Copy constructor.
| other | Buffer to copy. |
|
noexcept |
Move constructor.
| other | Buffer to take. other becomes null. |
|
noexcept |
Releases the buffer.
|
noexcept |
Character count (strlen), or 0 when empty or null.
|
inlineexplicitnoexcept |
true when the buffer pointer is not null.
"" is valid and empty. A default object is null.
|
explicitnoexcept |
View of the owned buffer.
std::string::c_str().
|
inline |
Copy of the text in the caller’s heap.
|
inlineexplicitnoexcept |
Non-owning view of the text.
std::string::c_str().
|
inlinenoexcept |
Content inequality with a C string.
| str | May be null. |
|
inlinenoexcept |
Content inequality.
| other | Other buffer. |
|
inline |
Writes the text to stream.
| stream | Destination. |
stream.
|
noexcept |
Content order against a C string.
| str | May be null. |
|
noexcept |
Content order.
Null is less than any text.
| other | Other buffer. |
Copy assignment.
| other | Buffer to copy. |
Move assignment.
| other | Buffer to take. other becomes null. |
|
noexcept |
Content equality with a C string.
| str | May be null (treated as a null CString). |
|
noexcept |
Content equality.
| other | Other buffer. |
|
noexcept |
|
noexcept |
Replaces the buffer with a copy of str.
| str | Source; may be null. |
|
noexcept |
Swaps buffers with other.
| other | Other buffer. |