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

Classes

struct  NativeWritable
 

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.
 
size_t sizeInBytesIncludingTerminator () const
 Get size of the StringView in bytes, including null terminator (2 bytes on UTF16)
 
constexpr StringEncoding getEncoding () const
 Get encoding of this StringView.
 
constexpr const char * bytesWithoutTerminator () const
 Directly access the memory of this StringView.
 
const char * bytesIncludingTerminator () const
 Directly access the memory of this null terminated-StringView.
 
const char * getNullTerminatedNative () const
 Directly access the memory of this null terminated-StringView:
 
Result writeNullTerminatedTo (NativeWritable &string) const
 Writes this Span to a destination Span, using native encoding and null-terminating it.
 
Result appendNullTerminatedTo (NativeWritable &string, bool removePreviousNullTerminator=true) const
 Appends this Span to a destination Span, using native encoding and null-terminating it.
 

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

◆ appendNullTerminatedTo()

Result SC::StringSpan::appendNullTerminatedTo ( NativeWritable & string,
bool removePreviousNullTerminator = true ) const

Appends this Span to a destination Span, using native encoding and null-terminating it.

Parameters
stringThe destination writable string to write to
removePreviousNullTerminatorIf true, the previous null terminator is removed

◆ bytesIncludingTerminator()

const char * SC::StringSpan::bytesIncludingTerminator ( ) const
nodiscard

Directly access the memory of this null terminated-StringView.

Warning
This method will assert if string is not null terminated.

◆ 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()

const char * SC::StringSpan::getNullTerminatedNative ( ) const
inlinenodiscard

Directly access the memory of this null terminated-StringView:

  • 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.

◆ sizeInBytesIncludingTerminator()

size_t SC::StringSpan::sizeInBytesIncludingTerminator ( ) const
nodiscard

Get size of the StringView in bytes, including null terminator (2 bytes on UTF16)

Warning
This method will Assert if this isNullTerminated returns false

◆ toCharSpan()

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

Obtain a const char Span from this StringView.

◆ writeNullTerminatedTo()

Result SC::StringSpan::writeNullTerminatedTo ( NativeWritable & string) const

Writes this Span to a destination Span, using native encoding and null-terminating it.

Parameters
stringThe destination writable string to write to

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