Duckcpp 2.1.0
Duckcpp is a high-performance c++ graphics engine.
Loading...
Searching...
No Matches
dcpp::nub::basic_string< T, TAlloc > Class Template Reference

Very simple string class with some useful features. More...

Public Types

using char_type = T
 

Public Member Functions

 basic_string ()
 Default constructor.
 
 basic_string (const self_type &other)
 Constructor.
 
template<class B , class A >
 basic_string (const dcpp::nub::basic_string< B, A > &other)
 Constructor from other string types.
 
 basic_string (const double number)
 Constructs a string from a float.
 
 basic_string (int number)
 Constructs a string from an int.
 
 basic_string (unsigned int number)
 Constructs a string from an unsigned int.
 
 basic_string (long number)
 Constructs a string from a long.
 
 basic_string (unsigned long number)
 Constructs a string from an unsigned long.
 
template<class B >
 basic_string (const B *const c, dcpp::u32 length)
 Constructor for copying a string from a pointer with a given length.
 
template<class B >
 basic_string (const B *const c)
 Constructor for Unicode and ASCII strings.
 
virtual ~basic_string ()
 Destructor.
 
self_typeoperator= (const self_type &other)
 Assignment operator.
 
template<class B , class A >
self_typeoperator= (const dcpp::nub::basic_string< B, A > &other)
 Assignment operator for other string types.
 
template<class B >
self_typeoperator= (const B *const c)
 Assignment operator for strings, ASCII and Unicode.
 
self_type operator+ (const self_type &other) const
 Append operator for other strings.
 
template<class B >
self_type operator+ (const B *const c) const
 Append operator for strings, ASCII and Unicode.
 
Toperator[] (const dcpp::u32 index)
 Direct access operator.
 
const Toperator[] (const dcpp::u32 index) const
 Direct access operator.
 
bool operator== (const T *const str) const
 Equality operator.
 
bool operator== (const self_type &other) const
 Equality operator.
 
bool operator< (const self_type &other) const
 Is smaller comparator.
 
bool operator!= (const T *const str) const
 Inequality operator.
 
bool operator!= (const self_type &other) const
 Inequality operator.
 
dcpp::u32 size () const
 Returns length of the string's content.
 
bool empty () const
 
void clear (bool releaseMemory=true)
 
const Tdata () const
 Returns character string.
 
std::basic_string< Tstr () const
 Returns std::string or std::wstring. The traits_type and allocator_type of the returned std::basic_string are the same as those from std::string or std::wstring.
 
 operator std::basic_string< T > () const
 Returns std::string or std::wstring. The traits_type and allocator_type of the returned std::basic_string are the same as those from std::string or std::wstring. Note that this type operator is marked explicit.
 
self_typemake_lower ()
 Makes the string lower case.
 
self_typemake_upper ()
 Makes the string upper case.
 
bool equals_ignore_case (const self_type &other) const
 Compares the strings ignoring case.
 
bool equals_substring_ignore_case (const self_type &other, const dcpp::i32 sourcePos=0) const
 Compares the strings ignoring case.
 
bool lower_ignore_case (const self_type &other) const
 Compares the strings ignoring case.
 
bool equalsn (const self_type &other, dcpp::u32 n) const
 compares the first n characters of the strings
 
bool equalsn (const T *const str, dcpp::u32 n) const
 compares the first n characters of the strings
 
self_typeappend (T character)
 Appends a character to this string.
 
self_typeappend (const T *const other, dcpp::u32 length=0xffffffff)
 Appends a char string to this string.
 
self_typeappend (const self_type &other)
 Appends a string to this string.
 
self_typeappend (const self_type &other, dcpp::u32 length)
 Appends a string of the length l to this string.
 
self_typeinsert (dcpp::u32 pos, const char *s, dcpp::u32 n)
 Insert a certain amount of characters into the string before the given index.
 
void reserve (dcpp::u32 count)
 Reserves some memory.
 
dcpp::i32 findFirst (T c) const
 finds first occurrence of character in string
 
dcpp::i32 findFirstChar (const T *const c, dcpp::u32 count=1) const
 finds first occurrence of a character of a list in string
 
template<class B >
dcpp::i32 findFirstCharNotInList (const B *const c, dcpp::u32 count=1) const
 Finds first position of a character not in a given list.
 
template<class B >
dcpp::i32 findLastCharNotInList (const B *const c, dcpp::u32 count=1) const
 Finds last position of a character not in a given list.
 
dcpp::i32 findNext (T c, dcpp::u32 startPos) const
 finds next occurrence of character in string
 
dcpp::i32 findLast (T c, dcpp::i32 start=-1) const
 finds last occurrence of character in string
 
dcpp::i32 findLastChar (const T *const c, dcpp::u32 count=1) const
 finds last occurrence of a character of a list in string
 
template<class B >
dcpp::i32 find (const B *const str, const dcpp::u32 start=0) const
 finds another string in this string
 
self_type subString (dcpp::u32 begin, dcpp::i32 length, bool make_lower=false) const
 Returns a substring.
 
self_typeoperator+= (T c)
 Appends a character to this string.
 
self_typeoperator+= (const T *const c)
 Appends a char string to this string.
 
self_typeoperator+= (const self_type &other)
 Appends a string to this string.
 
self_typeoperator+= (const int i)
 Appends a string representation of a number to this string.
 
self_typeoperator+= (const unsigned int i)
 Appends a string representation of a number to this string.
 
self_typeoperator+= (const long i)
 Appends a string representation of a number to this string.
 
self_typeoperator+= (const unsigned long i)
 Appends a string representation of a number to this string.
 
self_typeoperator+= (const double i)
 Appends a string representation of a number to this string.
 
self_typeoperator+= (const float i)
 Appends a string representation of a number to this string.
 
self_typereplace (T toReplace, T replaceWith)
 Replaces all characters of a special type with another one.
 
self_typereplace (const self_type &toReplace, const self_type &replaceWith)
 Replaces all instances of a string with another one.
 
self_typeremove (T c)
 Removes characters from a string.
 
self_typeremove (const self_type &toRemove)
 Removes a string from the string.
 
self_typeremoveChars (const self_type &characters)
 Removes characters from a string.
 
self_typetrim (const self_type &whitespace=" \t\n\r")
 Trims the string.
 
self_typeeraseTrailingFloatZeros (char decimalPoint='.')
 Erase 0's at the end when a string ends with a floating point number.
 
self_typeerase (dcpp::u32 index)
 Erases a character from the string.
 
self_typevalidate ()
 verify the existing string.
 
T lastChar () const
 gets the last char of a string or null
 
template<class container >
dcpp::u32 split (container &ret, const T *const delimiter, dcpp::u32 countDelimiters=1, bool ignoreEmptyTokens=true, bool keepSeparators=false) const
 Split string into parts (tokens).
 

Friends

size_t multibyteToWString (dcpp::nub::wstring &destination, const char *source, dcpp::u32 sourceSize)
 Internally used by the other multibyteToWString functions.
 

Detailed Description

template<typename T, typename TAlloc>
class dcpp::nub::basic_string< T, TAlloc >

Very simple string class with some useful features.

basic_string<dcpp::c8> and basic_string<wchar_t> both accept Unicode AND ASCII/Latin-1, so you can assign Unicode to basic_string<dcpp::c8> and ASCII/Latin-1 to basic_string<wchar_t> (and the other way round) if you want to.

However, note that the conversation between both is not done using any encoding. This means that dcpp::c8 strings are treated as ASCII/Latin-1, not UTF-8, and are simply expanded to the equivalent wchar_t, while Unicode/wchar_t characters are truncated to 8-bit ASCII/Latin-1 characters, discarding all other information in the wchar_t.

Helper functions for converting between UTF-8 and wchar_t are provided outside the string class for explicit use.

Member Function Documentation

◆ append() [1/4]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::append ( const self_type other)
inline

Appends a string to this string.

Parameters
otherString to append.

◆ append() [2/4]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::append ( const self_type other,
dcpp::u32  length 
)
inline

Appends a string of the length l to this string.

Parameters
otherother String to append to this string.
lengthHow much characters of the other string to add to this one.

◆ append() [3/4]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::append ( const T *const  other,
dcpp::u32  length = 0xffffffff 
)
inline

Appends a char string to this string.

Parameters
otherChar string to append.
lengthThe length of the string to append.

◆ append() [4/4]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::append ( T  character)
inline

Appends a character to this string.

Parameters
characterCharacter to append.

◆ data()

template<typename T , typename TAlloc >
const T * dcpp::nub::basic_string< T, TAlloc >::data ( ) const
inline

Returns character string.

Returns
pointer to NUL terminated string.

◆ empty()

template<typename T , typename TAlloc >
bool dcpp::nub::basic_string< T, TAlloc >::empty ( ) const
inline

Informs if the string is empty or not.

Returns
True if the string is empty, false if not.

◆ equals_ignore_case()

template<typename T , typename TAlloc >
bool dcpp::nub::basic_string< T, TAlloc >::equals_ignore_case ( const self_type other) const
inline

Compares the strings ignoring case.

Parameters
otherOther string to compare.
Returns
True if the strings are equal ignoring case.

◆ equals_substring_ignore_case()

template<typename T , typename TAlloc >
bool dcpp::nub::basic_string< T, TAlloc >::equals_substring_ignore_case ( const self_type other,
const dcpp::i32  sourcePos = 0 
) const
inline

Compares the strings ignoring case.

Parameters
otherOther string to compare.
sourcePoswhere to start to compare in the string
Returns
True if the strings are equal ignoring case.

◆ equalsn() [1/2]

template<typename T , typename TAlloc >
bool dcpp::nub::basic_string< T, TAlloc >::equalsn ( const self_type other,
dcpp::u32  n 
) const
inline

compares the first n characters of the strings

Parameters
otherOther string to compare.
nNumber of characters to compare
Returns
True if the n first characters of both strings are equal.

◆ equalsn() [2/2]

template<typename T , typename TAlloc >
bool dcpp::nub::basic_string< T, TAlloc >::equalsn ( const T *const  str,
dcpp::u32  n 
) const
inline

compares the first n characters of the strings

Parameters
strOther string to compare.
nNumber of characters to compare
Returns
True if the n first characters of both strings are equal.

◆ erase()

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::erase ( dcpp::u32  index)
inline

Erases a character from the string.

May be slow, because all elements following after the erased element have to be copied.

Parameters
indexIndex of element to be erased.

◆ eraseTrailingFloatZeros()

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::eraseTrailingFloatZeros ( char  decimalPoint = '.')
inline

Erase 0's at the end when a string ends with a floating point number.

After generating strings from floats we often end up with strings ending up with lots of zeros which don't add any value. Erase 'em all. Examples: "0.100000" becomes "0.1" "10.000000" becomes "10" "foo 3.140000" becomes "foo 3.14" "no_num.000" stays "no_num.000" "1." stays "1."

◆ find()

template<typename T , typename TAlloc >
template<class B >
dcpp::i32 dcpp::nub::basic_string< T, TAlloc >::find ( const B *const  str,
const dcpp::u32  start = 0 
) const
inline

finds another string in this string

Parameters
strAnother string
startStart position of the search
Returns
Positions where the string has been found, or -1 if not found.

◆ findFirst()

template<typename T , typename TAlloc >
dcpp::i32 dcpp::nub::basic_string< T, TAlloc >::findFirst ( T  c) const
inline

finds first occurrence of character in string

Parameters
cCharacter to search for.
Returns
Position where the character has been found, or -1 if not found.

◆ findFirstChar()

template<typename T , typename TAlloc >
dcpp::i32 dcpp::nub::basic_string< T, TAlloc >::findFirstChar ( const T *const  c,
dcpp::u32  count = 1 
) const
inline

finds first occurrence of a character of a list in string

Parameters
cList of characters to find. For example if the method should find the first occurrence of 'a' or 'b', this parameter should be "ab".
countAmount of characters in the list. Usually, this should be strlen(c)
Returns
Position where one of the characters has been found, or -1 if not found.

◆ findFirstCharNotInList()

template<typename T , typename TAlloc >
template<class B >
dcpp::i32 dcpp::nub::basic_string< T, TAlloc >::findFirstCharNotInList ( const B *const  c,
dcpp::u32  count = 1 
) const
inline

Finds first position of a character not in a given list.

Parameters
cList of characters not to find. For example if the method should find the first occurrence of a character not 'a' or 'b', this parameter should be "ab".
countAmount of characters in the list. Usually, this should be strlen(c)
Returns
Position where the character has been found, or -1 if not found.

◆ findLast()

template<typename T , typename TAlloc >
dcpp::i32 dcpp::nub::basic_string< T, TAlloc >::findLast ( T  c,
dcpp::i32  start = -1 
) const
inline

finds last occurrence of character in string

Parameters
cCharacter to search for.
startstart to search reverse ( default = -1, on end )
Returns
Position where the character has been found, or -1 if not found.

◆ findLastChar()

template<typename T , typename TAlloc >
dcpp::i32 dcpp::nub::basic_string< T, TAlloc >::findLastChar ( const T *const  c,
dcpp::u32  count = 1 
) const
inline

finds last occurrence of a character of a list in string

Parameters
cList of strings to find. For example if the method should find the last occurrence of 'a' or 'b', this parameter should be "ab".
countAmount of characters in the list. Usually, this should be strlen(c)
Returns
Position where one of the characters has been found, or -1 if not found.

◆ findLastCharNotInList()

template<typename T , typename TAlloc >
template<class B >
dcpp::i32 dcpp::nub::basic_string< T, TAlloc >::findLastCharNotInList ( const B *const  c,
dcpp::u32  count = 1 
) const
inline

Finds last position of a character not in a given list.

Parameters
cList of characters not to find. For example if the method should find the first occurrence of a character not 'a' or 'b', this parameter should be "ab".
countAmount of characters in the list. Usually, this should be strlen(c)
Returns
Position where the character has been found, or -1 if not found.

◆ findNext()

template<typename T , typename TAlloc >
dcpp::i32 dcpp::nub::basic_string< T, TAlloc >::findNext ( T  c,
dcpp::u32  startPos 
) const
inline

finds next occurrence of character in string

Parameters
cCharacter to search for.
startPosPosition in string to start searching.
Returns
Position where the character has been found, or -1 if not found.

◆ lower_ignore_case()

template<typename T , typename TAlloc >
bool dcpp::nub::basic_string< T, TAlloc >::lower_ignore_case ( const self_type other) const
inline

Compares the strings ignoring case.

Parameters
otherOther string to compare.
Returns
True if this string is smaller ignoring case.

◆ operator std::basic_string< T >()

template<typename T , typename TAlloc >
dcpp::nub::basic_string< T, TAlloc >::operator std::basic_string< T > ( ) const
inlineexplicit

Returns std::string or std::wstring. The traits_type and allocator_type of the returned std::basic_string are the same as those from std::string or std::wstring. Note that this type operator is marked explicit.

Returns
a copy of std::string or std::wstring.

◆ operator+=() [1/9]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::operator+= ( const double  i)
inline

Appends a string representation of a number to this string.

Parameters
iNumber to append.

◆ operator+=() [2/9]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::operator+= ( const float  i)
inline

Appends a string representation of a number to this string.

Parameters
iNumber to append.

◆ operator+=() [3/9]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::operator+= ( const int  i)
inline

Appends a string representation of a number to this string.

Parameters
iNumber to append.

◆ operator+=() [4/9]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::operator+= ( const long  i)
inline

Appends a string representation of a number to this string.

Parameters
iNumber to append.

◆ operator+=() [5/9]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::operator+= ( const self_type other)
inline

Appends a string to this string.

Parameters
otherString to append.

◆ operator+=() [6/9]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::operator+= ( const T *const  c)
inline

Appends a char string to this string.

Parameters
cChar string to append.

◆ operator+=() [7/9]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::operator+= ( const unsigned int  i)
inline

Appends a string representation of a number to this string.

Parameters
iNumber to append.

◆ operator+=() [8/9]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::operator+= ( const unsigned long  i)
inline

Appends a string representation of a number to this string.

Parameters
iNumber to append.

◆ operator+=() [9/9]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::operator+= ( T  c)
inline

Appends a character to this string.

Parameters
cCharacter to append.

◆ remove() [1/2]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::remove ( const self_type toRemove)
inline

Removes a string from the string.

Parameters
toRemoveString to remove.

◆ remove() [2/2]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::remove ( T  c)
inline

Removes characters from a string.

Parameters
cCharacter to remove.

◆ removeChars()

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::removeChars ( const self_type characters)
inline

Removes characters from a string.

Parameters
charactersCharacters to remove.

◆ replace() [1/2]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::replace ( const self_type toReplace,
const self_type replaceWith 
)
inline

Replaces all instances of a string with another one.

Parameters
toReplaceThe string to replace.
replaceWithThe string replacing the old one.

◆ replace() [2/2]

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::replace ( T  toReplace,
T  replaceWith 
)
inline

Replaces all characters of a special type with another one.

Parameters
toReplaceCharacter to replace.
replaceWithCharacter replacing the old one.

◆ reserve()

template<typename T , typename TAlloc >
void dcpp::nub::basic_string< T, TAlloc >::reserve ( dcpp::u32  count)
inline

Reserves some memory.

Parameters
countAmount of characters to reserve.

◆ size()

template<typename T , typename TAlloc >
dcpp::u32 dcpp::nub::basic_string< T, TAlloc >::size ( ) const
inline

Returns length of the string's content.

Returns
Length of the string's content in characters, excluding the trailing NUL.

◆ split()

template<typename T , typename TAlloc >
template<class container >
dcpp::u32 dcpp::nub::basic_string< T, TAlloc >::split ( container ret,
const T *const  delimiter,
dcpp::u32  countDelimiters = 1,
bool  ignoreEmptyTokens = true,
bool  keepSeparators = false 
) const
inline

Split string into parts (tokens).

This method will split a string at certain delimiter characters into the container passed in as reference. The type of the container has to be given as template parameter. It must provide a push_back and a size method.

Parameters
retThe result container. Tokens are added, the container is not cleared.
delimiterC-style string of delimiter characters
countDelimitersNumber of delimiter characters
ignoreEmptyTokensFlag to avoid empty substrings in the result container. If two delimiters occur without a character in between or an empty substring would be placed in the result. Or if a delimiter is the last character an empty substring would be added at the end. If this flag is set, only non-empty strings are stored.
keepSeparatorsFlag which allows to add the separator to the result string. If this flag is true, the concatenation of the substrings results in the original string. Otherwise, only the characters between the delimiters are returned.
Returns
The number of resulting substrings

◆ str()

template<typename T , typename TAlloc >
std::basic_string< T > dcpp::nub::basic_string< T, TAlloc >::str ( ) const
inline

Returns std::string or std::wstring. The traits_type and allocator_type of the returned std::basic_string are the same as those from std::string or std::wstring.

Returns
a copy of std::string or std::wstring.

◆ subString()

template<typename T , typename TAlloc >
self_type dcpp::nub::basic_string< T, TAlloc >::subString ( dcpp::u32  begin,
dcpp::i32  length,
bool  make_lower = false 
) const
inline

Returns a substring.

Parameters
beginStart of substring.
lengthLength of substring.
make_lowercopy only lower case

◆ trim()

template<typename T , typename TAlloc >
self_type & dcpp::nub::basic_string< T, TAlloc >::trim ( const self_type whitespace = " \t\n\r")
inline

Trims the string.

Removes the specified characters (by default, Latin-1 whitespace) from the beginning and the end of the string.


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

Duckcpp    @cppfx.xyz