Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::StringSpan Struct Reference

An read-only view over a string (to avoid including Strings library when parsing is not needed). More...

#include <StringSpan.h>

Inheritance diagram for SC::StringSpan:
SC::StringView

Public Member Functions

constexpr StringSpan (StringEncoding encoding=StringEncoding::Ascii)
 Construct an empty StringView.
 
constexpr StringSpan (Span< const char > text, bool nullTerm, StringEncoding encoding)
 Construct a StringView from a Span of bytes.
 
template<size_t N>
constexpr StringSpan (const char(&str)[N])
 Constructs a StringView with a null terminated string terminal.
 
constexpr bool operator== (const StringSpan other) const
 
Span< const char > toCharSpan () const
 Obtain a const char Span from this StringView.
 
constexpr bool isEmpty () const
 Return true if StringView is empty.
 
constexpr bool isNullTerminated () const
 Check if StringView is immediately followed by a null termination character.
 
constexpr size_t sizeInBytes () const
 Get size of the StringView in bytes.
 
constexpr StringEncoding getEncoding () const
 Get encoding of this StringView.
 
constexpr const char * bytesWithoutTerminator () const
 Directly access the memory of this StringView.
 
auto getNullTerminatedNative () const
 Directly access the memory of this null terminated-StringView.
 

Static Public Member Functions

static constexpr StringSpan fromNullTerminated (const char *text, StringEncoding encoding)
 Constructs a StringView from a null terminated string.
 

Protected Attributes

union { 
 
   const char *   text 
 
};  
 
size_t textSizeInBytes: sizeof(size_t) * 8 - NumOptionBits
 
size_t encoding: 2
 
size_t hasNullTerm: 1
 

Static Protected Attributes

static constexpr size_t NumOptionBits = 3
 
static constexpr size_t MaxLength = (~static_cast<size_t>(0)) >> NumOptionBits
 

Friends

struct StringView
 

Detailed Description

An read-only view over a string (to avoid including Strings library when parsing is not needed).

The most common use case is to pass it in and out of OS API as is for file system paths. Some libraries check the encoding and eventually convert the string to a different one when using specific OS api.

Constructor & Destructor Documentation

◆ StringSpan() [1/3]

SC::StringSpan::StringSpan ( StringEncoding encoding = StringEncoding::Ascii)
inlineconstexpr

Construct an empty StringView.

◆ StringSpan() [2/3]

SC::StringSpan::StringSpan ( Span< const char > text,
bool nullTerm,
StringEncoding encoding )
inlineconstexpr

Construct a StringView from a Span of bytes.

Parameters
textThe span containing the text EXCLUDING eventual null terminator
nullTermtrue if a null terminator code point is expected to be found after Span. On ASCII and UTF8 this is 1 byte, on UTF16 it must be 2 bytes.
encodingThe encoding of the text contained in this StringView

◆ StringSpan() [3/3]

template<size_t N>
SC::StringSpan::StringSpan ( const char(&) str[N])
inlineconstexpr

Constructs a StringView with a null terminated string terminal.

Member Function Documentation

◆ bytesWithoutTerminator()

const char * SC::StringSpan::bytesWithoutTerminator ( ) const
inlinenodiscardconstexpr

Directly access the memory of this StringView.

◆ fromNullTerminated()

static constexpr StringSpan SC::StringSpan::fromNullTerminated ( const char * text,
StringEncoding encoding )
inlinestaticconstexpr

Constructs a StringView from a null terminated string.

◆ getEncoding()

StringEncoding SC::StringSpan::getEncoding ( ) const
inlinenodiscardconstexpr

Get encoding of this StringView.

◆ getNullTerminatedNative()

auto SC::StringSpan::getNullTerminatedNative ( ) const
inlinenodiscard

Directly access the memory of this null terminated-StringView.

Returns
Pointer to start of StringView memory. On Windows return type will be const wchar_t*. On other platforms return type will be const char*.

◆ isEmpty()

bool SC::StringSpan::isEmpty ( ) const
inlinenodiscardconstexpr

Return true if StringView is empty.

◆ isNullTerminated()

bool SC::StringSpan::isNullTerminated ( ) const
inlinenodiscardconstexpr

Check if StringView is immediately followed by a null termination character.

◆ sizeInBytes()

size_t SC::StringSpan::sizeInBytes ( ) const
inlinenodiscardconstexpr

Get size of the StringView in bytes.

◆ toCharSpan()

Span< const char > SC::StringSpan::toCharSpan ( ) const
inlinenodiscard

Obtain a const char Span from this StringView.


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