Cogs.Core
Public Types | Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
Cogs::StringView Class Reference

Provides a weakly referenced view over the contents of a string. More...

#include <StringView.h>

Public Types

using iterator = const char *
 Iterator type.
 
using const_iterator = const char *
 Const iterator type.
 
using value_type = char
 Value type.
 
using Vector = Cogs::Collections::SmallVector< StringView, 10 >
 Vector of strings type.
 

Public Member Functions

constexpr StringView () noexcept=default
 Constructs an empty string view.
 
constexpr StringView (const char *data) noexcept
 Construct a string view of the given null-terminated string.
 
template<size_t LENGTH>
constexpr StringView (const char(&data)[LENGTH]) noexcept
 Construct a string view from the given string literal.
 
constexpr StringView (const char *data, const size_t length) noexcept
 Construct a string view of the given string data, with the given length.
 
constexpr StringView (const std::string_view s) noexcept
 Construct a String view of the given std::string_view object.
 
 StringView (const std::string &s) noexcept
 Construct a string view of the given string object.
 
bool operator== (const StringView &other) const
 Operator equal.
 
constexpr iterator begin () noexcept
 Iterator to the beginning of the string.
 
constexpr iterator end () noexcept
 Iterator to the end of the string.
 
constexpr const_iterator begin () const noexcept
 Const iterator to the beginning of the string.
 
constexpr const_iterator end () const noexcept
 Const iterator to the end of the string.
 
constexpr const_iterator cbegin () const noexcept
 Const iterator to the beginning of the string.
 
constexpr const_iterator cend () const noexcept
 Const iterator to the end of the string.
 
constexpr bool empty () const noexcept
 Check if the string is empty.
 
constexpr operator bool () const noexcept
 Explicit boolean conversion operator.
 
 operator std::string () const
 Explicit std::string conversion operator.
 
constexpr operator std::string_view () const noexcept
 Implicit std::string_view conversion operator.
 
std::string to_string () const
 String conversion method.
 
constexpr std::string_view to_string_view () const noexcept
 Create a standard library string_view of the same view.
 
constexpr const char * data () const noexcept
 Get the sequence of characters referenced by the string view.
 
constexpr size_t size () const noexcept
 Get the size of the string.
 
constexpr size_t length () const noexcept
 Get the length of the string.
 
constexpr char operator[] (const size_t index) const noexcept
 Get the character at position index in the string.
 
constexpr size_t hash () const noexcept
 Get the hash code of the string.
 
constexpr size_t hash (size_t hashValue) const noexcept
 Combine the hash code of the string with the initial hash value provided.
 
size_t hashLowercase (size_t hashValue=Cogs::hash()) const noexcept
 Get the hash code of the string converted to lowercase.
 
size_t find (const StringView &other, size_t offset=0) const noexcept
 Find the given string segment inside the string.
 
size_t find_first_of (char character, size_t pos=0) const noexcept
 Find the first occurance of the given character from the specified starting position.
 
size_t find_last_of (const StringView characters, size_t pos=NoPosition) const noexcept
 See std::basic_string::find_last_of.
 
size_t find_last_of (const char character, size_t pos=NoPosition) const noexcept
 See std::basic_string::find_last_of.
 
int compare (const StringView &other) const noexcept
 Lexicographically compares the content of this string view with the given other.
 
constexpr StringView substr (size_t offset, size_t count=NoPosition) const noexcept
 Get the given sub string.
 
Vector split (const StringView &delimiters) const
 Split this string into parts.
 
Vector splitAll (const StringView &delimiters) const
 Split this string into parts, empty parts will be retained.
 
StringViewtrimStart () noexcept
 Remove any whitespace from the start of this StringView's contents.
 
StringViewtrimEnd () noexcept
 Remove any whitespace from the end of this StringView's contents.
 
StringViewtrim () noexcept
 Remove any whitespace from the start and end of this StringView's contents.
 

Static Public Attributes

static constexpr size_t npos = static_cast<size_t>(-1)
 No position.
 
static constexpr size_t NoPosition = npos
 No position.
 

Private Types

using traits_type = std::char_traits< char >
 

Private Member Functions

int compareMemory (const char *a, const char *b, size_t length) const
 Compare two memory sequences.
 

Static Private Member Functions

static bool isWhiteSpace (char c)
 Helper function that tests whether the given character is an ASCII whitespace character.
 

Private Attributes

const char * str = nullptr
 Pointer to the string this view is over.
 
size_t strSize = 0
 Length of the string data pointed to.
 

Detailed Description

Provides a weakly referenced view over the contents of a string.

E.g. same usage rules as std::string_view

No ownership is assumed over the given string, making the valid lifetime of a StringView bound to the lifetime of the string data it is constructed from. Therefore, it is usually not safe to store instances of StringView in objects, except when it can be reasonably guaranteed that the strings in question are literals or otherwise exists in the StringView instance usage lifetime.

Definition at line 23 of file StringView.h.

Member Typedef Documentation

◆ const_iterator

using Cogs::StringView::const_iterator = const char *

Const iterator type.

Definition at line 31 of file StringView.h.

◆ iterator

using Cogs::StringView::iterator = const char *

Iterator type.

Definition at line 28 of file StringView.h.

◆ traits_type

using Cogs::StringView::traits_type = std::char_traits<char>
private

Definition at line 25 of file StringView.h.

◆ value_type

Value type.

Definition at line 34 of file StringView.h.

◆ Vector

Vector of strings type.

Definition at line 37 of file StringView.h.

Constructor & Destructor Documentation

◆ StringView() [1/5]

constexpr Cogs::StringView::StringView ( const char *  data)
inlineconstexprnoexcept

Construct a string view of the given null-terminated string.

The string length is automatically calculated from the string data.

Parameters
dataPointer to string data in memory.

Definition at line 54 of file StringView.h.

◆ StringView() [2/5]

template<size_t LENGTH>
constexpr Cogs::StringView::StringView ( const char(&)  data[LENGTH])
inlineconstexprnoexcept

Construct a string view from the given string literal.

Parameters
dataPointer to the string literal.

Definition at line 65 of file StringView.h.

◆ StringView() [3/5]

constexpr Cogs::StringView::StringView ( const char *  data,
const size_t  length 
)
inlineconstexprnoexcept

Construct a string view of the given string data, with the given length.

Parameters
dataPointer to string data in memory.
lengthLength of the string data, not including null-termination, if any.

Definition at line 74 of file StringView.h.

◆ StringView() [4/5]

constexpr Cogs::StringView::StringView ( const std::string_view  s)
inlineconstexprnoexcept

Construct a String view of the given std::string_view object.

Parameters
sstring_view to create a view of.

Definition at line 80 of file StringView.h.

◆ StringView() [5/5]

Cogs::StringView::StringView ( const std::string &  s)
noexcept

Construct a string view of the given string object.

Parameters
sString to create a view of.

Definition at line 5 of file StringView.cpp.

Member Function Documentation

◆ begin() [1/2]

constexpr const_iterator Cogs::StringView::begin ( ) const
inlineconstexprnoexcept

Const iterator to the beginning of the string.

Definition at line 106 of file StringView.h.

◆ begin() [2/2]

constexpr iterator Cogs::StringView::begin ( )
inlineconstexprnoexcept

Iterator to the beginning of the string.

Definition at line 100 of file StringView.h.

Referenced by Cogs::ContextD3D11::pushCommandGroupAnnotation(), and Cogs::ContextD3D11::setAnnotationMarker().

◆ cbegin()

constexpr const_iterator Cogs::StringView::cbegin ( ) const
inlineconstexprnoexcept

Const iterator to the beginning of the string.

Definition at line 112 of file StringView.h.

◆ cend()

constexpr const_iterator Cogs::StringView::cend ( ) const
inlineconstexprnoexcept

Const iterator to the end of the string.

Definition at line 115 of file StringView.h.

◆ compare()

int Cogs::StringView::compare ( const StringView other) const
noexcept

Lexicographically compares the content of this string view with the given other.

Parameters
otherStringView instance to compare to.
Returns
An integer signaling the order of the strings. See std::basic_string::compare() docs for details.

Definition at line 68 of file StringView.cpp.

◆ compareMemory()

int Cogs::StringView::compareMemory ( const char *  a,
const char *  b,
size_t  length 
) const
inlineprivate

Compare two memory sequences.

Definition at line 298 of file StringView.h.

◆ data()

constexpr const char * Cogs::StringView::data ( ) const
inlineconstexprnoexcept

◆ empty()

constexpr bool Cogs::StringView::empty ( ) const
inlineconstexprnoexcept

◆ end() [1/2]

constexpr const_iterator Cogs::StringView::end ( ) const
inlineconstexprnoexcept

Const iterator to the end of the string.

Definition at line 109 of file StringView.h.

◆ end() [2/2]

constexpr iterator Cogs::StringView::end ( )
inlineconstexprnoexcept

Iterator to the end of the string.

Definition at line 103 of file StringView.h.

Referenced by Cogs::ContextD3D11::pushCommandGroupAnnotation(), and Cogs::ContextD3D11::setAnnotationMarker().

◆ find()

size_t Cogs::StringView::find ( const StringView other,
size_t  offset = 0 
) const
noexcept

Find the given string segment inside the string.

Starts the search at character offset.

Returns
Starting position of the found string, NoPosition if not found.

Definition at line 18 of file StringView.cpp.

◆ find_first_of()

size_t Cogs::StringView::find_first_of ( char  character,
size_t  pos = 0 
) const
noexcept

Find the first occurance of the given character from the specified starting position.

Definition at line 46 of file StringView.cpp.

Referenced by split(), and splitAll().

◆ find_last_of() [1/2]

size_t Cogs::StringView::find_last_of ( const char  character,
size_t  pos = NoPosition 
) const
noexcept

See std::basic_string::find_last_of.

Definition at line 56 of file StringView.cpp.

◆ find_last_of() [2/2]

size_t Cogs::StringView::find_last_of ( const StringView  characters,
size_t  pos = NoPosition 
) const
noexcept

See std::basic_string::find_last_of.

Definition at line 31 of file StringView.cpp.

◆ hash() [1/2]

constexpr size_t Cogs::StringView::hash ( ) const
inlineconstexprnoexcept

◆ hash() [2/2]

constexpr size_t Cogs::StringView::hash ( size_t  hashValue) const
inlineconstexprnoexcept

Combine the hash code of the string with the initial hash value provided.

Deprecated. Use Cogs::hash(string, hashValue)

Returns
Hash code.

Definition at line 209 of file StringView.h.

References Cogs::hash().

◆ hashLowercase()

size_t Cogs::StringView::hashLowercase ( size_t  hashValue = Cogs::hash()) const
noexcept

Get the hash code of the string converted to lowercase.

Deprecated. Use Cogs::hashLowercase(string, hashValue)

Lowercase is here defined as default "C" locale, i.e., A-Z is converted to a-z.

Returns
Hash code.

Definition at line 13 of file StringView.cpp.

References Cogs::hashLowercase().

Referenced by Cogs::Core::ModelManager::handleLoad(), Cogs::Core::parseBool_(), Cogs::Core::MaterialInstance::setTextureAddressMode(), and Cogs::Core::MaterialInstance::setTextureFilterMode().

◆ isWhiteSpace()

static bool Cogs::StringView::isWhiteSpace ( char  c)
inlinestaticprivate

Helper function that tests whether the given character is an ASCII whitespace character.

Tests for tab (\t), line feed (
), carriage return (\r), and space ( ).

Definition at line 311 of file StringView.h.

◆ length()

constexpr size_t Cogs::StringView::length ( ) const
inlineconstexprnoexcept

Get the length of the string.

Equivalent to size().

Returns
Length of the string.

Definition at line 185 of file StringView.h.

Referenced by Cogs::RenderTargetsD3D11::annotate(), Cogs::TexturesD3D11::annotate(), Cogs::EffectsGLES30::getSamplerStateBinding(), and Cogs::Core::parseInt_().

◆ operator bool()

constexpr Cogs::StringView::operator bool ( ) const
inlineexplicitconstexprnoexcept

Explicit boolean conversion operator.

Provided for better check before use semantics.

Returns
True if the string is non-empty.

Definition at line 129 of file StringView.h.

◆ operator std::string()

Cogs::StringView::operator std::string ( ) const
inlineexplicit

Explicit std::string conversion operator.

Provided to make it possible to cast a string view to a std::string.

Returns
A std::string object with a copy of the string held by this StringView.

Definition at line 137 of file StringView.h.

◆ operator std::string_view()

constexpr Cogs::StringView::operator std::string_view ( ) const
inlineconstexprnoexcept

Implicit std::string_view conversion operator.

Provided to make StringView and std::string_view buddies (transition to std::string_view).

Returns
A std::string_view of the string view.

Definition at line 145 of file StringView.h.

◆ operator==()

bool Cogs::StringView::operator== ( const StringView other) const
inline

Operator equal.

Compares this instance to the given StringView. To consider the string views equal, the length and content of the referenced strings must match exactly.

Parameters
otherStringView instance to compare this instance to.
Returns
A boolean indicating if the strings are equal.

Definition at line 97 of file StringView.h.

References str, and strSize.

◆ operator[]()

constexpr char Cogs::StringView::operator[] ( const size_t  index) const
inlineconstexprnoexcept

Get the character at position index in the string.

Returns
Character at position index.

Definition at line 192 of file StringView.h.

◆ size()

constexpr size_t Cogs::StringView::size ( ) const
inlineconstexprnoexcept

◆ split()

Cogs::StringView::Vector Cogs::StringView::split ( const StringView delimiters) const

Split this string into parts.

Definition at line 79 of file StringView.cpp.

References find_first_of().

◆ splitAll()

Cogs::StringView::Vector Cogs::StringView::splitAll ( const StringView delimiters) const

Split this string into parts, empty parts will be retained.

Definition at line 102 of file StringView.cpp.

References find_first_of().

◆ substr()

constexpr StringView Cogs::StringView::substr ( size_t  offset,
size_t  count = NoPosition 
) const
inlineconstexprnoexcept

◆ to_string()

std::string Cogs::StringView::to_string ( ) const

String conversion method.

Provided to match standard library API. Deprecated: Use std::string(StringViewObject) for portability when converting to std::string_view

Returns
A std::string object with a copy of the string held by this StringView.

Definition at line 9 of file StringView.cpp.

References str, and strSize.

Referenced by Cogs::Core::ExportCommand::apply(), Cogs::Core::RemapMaterialCommand::apply(), Cogs::Core::AudioExtension::initialize(), Cogs::Core::OGC3DTilesSystem::loadMissingModels(), Cogs::Core::ModelManager::loadModel(), Cogs::Core::TextureManager::loadTexture(), Cogs::Core::TextureManager::loadTextureFromMemory(), Cogs::RationalReducerExtension::AnnotationComponent::setValue(), and Cogs::Core::Variable::setValue().

◆ to_string_view()

constexpr std::string_view Cogs::StringView::to_string_view ( ) const
inlineconstexprnoexcept

Create a standard library string_view of the same view.

Deprecated: Use std::string_view(StringViewObject)

Returns
A std::string_view of the string view.

Definition at line 161 of file StringView.h.

◆ trim()

Cogs::StringView & Cogs::StringView::trim ( )
noexcept

Remove any whitespace from the start and end of this StringView's contents.

Definition at line 135 of file StringView.cpp.

References trimEnd().

◆ trimEnd()

Cogs::StringView & Cogs::StringView::trimEnd ( )
noexcept

Remove any whitespace from the end of this StringView's contents.

Definition at line 127 of file StringView.cpp.

Referenced by trim().

◆ trimStart()

Cogs::StringView & Cogs::StringView::trimStart ( )
noexcept

Remove any whitespace from the start of this StringView's contents.

Definition at line 118 of file StringView.cpp.

Member Data Documentation

◆ NoPosition

constexpr size_t Cogs::StringView::NoPosition = npos
staticconstexpr

No position.

Definition at line 43 of file StringView.h.

Referenced by Cogs::Core::RemapMaterialCommand::apply(), and Cogs::Core::TextureManager::getTexture().

◆ npos

constexpr size_t Cogs::StringView::npos = static_cast<size_t>(-1)
staticconstexpr

No position.

Definition at line 40 of file StringView.h.

◆ str

const char* Cogs::StringView::str = nullptr
private

Pointer to the string this view is over.

Definition at line 301 of file StringView.h.

Referenced by operator==(), and to_string().

◆ strSize

size_t Cogs::StringView::strSize = 0
private

Length of the string data pointed to.

Definition at line 304 of file StringView.h.

Referenced by operator==(), and to_string().


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