Cogs.Foundation
Loading...
Searching...
No Matches
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.

Member Typedef Documentation

◆ const_iterator

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

Const iterator type.

◆ iterator

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

Iterator type.

◆ traits_type

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

◆ value_type

Value type.

◆ Vector

Vector of strings type.

Constructor & Destructor Documentation

◆ StringView() [1/6]

constexpr Cogs::StringView::StringView ( )
constexprdefaultnoexcept

Constructs an empty string view.

◆ StringView() [2/6]

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.

◆ StringView() [3/6]

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.

◆ StringView() [4/6]

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.

◆ StringView() [5/6]

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.

◆ StringView() [6/6]

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

Construct a string view of the given string object.

Parameters
sString to create a view of.

Member Function Documentation

◆ begin() [1/2]

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

Const iterator to the beginning of the string.

◆ begin() [2/2]

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

Iterator to the beginning of the string.

◆ cbegin()

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

Const iterator to the beginning of the string.

◆ cend()

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

Const iterator to the end of the string.

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

◆ compareMemory()

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

Compare two memory sequences.

◆ data()

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

Get the sequence of characters referenced by the string view.

Note: the data is not necessarily null-terminated, and may even contain null characters. To get a null-terminated string, use to_string.

Returns
A pointer to the underlying sequence of characters.

Referenced by Cogs::ImmutableString::ImmutableString(), and Cogs::Memory::MemoryBuffer::writeString().

◆ empty()

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

Check if the string is empty.

Returns
True if the string is empty (string length is zero).

Referenced by Cogs::Reflection::TypeDatabase::createType(), and Cogs::ImmutableString::ImmutableString().

◆ end() [1/2]

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

Const iterator to the end of the string.

◆ end() [2/2]

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

Iterator to the end of the string.

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

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

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.

References Cogs::N.

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

References Cogs::N.

◆ hash() [1/2]

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

Get the hash code of the string.

Returns
Hash code.

References Cogs::hash().

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

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.

References Cogs::hashLowercase().

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

◆ length()

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

Get the length of the string.

Equivalent to size().

Returns
Length of the string.

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

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

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

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

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.

◆ size()

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

Get the size of the string.

Equivalent to length().

Returns
Length of the string.

Referenced by Cogs::ImmutableString::ImmutableString(), and Cogs::Memory::MemoryBuffer::writeString().

◆ split()

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

◆ splitAll()

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

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

References find_first_of(), and Cogs::Collections::SmallVectorImplBase< T, isPod >::push_back().

◆ substr()

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

Get the given sub string.

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

References str, and strSize.

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

◆ trim()

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

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

References trimEnd().

◆ trimEnd()

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

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

Referenced by trim().

◆ trimStart()

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

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

Member Data Documentation

◆ NoPosition

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

No position.

◆ npos

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

No position.

◆ str

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

Pointer to the string this view is over.

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

◆ strSize

size_t Cogs::StringView::strSize = 0
private

Length of the string data pointed to.

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


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