StormByte 2.0.0
C++26 foundation of the StormByte suite
 
Loading...
Searching...
No Matches
StormByte::WCString Class Reference

Owned NUL-terminated wide buffer, safe to use across a DLL boundary. More...

#include <StormByte/wcstring.hxx>

Public Member Functions

Life
 WCString () noexcept
 Empty (null) buffer.
 
 WCString (const wchar_t *str) noexcept
 Copies str.
 
 WCString (const WCString &other) noexcept
 Copy constructor.
 
 WCString (WCString &&other) noexcept
 Move constructor.
 
 ~WCString () noexcept
 Releases the buffer.
 
WCString & operator= (const WCString &other) noexcept
 Copy assignment.
 
WCString & operator= (WCString &&other) noexcept
 Move assignment.
 
Modifiers
void Reset (const wchar_t *str=nullptr) noexcept
 Replaces the buffer with a copy of str.
 
void swap (WCString &other) noexcept
 Swaps buffers with other.
 
Observers
std::size_t Length () const noexcept
 Character count (wcslen), or 0 when empty or null.
 
wchar_t operator[] (std::size_t index) const noexcept
 Character at index.
 
 operator bool () const noexcept
 true when the buffer pointer is not null.
 
Conversions
 operator const wchar_t * () const noexcept
 View of the owned buffer.
 
 operator std::wstring_view () const noexcept
 Non-owning view of the text.
 
 operator std::wstring () const
 Copy of the text in the caller’s heap.
 
std::wostream & operator<< (std::wostream &stream) const
 Writes the text to stream.
 
Comparison
bool operator== (const WCString &other) const noexcept
 Content equality.
 
bool operator!= (const WCString &other) const noexcept
 Content inequality.
 
bool operator== (const wchar_t *str) const noexcept
 Content equality with a C wide string.
 
bool operator!= (const wchar_t *str) const noexcept
 Content inequality with a C wide string.
 
std::strong_ordering operator<=> (const WCString &other) const noexcept
 Content order.
 
std::strong_ordering operator<=> (const wchar_t *str) const noexcept
 Content order against a C wide string.
 

Detailed Description

Owned NUL-terminated wide buffer, safe to use across a DLL boundary.

Wide counterpart of CString. Not a replacement of std::wstring. The class is minimal: copy, move, reset, a C wide-string view, Length, subscript, equality, ordering, swap and conversions.

operator const wchar_t* is the analogue of std::wstring::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::wstring_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 L"" is empty (Length() == 0) and valid. A default-constructed object is null.

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 L"". Null orders before any text.

operator std::wstring, operator std::wstring_view and operator<< are inline so they run in the caller’s translation unit.

Constructor & Destructor Documentation

◆ WCString() [1/4]

StormByte::WCString::WCString ( )
noexcept

Empty (null) buffer.

◆ WCString() [2/4]

StormByte::WCString::WCString ( const wchar_t *  str)
explicitnoexcept

Copies str.

Parameters
strSource; may be null.

◆ WCString() [3/4]

StormByte::WCString::WCString ( const WCString &  other)
noexcept

Copy constructor.

Parameters
otherBuffer to copy.

◆ WCString() [4/4]

StormByte::WCString::WCString ( WCString &&  other)
noexcept

Move constructor.

Parameters
otherBuffer to take. other becomes null.

◆ ~WCString()

StormByte::WCString::~WCString ( )
noexcept

Releases the buffer.

Member Function Documentation

◆ Length()

std::size_t StormByte::WCString::Length ( ) const
noexcept

Character count (wcslen), or 0 when empty or null.

Returns
Length.

◆ operator bool()

StormByte::WCString::operator bool ( ) const
inlineexplicitnoexcept

true when the buffer pointer is not null.

Note
L"" is valid and empty. A default object is null.
Returns
Whether a buffer is held.

◆ operator const wchar_t *()

StormByte::WCString::operator const wchar_t * ( ) const
explicitnoexcept

View of the owned buffer.

Returns
Buffer, or null.
Note
Same lifetime rules as std::wstring::c_str().

◆ operator std::wstring()

StormByte::WCString::operator std::wstring ( ) const
inline

Copy of the text in the caller’s heap.

Returns
Empty string when the buffer is null.

◆ operator std::wstring_view()

StormByte::WCString::operator std::wstring_view ( ) const
inlineexplicitnoexcept

Non-owning view of the text.

Returns
Empty view when the buffer is null.
Note
Same lifetime rules as std::wstring::c_str().

◆ operator!=() [1/2]

bool StormByte::WCString::operator!= ( const wchar_t *  str) const
inlinenoexcept

Content inequality with a C wide string.

Parameters
strMay be null.
Returns
Whether the texts differ.

◆ operator!=() [2/2]

bool StormByte::WCString::operator!= ( const WCString &  other) const
inlinenoexcept

Content inequality.

Parameters
otherOther buffer.
Returns
Whether the texts differ.

◆ operator<<()

std::wostream & StormByte::WCString::operator<< ( std::wostream &  stream) const
inline

Writes the text to stream.

Parameters
streamDestination.
Returns
stream.

◆ operator<=>() [1/2]

std::strong_ordering StormByte::WCString::operator<=> ( const wchar_t *  str) const
noexcept

Content order against a C wide string.

Parameters
strMay be null.
Returns
Ordering.

◆ operator<=>() [2/2]

std::strong_ordering StormByte::WCString::operator<=> ( const WCString &  other) const
noexcept

Content order.

Null is less than any text.

Parameters
otherOther buffer.
Returns
Ordering.

◆ operator=() [1/2]

WCString & StormByte::WCString::operator= ( const WCString &  other)
noexcept

Copy assignment.

Parameters
otherBuffer to copy.
Returns
*this.

◆ operator=() [2/2]

WCString & StormByte::WCString::operator= ( WCString &&  other)
noexcept

Move assignment.

Parameters
otherBuffer to take. other becomes null.
Returns
*this.

◆ operator==() [1/2]

bool StormByte::WCString::operator== ( const wchar_t *  str) const
noexcept

Content equality with a C wide string.

Parameters
strMay be null (treated as a null WCString).
Returns
Whether the texts are equal.

◆ operator==() [2/2]

bool StormByte::WCString::operator== ( const WCString &  other) const
noexcept

Content equality.

Parameters
otherOther buffer.
Returns
Whether the texts are equal.

◆ operator[]()

wchar_t StormByte::WCString::operator[] ( std::size_t  index) const
noexcept

Character at index.

Parameters
indexPosition in [0, Length()]. Length() is the trailing NUL.
Returns
The character.
Note
Null or index > Length() is undefined. Checked with assert when assertions are on.

◆ Reset()

void StormByte::WCString::Reset ( const wchar_t *  str = nullptr)
noexcept

Replaces the buffer with a copy of str.

Parameters
strSource; may be null.

◆ swap()

void StormByte::WCString::swap ( WCString &  other)
noexcept

Swaps buffers with other.

Parameters
otherOther buffer.

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