ToyGine2 26.2.0
Game Engine for retro consoles
Loading...
Searching...
No Matches
toy::CStringView Class Reference

Non-owning string view class for C-style strings. More...

Public Member Functions

constexpr CStringView () noexcept
 Default constructor.
constexpr CStringView (const CStringView &string) noexcept=default
 Constructs a copy of string view.
constexpr explicit (false) CStringView(const char *string) noexcept
 Constructs a string view from a C string.
constexpr ~CStringView () noexcept=default
 Destructor for the string view.
constexpr CStringViewoperator= (const CStringView &string) noexcept=default
 Copy assigns other string view to this string view.
constexpr CStringViewoperator= (const char *string) noexcept
 Copy assigns the C string to this string view.
constexpr CStringViewassign (const CStringView &string) noexcept
 Copy assigns other string view to this string view.
constexpr CStringViewassign (const char *string) noexcept
 Copy assigns the C string to this string view.
constexpr const char & at (size_t offset) const noexcept
 Access a character in the string view at a given offset.
constexpr const char & operator[] (size_t offset) const noexcept
 Access a character in the string view at a given offset.
constexpr const char & front () const noexcept
 Returns a const reference to the first character of the string view.
constexpr const char & back () const noexcept
 Returns a const reference to the last character of the string view.
constexpr const char * data () const noexcept
 Returns a constant pointer to the data of the string view.
constexpr const char * c_str () const noexcept
 Returns a constant pointer to the C string representation of this string view.
constexpr bool empty () const noexcept
 Checks if the string view is empty.
constexpr size_t size () const noexcept
 Returns the size of the string view.
size_t utf8_size () const noexcept
 Returns the size of the Unicode string in UTF-8 encoding.
constexpr size_t length () const noexcept
 Returns the size of the string view.
constexpr size_t max_size () const noexcept
 Returns the maximum observable size of this string view.
constexpr size_t capacity () const noexcept
 Returns the capacity of the string view.
constexpr void clear () noexcept
 Clears the contents by detaching from the current C string.
constexpr void swap (CStringView &string) noexcept
 Swaps the contents of this string view with another string view.
template<StringLike stringType>
constexpr size_t find (const stringType &string, size_t position=0) const noexcept
 Finds the first occurrence of a toy::StringLike object in the string view.
constexpr size_t find (const char *string, size_t position=0) const noexcept
 Finds the first occurrence of the C string in the string view.
constexpr size_t find (char character, size_t position=0) const noexcept
 Finds the first occurrence of a character in the string view.
template<StringLike stringType>
constexpr size_t rfind (const stringType &string, size_t position=npos) const noexcept
 Finds the last occurrence of a toy::StringLike object in the string view.
constexpr size_t rfind (const char *string, size_t position=npos) const noexcept
 Finds the last occurrence of the C string in the string view.
constexpr size_t rfind (char character, size_t position=npos) const noexcept
 Finds the last occurrence of a character in the string view.
template<StringLike stringType>
constexpr size_t find_first_of (const stringType &string, size_t position=0) const noexcept
 Finds the first occurrence of any character from a toy::StringLike object.
constexpr size_t find_first_of (const char *string, size_t position=0) const noexcept
 Finds the first occurrence of any character from the C string.
constexpr size_t find_first_of (char character, size_t position=0) const noexcept
 Finds the first occurrence of the specified character.
template<StringLike stringType>
constexpr size_t find_first_not_of (const stringType &string, size_t position=0) const noexcept
 Finds the first occurrence of any character not from a toy::StringLike object.
constexpr size_t find_first_not_of (const char *string, size_t position=0) const noexcept
 Finds the first occurrence of any character not from the C string.
constexpr size_t find_first_not_of (char character, size_t position=0) const noexcept
 Finds the first occurrence of any character not equal to the specified character.
template<StringLike stringType>
constexpr size_t find_last_of (const stringType &string, size_t position=npos) const noexcept
 Finds the last occurrence of any character from a toy::StringLike object.
constexpr size_t find_last_of (const char *string, size_t position=npos) const noexcept
 Finds the last occurrence of any character from the C string.
constexpr size_t find_last_of (char character, size_t position=npos) const noexcept
 Finds the last occurrence of the specified character.
template<StringLike stringType>
constexpr size_t find_last_not_of (const stringType &string, size_t position=npos) const noexcept
 Finds the last occurrence of any character not from a toy::StringLike object.
constexpr size_t find_last_not_of (const char *string, size_t position=npos) const noexcept
 Finds the last occurrence of any character not from the C string.
constexpr size_t find_last_not_of (char character, size_t position=npos) const noexcept
 Finds the last occurrence of any character not equal to the specified character.
template<StringLike stringType>
constexpr int compare (const stringType &string) const noexcept
 Compares this string view with a toy::StringLike object lexicographically.
constexpr int compare (const char *string) const noexcept
 Compares this string view with the C string lexicographically.
template<StringLike stringType>
constexpr bool starts_with (const stringType &string) const noexcept
 Checks if the string view starts with a toy::StringLike object.
constexpr bool starts_with (const char *string) const noexcept
 Checks if the string view starts with the specified C string.
constexpr bool starts_with (char character) const noexcept
 Checks if the string view starts with the specified character.
template<StringLike stringType>
constexpr bool ends_with (const stringType &string) const noexcept
 Checks if the string view ends with a toy::StringLike object.
constexpr bool ends_with (const char *string) const noexcept
 Checks if the string view ends with the C string.
constexpr bool ends_with (char character) const noexcept
 Checks if the string view ends with the specified character.
template<StringLike stringType>
constexpr bool contains (const stringType &string) const noexcept
 Checks if the string view contains a toy::StringLike object.
constexpr bool contains (const char *string) const noexcept
 Checks if the string view contains the C string.
constexpr bool contains (char character) const noexcept
 Checks if the string view contains the specified character.

Static Public Attributes

static constexpr size_t npos = size_t(-1)
 The special value, its exact meaning depends on the context.

Private Member Functions

constexpr size_t _find_raw (size_t position, const char *data, size_t dataSize) const noexcept
 Helper method for finding data in the string view.
constexpr size_t _rfind_raw (size_t position, const char *data, size_t dataSize) const noexcept
 Helper method for finding data in the string view backwards.
constexpr size_t _find_first_of_raw (size_t position, const char *data, size_t dataSize) const noexcept
 Helper method for finding the first occurrence of any character from data.
constexpr size_t _find_first_not_of_raw (size_t position, const char *data, size_t dataSize) const noexcept
 Helper method for finding the first occurrence of any character not from data.
constexpr size_t _find_last_of_raw (size_t position, const char *data, size_t dataSize) const noexcept
 Helper method for finding the last occurrence of any character from data.
constexpr size_t _find_last_not_of_raw (size_t position, const char *data, size_t dataSize) const noexcept
 Helper method for finding the last occurrence of any character not from data.

Private Attributes

const char * _data
 Pointer to the wrapped C string.

Static Private Attributes

static constexpr char _emptyString [] {'\0'}
 Static empty C string used as default value for null pointers.

Friends

constexpr bool operator== (const CStringView &lhs, const CStringView &rhs)
 Equality comparison operator for two CStringView objects.
template<StringLike stringType>
constexpr bool operator== (const CStringView &lhs, const stringType &rhs)
 Equality comparison operator for CStringView and toy::StringLike object.
template<StringLike stringType>
constexpr bool operator== (const stringType &lhs, const CStringView &rhs)
 Equality comparison operator for toy::StringLike object and CStringView.
constexpr bool operator== (const CStringView &lhs, const char *rhs)
 Equality comparison operator for CStringView and C string.
constexpr bool operator== (const char *lhs, const CStringView &rhs)
 Equality comparison operator for C string and CStringView.
constexpr strong_ordering operator<=> (const CStringView &lhs, const CStringView &rhs)
 Three-way comparison operator for two CStringView objects.
template<StringLike stringType>
constexpr strong_ordering operator<=> (const CStringView &lhs, const stringType &rhs)
 Three-way comparison operator for CStringView and toy::StringLike object.
template<StringLike stringType>
constexpr strong_ordering operator<=> (const stringType &lhs, const CStringView &rhs)
 Three-way comparison operator for toy::StringLike object and CStringView.
constexpr strong_ordering operator<=> (const CStringView &lhs, const char *rhs)
 Three-way comparison operator for CStringView and C string.
constexpr strong_ordering operator<=> (const char *lhs, const CStringView &rhs)
 Three-way comparison operator for C string and CStringView.

Detailed Description

Non-owning string view class for C-style strings.

CStringView is a lightweight, non-owning wrapper around C-style strings that provides a safe and efficient way to work with null-terminated character arrays. It offers a std::string_view-like interface while being specifically designed for constexpr contexts and compile-time string manipulation.

Unlike std::string_view, CStringView is optimized for constexpr usage and provides additional functionality for compile-time string operations. It does not own the underlying string data and simply holds a pointer to it, making it extremely lightweight and efficient.

Key Features

  • ⚙️ Zero Allocation: No dynamic memory allocation, only holds a pointer
  • 🔧 ConstExpr Support: Most operations can be evaluated at compile time
  • 🛡️ Exception Safety: All operations are noexcept
  • 🔗 STL Compatibility: Provides std::string_view / std::string like interface
  • 🌍 UTF-8 Support: Built-in UTF-8 character counting and manipulation
  • 🧬 Type Safety: Uses C++20 concepts for type safety
  • 📏 Non-owning: Does not manage memory, safe for temporary strings

Usage Example

// Create a string view from a C string
toy::CStringView str("Hello, World!");
// Use in constexpr context
constexpr auto greeting = toy::CStringView("Hello, World!");
constexpr auto world = toy::CStringView("World");
constexpr auto position = greeting.find(world);
// Safe string operations
if (str.starts_with("Hello")) {
// Process the string
}
Non-owning view over null-terminated C strings.
Non-owning string view class for C-style strings.
Definition c_string_view.hpp:109

Performance Characteristics

  • ⚙️ Construction: O(1) - just stores a pointer
  • 📝 Assignment: O(1) - just copies a pointer
  • 📏 Size/Length: O(n) - requires traversing the string to find the null terminator
  • 🔍 Search Operations: O(n*m) where n is the string length and m is the pattern length
  • 💾 Memory Usage: Minimal - only stores a single pointer
  • Cache Performance: Excellent due to pointer-only storage
  • 📋 Copy Performance: Extremely fast - just pointer copy
  • 🎯 String Operations: Optimized for common cases

Safety Guarantees

  • 🛡️ Contracts & Debug Checks: Bounds are asserted in debug; in production, violating preconditions is UB
  • 🔒 Null Pointer Safety: All C-string operations validate pointers via assertions in debug
  • 📐 Type Safety: Concepts prevent invalid usage
  • ⚠️ Exception Safety: All operations are noexcept, no exceptions thrown
  • Lifetime Safety: User must ensure underlying string remains valid

Compatibility

  • 🆕 C++23: Requires C++23 or later for full functionality
  • 🔗 STL Integration: Compatible with STL algorithms and containers
  • 🌐 Cross-Platform: Works on all platforms supported by the compiler
  • 🔧 Embedded Systems: Suitable for resource-constrained environments
Warning
The underlying C string must remain valid for the lifetime of the CStringView object. CStringView does not own the string data and will not manage its lifetime.
See also
std::string_view, toy::StringLike, toy::FixedString

Constructor & Destructor Documentation

◆ CStringView() [1/2]

toy::CStringView::CStringView ( )
constexprnoexcept

Default constructor.

Creates an empty string view that points to an empty string.

Postcondition
The string view is empty and ready for use.

◆ CStringView() [2/2]

toy::CStringView::CStringView ( const CStringView & string)
constexprdefaultnoexcept

Constructs a copy of string view.

This constructor initializes a string view by copying the C string from another string view.

Parameters
stringThe source string view to copy C string from.
Precondition
The source string view must be valid and properly initialized.

◆ ~CStringView()

toy::CStringView::~CStringView ( )
constexprdefaultnoexcept

Destructor for the string view.

Note
Since the CStringView does not manage dynamic memory, no special cleanup is required.

Member Function Documentation

◆ _find_first_not_of_raw()

size_t toy::CStringView::_find_first_not_of_raw ( size_t position,
const char * data,
size_t dataSize ) const
constexprprivatenoexcept

Helper method for finding the first occurrence of any character not from data.

This private method performs the common search logic used by all find_first_not_of methods. It searches for the first occurrence of any character that is not present in the specified data starting from the given position.

Parameters
positionThe position to start searching from.
dataThe data containing characters to exclude from search.
dataSizeThe size of the data containing characters to exclude from search.
Returns
The position of the first occurrence of any character not from data, or npos if not found.
Precondition
The position must be less than the string view size.
The data must not be null.

◆ _find_first_of_raw()

size_t toy::CStringView::_find_first_of_raw ( size_t position,
const char * data,
size_t dataSize ) const
constexprprivatenoexcept

Helper method for finding the first occurrence of any character from data.

This private method performs the common search logic used by all find_first_of methods. It searches for the first occurrence of any character from the specified data starting from the given position.

Parameters
positionThe position to start searching from.
dataThe data containing characters to search for.
dataSizeThe size of the data containing characters to search for.
Returns
The position of the first occurrence of any character from data, or npos if not found.
Precondition
The position must be less than the string view size.
The data must not be null.

◆ _find_last_not_of_raw()

size_t toy::CStringView::_find_last_not_of_raw ( size_t position,
const char * data,
size_t dataSize ) const
constexprprivatenoexcept

Helper method for finding the last occurrence of any character not from data.

This private method performs the common reverse search logic used by all find_last_not_of methods. It searches for the last occurrence of any character that is not present in the specified data starting from the given position and searching backwards.

Parameters
positionThe position to start searching from (default: npos). If npos, searches from the end.
dataThe data containing characters to exclude from search.
dataSizeThe size of the data containing characters to exclude from search.
Returns
The position of the last occurrence of any character not from data, or npos if not found.
Precondition
If position is not npos, it must be less than the string view size.
The data must not be null.

◆ _find_last_of_raw()

size_t toy::CStringView::_find_last_of_raw ( size_t position,
const char * data,
size_t dataSize ) const
constexprprivatenoexcept

Helper method for finding the last occurrence of any character from data.

This private method performs the common reverse search logic used by all find_last_of methods. It searches for the last occurrence of any character from the specified data starting from the given position and searching backwards.

Parameters
positionThe position to start searching from (default: npos). If npos, searches from the end.
dataThe data containing characters to search for.
dataSizeThe size of the data containing characters to search for.
Returns
The position of the last occurrence of any character from data, or npos if not found.
Precondition
If position is not npos, it must be less than the string view size.
The data must not be null.

◆ _find_raw()

size_t toy::CStringView::_find_raw ( size_t position,
const char * data,
size_t dataSize ) const
constexprprivatenoexcept

Helper method for finding data in the string view.

This private method performs the common search logic used by all find methods. It searches for the specified data starting from the given position.

Parameters
positionThe position to start searching from.
dataThe data to search for.
dataSizeThe size of the data to search for.
Returns
The position of the first occurrence of data, or npos if not found.
Precondition
The position must be less than the string view size.
The data must not be null.

◆ _rfind_raw()

size_t toy::CStringView::_rfind_raw ( size_t position,
const char * data,
size_t dataSize ) const
constexprprivatenoexcept

Helper method for finding data in the string view backwards.

This private method performs the common reverse search logic used by all rfind methods. It searches for the specified data starting from the given position and searching backwards.

Parameters
positionThe position to start searching from.
dataThe data to search for.
dataSizeThe size of the data to search for.
Returns
The position of the last occurrence of data, or npos if not found.
Precondition
If position is not npos, it must be less than or equal to the string view size.
The data must not be null.

◆ assign() [1/2]

CStringView & toy::CStringView::assign ( const char * string)
constexprnoexcept

Copy assigns the C string to this string view.

This method assigns a new C string to this string view.

Parameters
stringThe C string to assign.
Returns
A reference to this string view after assignment.
Precondition
The source C string must not be null.
Note
This method is equivalent to the copy assignment operator.
See also
operator=(const char * string)

◆ assign() [2/2]

CStringView & toy::CStringView::assign ( const CStringView & string)
constexprnoexcept

Copy assigns other string view to this string view.

This method assigns the C string of another string view to this string view.

Parameters
stringThe source string view to copy C string from.
Returns
A reference to this string view after assignment.
Note
This method is equivalent to the copy assignment operator.
See also
operator=(const CStringView & string)

◆ at()

const char & toy::CStringView::at ( size_t offset) const
nodiscardconstexprnoexcept

Access a character in the string view at a given offset.

This method provides read-only access to a character at the specified offset within the string view.

Parameters
offsetThe offset of the character to access in the string view.
Returns
A const reference to the character at the specified offset.
Precondition
The offset must be less than the current string view size.
Note
The returned reference is read-only and cannot modify the character.
This method is equivalent to the subscript operator.
See also
operator[](size_t offset) const

◆ back()

const char & toy::CStringView::back ( ) const
nodiscardconstexprnoexcept

Returns a const reference to the last character of the string view.

This method provides read-only access to the last character of the string view.

Returns
A const reference to the last character of the string view.
Precondition
The string view must not be empty.
Note
The returned reference is read-only and cannot modify the character.

◆ c_str()

const char * toy::CStringView::c_str ( ) const
nodiscardconstexprnoexcept

Returns a constant pointer to the C string representation of this string view.

This method returns a constant pointer to the C string representation of this string view. The returned pointer provides read-only access to the string view contents and can be used with C functions that require a char pointer.

Returns
A constant pointer to the C string representation of this string view.
Note
The returned pointer points to a C string.
The returned pointer is read-only and cannot modify the string view contents.
This method is equivalent to data().
See also
data()

◆ capacity()

size_t toy::CStringView::capacity ( ) const
nodiscardconstexprnoexcept

Returns the capacity of the string view.

This method returns the capacity of the allocated buffer for the string view.

Returns
The capacity of the string view in characters, excluding the terminating null character.
Note
CStringView is non-owning and has no capacity; this is an alias of size().

◆ clear()

void toy::CStringView::clear ( )
constexprnoexcept

Clears the contents by detaching from the current C string.

This method resets this view to point at the internal empty sentinel. The previously referenced character buffer is not modified or freed.

Postcondition
The string view is empty (size is 0).
The string view points to an empty sentinel string.
Note
No allocation or deallocation occurs.

◆ compare() [1/2]

int toy::CStringView::compare ( const char * string) const
nodiscardconstexprnoexcept

Compares this string view with the C string lexicographically.

This method performs a lexicographic comparison between this string view and the C string. The comparison is performed character by character using the character's numeric value.

Parameters
stringThe C string to compare with this string.
Returns
A negative value if this string view is lexicographically less than C string, zero if they are equal, or a positive value if this string view is lexicographically greater than C string.
Precondition
The C string must not be null.
Note
The comparison is case-sensitive.
The comparison stops at the first character that differs between the strings.
If one string is a prefix of another, the shorter is considered lexicographically smaller.

◆ compare() [2/2]

template<StringLike stringType>
int toy::CStringView::compare ( const stringType & string) const
nodiscardconstexprnoexcept

Compares this string view with a toy::StringLike object lexicographically.

This method performs a lexicographic comparison between this string view and a toy::StringLike object. The comparison is performed character by character using the character's numeric value.

Template Parameters
stringTypeThe type of the source string. Must satisfy the toy::StringLike concept.
Parameters
stringThe toy::StringLike object to compare with this string.
Returns
A negative value if this string view is lexicographically less than a toy::StringLike object, zero if they are equal, or a positive value if this string view is lexicographically greater than a toy::StringLike object.
Note
The comparison is case-sensitive.
The comparison stops at the first character that differs between the strings.
If one string is a prefix of another, the shorter is considered lexicographically smaller.

◆ contains() [1/3]

bool toy::CStringView::contains ( char character) const
nodiscardconstexprnoexcept

Checks if the string view contains the specified character.

This method checks if the current string view contains the specified character anywhere within it.

Parameters
characterThe character to search for within this string view.
Returns
true if this string view contains the specified character, false otherwise.
Note
The search is case-sensitive.
If the string view is empty, this method returns false.

◆ contains() [2/3]

bool toy::CStringView::contains ( const char * string) const
nodiscardconstexprnoexcept

Checks if the string view contains the C string.

This method checks if the current string view contains the C string anywhere within it.

Parameters
stringThe C string to search for within this string.
Returns
true if this string view contains the C string, false otherwise.
Precondition
The C string must not be null.
Note
The search is case-sensitive.
If the C string is empty, this method returns true.
If the C string is longer than this string view, returns false.

◆ contains() [3/3]

template<StringLike stringType>
bool toy::CStringView::contains ( const stringType & string) const
nodiscardconstexprnoexcept

Checks if the string view contains a toy::StringLike object.

This method checks if the current string view contains a toy::StringLike object anywhere within it.

Template Parameters
stringTypeThe type of the source string. Must satisfy the toy::StringLike concept.
Parameters
stringThe toy::StringLike object to search for within this string.
Returns
true if this string view contains a toy::StringLike object, false otherwise.
Note
The search is case-sensitive.
If a toy::StringLike object is empty, this method returns true.
If a toy::StringLike object is longer than this string view, returns false.

◆ data()

const char * toy::CStringView::data ( ) const
nodiscardconstexprnoexcept

Returns a constant pointer to the data of the string view.

This method returns a constant pointer to the internal character array that stores the string view data. The returned pointer provides read-only access to the string view contents and can be used for low-level operations.

Returns
A constant pointer to the internal character array.
Note
The returned pointer points to a null-terminated character array.
The returned pointer is read-only and cannot modify the string view contents.

◆ empty()

bool toy::CStringView::empty ( ) const
nodiscardconstexprnoexcept

Checks if the string view is empty.

This method checks if the string view is empty, i.e. its size is zero. An empty string view contains no characters and has a length of zero.

Returns
true if the string view is empty, false otherwise.
Note
An empty string view has size zero.
An empty string view still contains a null terminator.
This method is equivalent to the expression: size() == 0.
See also
size()

◆ ends_with() [1/3]

bool toy::CStringView::ends_with ( char character) const
nodiscardconstexprnoexcept

Checks if the string view ends with the specified character.

This method checks if the current string view ends with the specified character. The comparison is performed on the last character of the string view.

Parameters
characterThe character to check if this string view ends with.
Returns
true if this string view ends with the specified character, false otherwise.
Note
The comparison is case-sensitive.
If the string view is empty, this method returns false.

◆ ends_with() [2/3]

bool toy::CStringView::ends_with ( const char * string) const
nodiscardconstexprnoexcept

Checks if the string view ends with the C string.

This method checks if the current string view ends with the C string. The comparison is performed character by character from the end of the string view.

Parameters
stringThe C string to check if this string view ends with.
Returns
true if this string view ends with the C string, false otherwise.
Precondition
The C string must not be null.
Note
The comparison is case-sensitive.
If the C string is empty, this method returns true.
If the C string is longer than this string view, returns false.

◆ ends_with() [3/3]

template<StringLike stringType>
bool toy::CStringView::ends_with ( const stringType & string) const
nodiscardconstexprnoexcept

Checks if the string view ends with a toy::StringLike object.

This method checks if the current string view ends with a toy::StringLike object. The comparison is performed character by character from the end of the string view.

Template Parameters
stringTypeThe type of the source string. Must satisfy the toy::StringLike concept.
Parameters
stringThe toy::StringLike object to check if this string view ends with.
Returns
true if this string view ends with a toy::StringLike object, false otherwise.
Note
The comparison is case-sensitive.
If a toy::StringLike object is empty, this method returns true.
If a toy::StringLike object is longer than this string view, returns false.

◆ explicit()

toy::CStringView::explicit ( false ) const
constexprnoexcept

Constructs a string view from a C string.

Creates a new string view that wraps the provided C string pointer.

Parameters
stringThe C string to wrap.
Precondition
The source C string must not be null.

◆ find() [1/3]

size_t toy::CStringView::find ( char character,
size_t position = 0 ) const
nodiscardconstexprnoexcept

Finds the first occurrence of a character in the string view.

This method searches for the first occurrence of the specified character within this string view, starting from the given position.

Parameters
characterThe character to search for.
positionThe position to start searching from (default: 0).
Returns
The position of the first occurrence of the character, or npos if not found.
Precondition
The position must be less than the string view size.
Note
The search is case-sensitive.

◆ find() [2/3]

size_t toy::CStringView::find ( const char * string,
size_t position = 0 ) const
nodiscardconstexprnoexcept

Finds the first occurrence of the C string in the string view.

This method searches for the first occurrence of the C string within this string view, starting from the given position.

Parameters
stringThe source C string to search for.
positionThe position to start searching from (default: 0).
Returns
The position of the first occurrence of the C string, or npos if not found.
Precondition
The position must be less than the string view size.
The string must not be null.
Note
The search is case-sensitive.

◆ find() [3/3]

template<StringLike stringType>
size_t toy::CStringView::find ( const stringType & string,
size_t position = 0 ) const
nodiscardconstexprnoexcept

Finds the first occurrence of a toy::StringLike object in the string view.

This method searches for the first occurrence of a toy::StringLike object within this string view, starting from the given position.

Template Parameters
stringTypeThe type of the source string. Must satisfy the toy::StringLike concept.
Parameters
stringThe source toy::StringLike object to search for.
positionThe position to start searching from (default: 0).
Returns
The position of the first occurrence of a toy::StringLike object, or npos if not found.
Precondition
The position must be less than the string view size.
Note
The search is case-sensitive.

◆ find_first_not_of() [1/3]

size_t toy::CStringView::find_first_not_of ( char character,
size_t position = 0 ) const
nodiscardconstexprnoexcept

Finds the first occurrence of any character not equal to the specified character.

This method searches for the first occurrence of any character that is not equal to the specified character within this string view, starting from the given position.

Parameters
characterThe character to exclude from search.
positionThe position to start searching from (default: 0).
Returns
The position of the first occurrence of any character not equal to character, or npos if not found.
Precondition
The position must be less than the string view size.
Note
The search is case-sensitive.

◆ find_first_not_of() [2/3]

size_t toy::CStringView::find_first_not_of ( const char * string,
size_t position = 0 ) const
nodiscardconstexprnoexcept

Finds the first occurrence of any character not from the C string.

This method searches for the first occurrence of any character that is not present in the C string within this string view, starting from the given position.

Parameters
stringThe C string containing characters to exclude from search.
positionThe position to start searching from (default: 0).
Returns
The position of the first occurrence of any character not from the C string, or npos if not found.
Precondition
The position must be less than the string size.
The string must not be null.
Note
The search is case-sensitive.
If the C string is empty, returns position if within bounds, otherwise returns npos.

◆ find_first_not_of() [3/3]

template<StringLike stringType>
size_t toy::CStringView::find_first_not_of ( const stringType & string,
size_t position = 0 ) const
nodiscardconstexprnoexcept

Finds the first occurrence of any character not from a toy::StringLike object.

This method searches for the first occurrence of any character that is not present in a toy::StringLike object within this string view, starting from the given position.

Template Parameters
stringTypeThe type of the source string. Must satisfy the toy::StringLike concept.
Parameters
stringThe toy::StringLike object containing characters to exclude from search.
positionThe position to start searching from (default: 0).
Returns
The position of the first occurrence of any character not from a toy::StringLike object, or npos if not found.
Precondition
The position must be less than the string view size.
Note
The search is case-sensitive.
If a toy::StringLike object is empty, returns position if within bounds, otherwise returns npos.

◆ find_first_of() [1/3]

size_t toy::CStringView::find_first_of ( char character,
size_t position = 0 ) const
nodiscardconstexprnoexcept

Finds the first occurrence of the specified character.

This method searches for the first occurrence of the specified character within this string view, starting from the given position.

Parameters
characterThe character to search for.
positionThe position to start searching from (default: 0).
Returns
The position of the first occurrence of the character, or npos if not found.
Precondition
The position must be less than the string view size.
Note
The search is case-sensitive.
This method is equivalent to find(char, size_t) const.
See also
find(char, size_t) const

◆ find_first_of() [2/3]

size_t toy::CStringView::find_first_of ( const char * string,
size_t position = 0 ) const
nodiscardconstexprnoexcept

Finds the first occurrence of any character from the C string.

This method searches for the first occurrence of any character from the C string within this string view, starting from the given position.

Parameters
stringThe C string containing characters to search for.
positionThe position to start searching from (default: 0).
Returns
The position of the first occurrence of any character from the C string, or npos if not found.
Precondition
The position must be less than the string view size.
The string must not be null.
Note
The search is case-sensitive.
If the C string is empty, this method returns npos.

◆ find_first_of() [3/3]

template<StringLike stringType>
size_t toy::CStringView::find_first_of ( const stringType & string,
size_t position = 0 ) const
nodiscardconstexprnoexcept

Finds the first occurrence of any character from a toy::StringLike object.

This method searches for the first occurrence of any character from a toy::StringLike object within this string view, starting from the given position.

Template Parameters
stringTypeThe type of the source string. Must satisfy the toy::StringLike concept.
Parameters
stringThe toy::StringLike object containing characters to search for.
positionThe position to start searching from (default: 0).
Returns
The position of the first occurrence of any character from a toy::StringLike object, or npos if not found.
Precondition
The position must be less than the string view size.
Note
The search is case-sensitive.
If a toy::StringLike object is empty, this method returns npos.

◆ find_last_not_of() [1/3]

size_t toy::CStringView::find_last_not_of ( char character,
size_t position = npos ) const
nodiscardconstexprnoexcept

Finds the last occurrence of any character not equal to the specified character.

This method searches for the last occurrence of any character that is not equal to the specified character within this string view, starting from the given position and searching backwards.

Parameters
characterThe character to exclude from search.
positionThe position to start searching from (default: npos). If npos, searches from the end.
Returns
The position of the last occurrence of any character not equal to character, or npos if not found.
Precondition
If position is not npos, it must be less than the string view size.
Note
The search is case-sensitive.

◆ find_last_not_of() [2/3]

size_t toy::CStringView::find_last_not_of ( const char * string,
size_t position = npos ) const
nodiscardconstexprnoexcept

Finds the last occurrence of any character not from the C string.

This method searches for the last occurrence of any character that is not present in the C string within this string view, starting from the given position and searching backwards.

Parameters
stringThe C string containing characters to exclude from search.
positionThe position to start searching from (default: npos). If npos, searches from the end.
Returns
The position of the last occurrence of any character not from the C string, or npos if not found.
Precondition
If position is not npos, it must be less than the string view size.
The string must not be null.
Note
The search is case-sensitive.
If the C string is empty, returns position if within bounds, otherwise returns npos.

◆ find_last_not_of() [3/3]

template<StringLike stringType>
size_t toy::CStringView::find_last_not_of ( const stringType & string,
size_t position = npos ) const
nodiscardconstexprnoexcept

Finds the last occurrence of any character not from a toy::StringLike object.

This method searches for the last occurrence of any character that is not present in a toy::StringLike object within this string view, starting from the given position and searching backwards.

Template Parameters
stringTypeThe type of the source string. Must satisfy the toy::StringLike concept.
Parameters
stringThe toy::StringLike object containing characters to exclude from search.
positionThe position to start searching from (default: npos). If npos, searches from the end.
Returns
The position of the last occurrence of any character not from a toy::StringLike object, or npos if not found.
Precondition
If position is not npos, it must be less than the string view size.
Note
The search is case-sensitive.
If a toy::StringLike object is empty, returns position if within bounds, otherwise returns npos.

◆ find_last_of() [1/3]

size_t toy::CStringView::find_last_of ( char character,
size_t position = npos ) const
nodiscardconstexprnoexcept

Finds the last occurrence of the specified character.

This method searches for the last occurrence of the specified character within this string view, starting from the given position and searching backwards.

Parameters
characterThe character to search for.
positionThe position to start searching from (default: npos). If npos, searches from the end.
Returns
The position of the last occurrence of the character, or npos if not found.
Precondition
If position is not npos, it must be less than the string view size.
Note
The search is case-sensitive.
This method is equivalent to rfind(char, size_t) const.
See also
rfind(char, size_t) const

◆ find_last_of() [2/3]

size_t toy::CStringView::find_last_of ( const char * string,
size_t position = npos ) const
nodiscardconstexprnoexcept

Finds the last occurrence of any character from the C string.

This method searches for the last occurrence of any character from the C string within this string view, starting from the given position and searching backwards.

Parameters
stringThe C string containing characters to search for.
positionThe position to start searching from (default: npos). If npos, searches from the end.
Returns
The position of the last occurrence of any character from the C string, or npos if not found.
Precondition
If position is not npos, it must be less than the string view size.
The string must not be null.
Note
The search is case-sensitive.
If the C string is empty, this method returns npos.

◆ find_last_of() [3/3]

template<StringLike stringType>
size_t toy::CStringView::find_last_of ( const stringType & string,
size_t position = npos ) const
nodiscardconstexprnoexcept

Finds the last occurrence of any character from a toy::StringLike object.

This method searches for the last occurrence of any character from a toy::StringLike object within this string view, starting from the given position and searching backwards.

Template Parameters
stringTypeThe type of the source string. Must satisfy the toy::StringLike concept.
Parameters
stringThe toy::StringLike object containing characters to search for.
positionThe position to start searching from (default: npos). If npos, searches from the end.
Returns
The position of the last occurrence of any character from a toy::StringLike object, or npos if not found.
Precondition
If position is not npos, it must be less than the string view size.
Note
The search is case-sensitive.
If a toy::StringLike object is empty, this method returns npos.

◆ front()

const char & toy::CStringView::front ( ) const
nodiscardconstexprnoexcept

Returns a const reference to the first character of the string view.

This method provides read-only access to the first character of the string view.

Returns
A const reference to the first character of the string view.
Precondition
The string view must not be empty.
Note
The returned reference is read-only and cannot modify the character.

◆ length()

size_t toy::CStringView::length ( ) const
nodiscardconstexprnoexcept

Returns the size of the string view.

This method returns the current number of characters in the string view, excluding the terminating null character. The length represents the actual length of the string view content.

Returns
The number of characters in the string view, excluding the terminating null character.
Note
This method is equivalent to size().
See also
size()

◆ max_size()

size_t toy::CStringView::max_size ( ) const
nodiscardconstexprnoexcept

Returns the maximum observable size of this string view.

This method returns the maximum possible size for the string view. The maximum size represents the size of the allocated buffer.

Returns
The maximum number of characters in the string view, excluding the terminating null character.
Note
CStringView is non-owning and has no capacity; this is an alias of size().

◆ operator=() [1/2]

CStringView & toy::CStringView::operator= ( const char * string)
constexprnoexcept

Copy assigns the C string to this string view.

This operator assigns a new C string to this string view.

Parameters
stringThe C string to assign.
Returns
A reference to this string view after assignment.
Precondition
The source C string must not be null.

◆ operator=() [2/2]

CStringView & toy::CStringView::operator= ( const CStringView & string)
constexprdefaultnoexcept

Copy assigns other string view to this string view.

This operator assigns the C string of another string view to this string view.

Parameters
stringThe source string view to copy C string from.
Returns
A reference to this string view after assignment.

◆ operator[]()

const char & toy::CStringView::operator[] ( size_t offset) const
nodiscardconstexprnoexcept

Access a character in the string view at a given offset.

This operator provides read-only access to a character at the specified offset within the string view.

Parameters
offsetThe offset of the character to access in the string view.
Returns
A const reference to the character at the specified offset.
Precondition
The offset must be less than the current string view size.
Note
The returned reference is read-only and cannot modify the character.

◆ rfind() [1/3]

size_t toy::CStringView::rfind ( char character,
size_t position = npos ) const
nodiscardconstexprnoexcept

Finds the last occurrence of a character in the string view.

This method searches for the last occurrence of the specified character within this string view, starting from the given position and searching backwards.

Parameters
characterThe character to search for.
positionThe position to start searching from (default: npos). If npos, searches from the end.
Returns
The position of the last occurrence of the character, or npos if not found.
Precondition
The position must be less than the string view size.
Note
The search is case-sensitive.

◆ rfind() [2/3]

size_t toy::CStringView::rfind ( const char * string,
size_t position = npos ) const
nodiscardconstexprnoexcept

Finds the last occurrence of the C string in the string view.

This method searches for the last occurrence of the C string within this string view, starting from the given position and searching backwards.

Parameters
stringThe source C string to search for.
positionThe position to start searching from (default: npos). If npos, searches from the end.
Returns
The position of the last occurrence of the C string, or npos if not found.
Precondition
If position is not npos, it must be less than or equal to the maximum valid start index: the string view size minus the size of the C string.
The string must not be null.
Note
The search is case-sensitive.
If the C string is empty, returns position if within bounds, otherwise returns the string view size.

◆ rfind() [3/3]

template<StringLike stringType>
size_t toy::CStringView::rfind ( const stringType & string,
size_t position = npos ) const
nodiscardconstexprnoexcept

Finds the last occurrence of a toy::StringLike object in the string view.

This method searches for the last occurrence of a toy::StringLike object within this string view, starting from the given position and searching backwards.

Template Parameters
stringTypeThe type of the source string. Must satisfy the toy::StringLike concept.
Parameters
stringThe source toy::StringLike object to search for.
positionThe position to start searching from (default: npos). If npos, searches from the end.
Returns
The position of the last occurrence of a toy::StringLike object, or npos if not found.
Precondition
If position is not npos, it must be less than or equal to the maximum valid start index: the string view size minus the size of a toy::StringLike object.
Note
The search is case-sensitive.
If a toy::StringLike object is empty, returns position if within bounds, otherwise returns the string view size.

◆ size()

size_t toy::CStringView::size ( ) const
nodiscardconstexprnoexcept

Returns the size of the string view.

This method returns the current number of characters in the string view, excluding the terminating null character. The size represents the actual length of the string view content.

Returns
The number of characters in the string view, excluding the terminating null character.
Note
This method is equivalent to length().
See also
length()

◆ starts_with() [1/3]

bool toy::CStringView::starts_with ( char character) const
nodiscardconstexprnoexcept

Checks if the string view starts with the specified character.

This method checks if the current string view starts with the specified character. The comparison is performed on the first character of the string view.

Parameters
characterThe character to check if this string view starts with.
Returns
true if this string view starts with the specified character, false otherwise.
Note
The comparison is case-sensitive.
If the string view is empty, this method returns false.

◆ starts_with() [2/3]

bool toy::CStringView::starts_with ( const char * string) const
nodiscardconstexprnoexcept

Checks if the string view starts with the specified C string.

This method checks if the current string view starts with the C string. The comparison is performed character by character from the beginning of the string view.

Parameters
stringThe C string to check if this string starts with.
Returns
true if this string view starts with the C string, false otherwise.
Precondition
The C string must not be null.
Note
The comparison is case-sensitive.
If the C string is empty, this method returns true.
If the C string is longer than this string view, returns false.

◆ starts_with() [3/3]

template<StringLike stringType>
bool toy::CStringView::starts_with ( const stringType & string) const
nodiscardconstexprnoexcept

Checks if the string view starts with a toy::StringLike object.

This method checks if the current string view starts with a toy::StringLike object. The comparison is performed character by character from the beginning of the string view.

Template Parameters
stringTypeThe type of the source string. Must satisfy the toy::StringLike concept.
Parameters
stringThe toy::StringLike object to check if this string starts with.
Returns
true if this string view starts with a toy::StringLike object, false otherwise.
Note
The comparison is case-sensitive.
If a toy::StringLike object is empty, this method returns true.
If a toy::StringLike object is longer than this string view, returns false.

◆ swap()

void toy::CStringView::swap ( CStringView & string)
constexprnoexcept

Swaps the contents of this string view with another string view.

This method exchanges the contents of this string view with another string view of the same type. The operation is performed efficiently by swapping the internal data information.

Parameters
stringThe string view to swap contents with.
Postcondition
This string view contains the contents that were in other string.
The other string view contains the contents that were in this string.
Note
Self-swap is handled correctly and safely (no-op).

◆ utf8_size()

size_t toy::CStringView::utf8_size ( ) const
inlinenodiscardnoexcept

Returns the size of the Unicode string in UTF-8 encoding.

This method returns the number of Unicode characters in the UTF-8 encoded string, excluding the terminating null character. For ASCII strings, this value equals the size() method. For UTF-8 encoded strings, this method counts the number of Unicode characters rather than bytes. The method validates UTF-8 encoding and counts only complete, well-formed Unicode characters.

Returns
The number of Unicode characters in the string view, excluding the terminating null character.
Note
For ASCII strings, utf8_size() equals size().
For UTF-8 strings, utf8_size() may be less than size().
Invalid UTF-8 sequences are handled gracefully and may affect the count.
This method is useful for internationalization and text processing.

◆ operator<=> [1/5]

strong_ordering operator<=> ( const char * lhs,
const CStringView & rhs )
friend

Three-way comparison operator for C string and CStringView.

This operator provides a three-way comparison between a C string and a CStringView object. It returns a strong_ordering value that indicates the relationship between the strings.

Parameters
lhsThe C string to compare.
rhsThe CStringView object to compare.
Returns
strong_ordering::less if lhs is lexicographically less than rhs, strong_ordering::equal if they are equal, or strong_ordering::greater if lhs is lexicographically greater than rhs.
Precondition
The lhs pointer must not be null.
Note
The comparison is case-sensitive.
The comparison is performed lexicographically character by character.
Empty strings are considered equal.
See also
operator==(const char *, const CStringView &)

◆ operator<=> [2/5]

strong_ordering operator<=> ( const CStringView & lhs,
const char * rhs )
friend

Three-way comparison operator for CStringView and C string.

This operator provides a three-way comparison between a CStringView object and a C string. It returns a strong_ordering value that indicates the relationship between the strings.

Parameters
lhsThe CStringView object to compare.
rhsThe C string to compare.
Returns
strong_ordering::less if lhs is lexicographically less than rhs, strong_ordering::equal if they are equal, or strong_ordering::greater if lhs is lexicographically greater than rhs.
Precondition
The rhs pointer must not be null.
Note
The comparison is case-sensitive.
The comparison is performed lexicographically character by character.
Empty strings are considered equal.
See also
operator==(const CStringView &, const char *)

◆ operator<=> [3/5]

strong_ordering operator<=> ( const CStringView & lhs,
const CStringView & rhs )
friend

Three-way comparison operator for two CStringView objects.

This operator provides a three-way comparison between two CStringView objects. It returns a strong_ordering value that indicates the relationship between the string views.

Parameters
lhsThe left-hand side CStringView object to compare.
rhsThe right-hand side CStringView object to compare.
Returns
strong_ordering::less if lhs is lexicographically less than rhs, strong_ordering::equal if they are equal, or strong_ordering::greater if lhs is lexicographically greater than rhs.
Note
The comparison is case-sensitive.
The comparison is performed lexicographically character by character.
Empty string views are considered equal.
See also
operator==(const CStringView &, const CStringView &)

◆ operator<=> [4/5]

template<StringLike stringType>
strong_ordering operator<=> ( const CStringView & lhs,
const stringType & rhs )
friend

Three-way comparison operator for CStringView and toy::StringLike object.

This operator provides a three-way comparison between a CStringView object and a toy::StringLike object. It returns a strong_ordering value that indicates the relationship between the strings.

Template Parameters
stringTypeThe type of the toy::StringLike object. Must satisfy the toy::StringLike concept.
Parameters
lhsThe CStringView object to compare.
rhsThe toy::StringLike object to compare.
Returns
strong_ordering::less if lhs is lexicographically less than rhs, strong_ordering::equal if they are equal, or strong_ordering::greater if lhs is lexicographically greater than rhs.
Note
The comparison is case-sensitive.
The comparison is performed lexicographically character by character.
Empty strings are considered equal.
See also
operator==(const CStringView &, const stringType &)

◆ operator<=> [5/5]

template<StringLike stringType>
strong_ordering operator<=> ( const stringType & lhs,
const CStringView & rhs )
friend

Three-way comparison operator for toy::StringLike object and CStringView.

This operator provides a three-way comparison between a toy::StringLike object and a CStringView object. It returns a strong_ordering value that indicates the relationship between the strings.

Template Parameters
stringTypeThe type of the toy::StringLike object. Must satisfy the toy::StringLike concept.
Parameters
lhsThe toy::StringLike object to compare.
rhsThe CStringView object to compare.
Returns
strong_ordering::less if lhs is lexicographically less than rhs, strong_ordering::equal if they are equal, or strong_ordering::greater if lhs is lexicographically greater than rhs.
Note
The comparison is case-sensitive.
The comparison is performed lexicographically character by character.
Empty strings are considered equal.
See also
operator==(const stringType &, const CStringView &)

◆ operator== [1/5]

bool operator== ( const char * lhs,
const CStringView & rhs )
friend

Equality comparison operator for C string and CStringView.

This operator compares a C string with a CStringView object for equality.

Parameters
lhsThe C string.
rhsThe CStringView object.
Returns
true if both strings have the same content, false otherwise.
Precondition
The lhs pointer must not be null.
Note
The comparison is case-sensitive.
Empty strings are considered equal.
See also
operator<=>(const char *, const CStringView &)

◆ operator== [2/5]

bool operator== ( const CStringView & lhs,
const char * rhs )
friend

Equality comparison operator for CStringView and C string.

This operator compares a CStringView object with a C string for equality.

Parameters
lhsThe CStringView object.
rhsThe C string.
Returns
true if both strings have the same content, false otherwise.
Precondition
The rhs pointer must not be null.
Note
The comparison is case-sensitive.
Empty strings are considered equal.
See also
operator<=>(const CStringView &, const char *)

◆ operator== [3/5]

bool operator== ( const CStringView & lhs,
const CStringView & rhs )
friend

Equality comparison operator for two CStringView objects.

This operator compares two CStringView objects for equality. The comparison is performed character by character.

Parameters
lhsThe left-hand side CStringView object.
rhsThe right-hand side CStringView object.
Returns
true if both strings view have the same content, false otherwise.
Note
The comparison is case-sensitive.
Empty string views are considered equal.
See also
operator<=>(const CStringView &, const CStringView &)

◆ operator== [4/5]

template<StringLike stringType>
bool operator== ( const CStringView & lhs,
const stringType & rhs )
friend

Equality comparison operator for CStringView and toy::StringLike object.

This operator compares a CStringView object with a toy::StringLike object for equality.

Template Parameters
stringTypeThe type of the toy::StringLike object. Must satisfy the toy::StringLike concept.
Parameters
lhsThe CStringView object.
rhsThe toy::StringLike object.
Returns
true if both strings have the same content, false otherwise.
Note
The comparison is case-sensitive.
Empty strings are considered equal.
See also
operator<=>(const CStringView &, const stringType &)

◆ operator== [5/5]

template<StringLike stringType>
bool operator== ( const stringType & lhs,
const CStringView & rhs )
friend

Equality comparison operator for toy::StringLike object and CStringView.

This operator compares a toy::StringLike object with a CStringView object for equality.

Template Parameters
stringTypeThe type of the toy::StringLike object. Must satisfy the toy::StringLike concept.
Parameters
lhsThe toy::StringLike object.
rhsThe CStringView object.
Returns
true if both strings have the same content, false otherwise.
Note
The comparison is case-sensitive.
Empty strings are considered equal.
See also
operator<=>(const stringType &, const CStringView &)

Member Data Documentation

◆ _data

const char* toy::CStringView::_data
private

Pointer to the wrapped C string.

◆ _emptyString

char toy::CStringView::_emptyString[] {'\0'}
staticconstexprprivate

Static empty C string used as default value for null pointers.

◆ npos

size_t toy::CStringView::npos = size_t(-1)
staticconstexpr

The special value, its exact meaning depends on the context.


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