5#ifndef __IRR_STRING_H_INCLUDED__
6#define __IRR_STRING_H_INCLUDED__
9#include "irrAllocator.h"
41static eLocaleID locale_current = IRR_LOCALE_ANSI;
42static inline void locale_set (
eLocaleID id )
48static inline u32 locale_lower (
u32 x )
50 switch ( locale_current )
52 case IRR_LOCALE_GERMAN:
57 return x >=
'A' && x <=
'Z' ? x + 0x20 : x;
61static inline u32 locale_upper (
u32 x )
63 switch ( locale_current )
65 case IRR_LOCALE_GERMAN:
71 return x >=
'a' && x <=
'z' ? x + (
'A' -
'a' ) : x;
87template <
typename T,
typename TAlloc = irrAllocator<T> >
96 : array(0), allocated(1), used(1)
98 array = allocator.allocate(1);
105 : array(0), allocated(0), used(0)
111 template <
class B,
class A>
113 : array(0), allocated(0), used(0)
121 : array(0), allocated(0), used(0)
124 snprintf(tmpbuf, 255,
"%0.6f", number);
131 : array(0), allocated(0), used(0)
135 bool negative =
false;
161 tmpbuf[idx] = (
c8)(
'0' + (number % 10));
173 *
this = &tmpbuf[idx];
179 : array(0), allocated(0), used(0)
200 tmpbuf[idx] = (
c8)(
'0' + (number % 10));
204 *
this = &tmpbuf[idx];
210 : array(0), allocated(0), used(0)
214 bool negative =
false;
240 tmpbuf[idx] = (
c8)(
'0' + (number % 10));
252 *
this = &tmpbuf[idx];
258 : array(0), allocated(0), used(0)
279 tmpbuf[idx] = (
c8)(
'0' + (number % 10));
283 *
this = &tmpbuf[idx];
290 : array(0), allocated(0), used(0)
299 allocated = used = length+1;
300 array = allocator.allocate(used);
302 for (
u32 l = 0; l<length; ++l)
312 : array(0), allocated(0), used(0)
321 allocator.deallocate(array);
331 used = other.
size()+1;
334 allocator.deallocate(array);
336 array = allocator.allocate(used);
339 const T* p = other.
c_str();
340 for (
u32 i=0; i<used; ++i, ++p)
347 template <
class B,
class A>
350 *
this = other.
c_str();
363 array = allocator.allocate(1);
371 if ((
void*)c == (
void*)array)
389 array = allocator.allocate(used);
392 for (
u32 l = 0; l<len; ++l)
395 if (oldArray != array)
396 allocator.deallocate(oldArray);
426 _IRR_DEBUG_BREAK_IF(index>=used)
434 _IRR_DEBUG_BREAK_IF(index>=used)
446 for (i=0; array[i] && str[i]; ++i)
447 if (array[i] != str[i])
450 return (!array[i] && !str[i]);
457 for (
u32 i=0; array[i] && other.array[i]; ++i)
458 if (array[i] != other.array[i])
461 return used == other.used;
468 for (
u32 i=0; array[i] && other.array[i]; ++i)
470 const s32 diff = array[i] - other.array[i];
475 return (used < other.used);
482 return !(*
this == str);
489 return !(*
this == other);
505 return (
size() == 0);
519 for (
u32 i=0; array[i]; ++i)
520 array[i] = locale_lower ( array[i] );
528 for (
u32 i=0; array[i]; ++i)
529 array[i] = locale_upper ( array[i] );
539 for(
u32 i=0; array[i] && other[i]; ++i)
540 if (locale_lower( array[i]) != locale_lower(other[i]))
543 return used == other.used;
552 if ( (
u32) sourcePos >= used )
556 for( i=0; array[sourcePos + i] && other[i]; ++i)
557 if (locale_lower( array[sourcePos + i]) != locale_lower(other[i]))
560 return array[sourcePos + i] == 0 && other[i] == 0;
569 for(
u32 i=0; array[i] && other.array[i]; ++i)
571 s32 diff = (
s32) locale_lower ( array[i] ) - (
s32) locale_lower ( other.array[i] );
576 return used < other.used;
587 for(i=0; i < n && array[i] && other[i]; ++i)
588 if (array[i] != other[i])
593 return (i == n) || (used == other.used);
606 for(i=0; i < n && array[i] && str[i]; ++i)
607 if (array[i] != str[i])
612 return (i == n) || (array[i] == 0 && str[i] == 0);
620 if (used + 1 > allocated)
621 reallocate(used + 1);
625 array[used-2] = character;
650 if (used + len > allocated)
651 reallocate(used + len);
656 for (
u32 l=0; l<len; ++l)
657 array[l+used] = *(other+l);
669 if (other.
size() == 0)
675 if (used + len > allocated)
676 reallocate(used + len);
678 for (
u32 l=0; l<len; ++l)
679 array[used+l] = other[l];
692 if (other.
size() == 0)
695 if (other.
size() < length)
701 if (used + length > allocated)
702 reallocate(used + length);
706 for (
u32 l=0; l<length; ++l)
707 array[l+used] = other[l];
722 if (count < allocated)
735 for (
u32 i=0; i<used-1; ++i)
754 for (
u32 i=0; i<used-1; ++i)
755 for (
u32 j=0; j<count; ++j)
756 if (array[i] == c[j])
776 for (
u32 i=0; i<used-1; ++i)
779 for (j=0; j<count; ++j)
780 if (array[i] == c[j])
803 for (
s32 i=(
s32)(used-2); i>=0; --i)
806 for (j=0; j<count; ++j)
807 if (array[i] == c[j])
824 for (
u32 i=startPos; i<used-1; ++i)
840 for (
s32 i=start; i>=0; --i)
859 for (
s32 i=(
s32)used-2; i>=0; --i)
860 for (
u32 j=0; j<count; ++j)
861 if (array[i] == c[j])
886 for (
u32 i=start; i<used-len; ++i)
890 while(str[j] && array[i+j] == str[j])
910 if ((length <= 0) || (begin>=
size()))
913 if ((length+begin) >
size())
914 length =
size()-begin;
922 for (i=0; i<length; ++i)
923 o.array[i] = array[i+begin];
927 for (i=0; i<length; ++i)
928 o.array[i] = locale_lower ( array[i+begin] );
1024 for (
u32 i=0; i<used-1; ++i)
1025 if (array[i] == toReplace)
1026 array[i] = replaceWith;
1036 if (toReplace.
size() == 0)
1039 const T* other = toReplace.
c_str();
1041 const u32 other_size = toReplace.
size();
1042 const u32 replace_size = replaceWith.
size();
1045 s32 delta = replace_size - other_size;
1051 while ((pos =
find(other, pos)) != -1)
1053 for (
u32 i = 0; i < replace_size; ++i)
1064 for (
u32 pos = 0; pos < used; ++i, ++pos)
1067 if (array[pos] == *other)
1071 for (j = 0; j < other_size; ++j)
1073 if (array[pos + j] != other[j])
1078 if (j == other_size)
1080 for (j = 0; j < replace_size; ++j)
1082 i += replace_size - 1;
1083 pos += other_size - 1;
1089 array[i] = array[pos];
1101 while ((pos =
find(other, pos)) != -1)
1108 u32 len = delta * find_count;
1109 if (used + len > allocated)
1110 reallocate(used + len);
1114 while ((pos =
find(other, pos)) != -1)
1116 T* start = array + pos + other_size - 1;
1117 T* ptr = array + used - 1;
1118 T* end = array + delta + used -1;
1121 while (ptr != start)
1129 for (
u32 i = 0; i < replace_size; ++i)
1132 pos += replace_size;
1146 for (
u32 i=0; i<used-1; ++i)
1154 array[pos++] = array[i];
1171 for (
u32 i=0; i<used-1; ++i)
1176 if (array[i + j] != toRemove[j])
1187 array[pos++] = array[i];
1199 if (characters.
size() == 0)
1204 for (
u32 i=0; i<used-1; ++i)
1208 bool docontinue =
false;
1209 for (
u32 j=0; j<characters.
size(); ++j)
1211 if (characters[j] == array[i])
1221 array[pos++] = array[i];
1242 return (*
this =
subString(begin, (end +1) - begin));
1252 _IRR_DEBUG_BREAK_IF(index>=used)
1254 for (
u32 i=index+1; i<used; ++i)
1255 array[i-1] = array[i];
1265 for (
u32 i=0; i<allocated; ++i)
1275 if ( allocated > 0 )
1291 return used > 1 ? array[used-2] : 0;
1312 template<
class container>
1313 u32 split(container& ret,
const T*
const c,
u32 count=1,
bool ignoreEmptyTokens=
true,
bool keepSeparators=
false)
const
1318 const u32 oldSize=ret.size();
1320 bool lastWasSeparator =
false;
1321 for (
u32 i=0; i<used; ++i)
1323 bool foundSeparator =
false;
1324 for (
u32 j=0; j<count; ++j)
1326 if (array[i] == c[j])
1328 if ((!ignoreEmptyTokens || i - lastpos != 0) &&
1331 foundSeparator =
true;
1332 lastpos = (keepSeparators ? i : i + 1);
1336 lastWasSeparator = foundSeparator;
1338 if ((used - 1) > lastpos)
1340 return ret.size()-oldSize;
1346 void reallocate(
u32 new_size)
1348 T* old_array =
array;
1350 array = allocator.allocate(new_size);
1351 allocated = new_size;
1353 u32 amount = used < new_size ? used : new_size;
1354 for (
u32 i=0; i<amount; ++i)
1355 array[i] = old_array[i];
1357 if (allocated < used)
1360 allocator.deallocate(old_array);
Self reallocating template array (like stl vector) with additional features.
Definition irrArray.h:23
Definition irrString.h:89
~string()
Destructor.
Definition irrString.h:319
string< T, TAlloc > & append(const T *const other, u32 length=0xffffffff)
Appends a char string to this string.
Definition irrString.h:635
s32 findFirstChar(const T *const c, u32 count=1) const
finds first occurrence of a character of a list in string
Definition irrString.h:749
u32 size() const
Returns length of the string's content.
Definition irrString.h:496
bool equals_ignore_case(const string< T, TAlloc > &other) const
Compares the strings ignoring case.
Definition irrString.h:537
s32 find(const B *const str, const u32 start=0) const
finds another string in this string
Definition irrString.h:874
bool operator!=(const T *const str) const
Inequality operator.
Definition irrString.h:480
string< T, TAlloc > & operator+=(T c)
Appends a character to this string.
Definition irrString.h:940
string(long number)
Constructs a string from a long.
Definition irrString.h:209
s32 findFirst(T c) const
finds first occurrence of character in string
Definition irrString.h:733
string(unsigned long number)
Constructs a string from an unsigned long.
Definition irrString.h:257
s32 findFirstCharNotInList(const B *const c, u32 count=1) const
Finds first position of a character not in a given list.
Definition irrString.h:771
bool operator==(const string< T, TAlloc > &other) const
Equality operator.
Definition irrString.h:455
string< T, TAlloc > & validate()
verify the existing string.
Definition irrString.h:1262
string(int number)
Constructs a string from an int.
Definition irrString.h:130
string< T, TAlloc > & append(T character)
Appends a character to this string.
Definition irrString.h:618
void reserve(u32 count)
Reserves some memory.
Definition irrString.h:720
string< T, TAlloc > & trim(const string< T, TAlloc > &whitespace=" \t\n\r")
Trims the string.
Definition irrString.h:1233
string< T, TAlloc > & remove(const string< T, TAlloc > &toRemove)
Removes a string from the string.
Definition irrString.h:1164
string< T > subString(u32 begin, s32 length, bool make_lower=false) const
Returns a substring.
Definition irrString.h:906
s32 findLast(T c, s32 start=-1) const
finds last occurrence of character in string
Definition irrString.h:837
string< T, TAlloc > & append(const string< T, TAlloc > &other)
Appends a string to this string.
Definition irrString.h:667
string< T, TAlloc > & operator=(const B *const c)
Assignment operator for strings, ascii and unicode.
Definition irrString.h:357
string(const B *const c)
Constructor for unicode and ascii strings.
Definition irrString.h:311
string()
Default constructor.
Definition irrString.h:95
T lastChar() const
gets the last char of a string or null
Definition irrString.h:1289
string< T, TAlloc > & operator=(const string< B, A > &other)
Assignment operator for other string types.
Definition irrString.h:348
bool operator<(const string< T, TAlloc > &other) const
Is smaller comparator.
Definition irrString.h:466
string(const B *const c, u32 length)
Constructor for copying a string from a pointer with a given length.
Definition irrString.h:289
string< T, TAlloc > & replace(T toReplace, T replaceWith)
Replaces all characters of a special type with another one.
Definition irrString.h:1022
s32 findLastChar(const T *const c, u32 count=1) const
finds last occurrence of a character of a list in string
Definition irrString.h:854
string(const string< B, A > &other)
Constructor from other string types.
Definition irrString.h:112
string(unsigned int number)
Constructs a string from an unsigned int.
Definition irrString.h:178
s32 findNext(T c, u32 startPos) const
finds next occurrence of character in string
Definition irrString.h:822
string< T, TAlloc > & replace(const string< T, TAlloc > &toReplace, const string< T, TAlloc > &replaceWith)
Replaces all instances of a string with another one.
Definition irrString.h:1034
string< T, TAlloc > & operator=(const string< T, TAlloc > &other)
Assignment operator.
Definition irrString.h:326
string< T, TAlloc > & erase(u32 index)
Erases a character from the string.
Definition irrString.h:1250
string< T, TAlloc > & make_upper()
Makes the string upper case.
Definition irrString.h:526
s32 findLastCharNotInList(const B *const c, u32 count=1) const
Finds last position of a character not in a given list.
Definition irrString.h:798
bool lower_ignore_case(const string< T, TAlloc > &other) const
Compares the strings ignoring case.
Definition irrString.h:567
string< T, TAlloc > operator+(const string< T, TAlloc > &other) const
Append operator for other strings.
Definition irrString.h:403
string(const double number)
Constructs a string from a float.
Definition irrString.h:120
string(const string< T, TAlloc > &other)
Constructor.
Definition irrString.h:104
u32 split(container &ret, const T *const c, u32 count=1, bool ignoreEmptyTokens=true, bool keepSeparators=false) const
split string into parts.
Definition irrString.h:1313
bool operator!=(const string< T, TAlloc > &other) const
Inequality operator.
Definition irrString.h:487
string< T, TAlloc > & append(const string< T, TAlloc > &other, u32 length)
Appends a string of the length l to this string.
Definition irrString.h:690
const T * c_str() const
Returns character string.
Definition irrString.h:510
string< T, TAlloc > & removeChars(const string< T, TAlloc > &characters)
Removes characters from a string.
Definition irrString.h:1197
string< T, TAlloc > & make_lower()
Makes the string lower case.
Definition irrString.h:517
string< T, TAlloc > & remove(T c)
Removes characters from a string.
Definition irrString.h:1142
bool equalsn(const T *const str, u32 n) const
compares the first n characters of the strings
Definition irrString.h:601
bool equals_substring_ignore_case(const string< T, TAlloc > &other, const s32 sourcePos=0) const
Compares the strings ignoring case.
Definition irrString.h:550
bool equalsn(const string< T, TAlloc > &other, u32 n) const
compares the first n characters of the strings
Definition irrString.h:584
bool operator==(const T *const str) const
Equality operator.
Definition irrString.h:440
string< T, TAlloc > operator+(const B *const c) const
Append operator for strings, ascii and unicode.
Definition irrString.h:414
T & operator[](const u32 index)
Direct access operator.
Definition irrString.h:424
bool empty() const
Definition irrString.h:503
Basic classes such as vectors, planes, arrays, lists, and so on can be found in this namespace.
Definition aabbox3d.h:15
eLocaleID
Very simple string class with some useful features.
Definition irrString.h:36
IRRLICHT_API void wcharToUtf8(const wchar_t *in, char *out, const u64 len)
Convert this wchar string to utf-8.
const T clamp(const T &value, const T &low, const T &high)
clamps a value between low and high
Definition irrMath.h:166
string< c8 > stringc
Typedef for character strings.
Definition irrString.h:1373
IRRLICHT_API void utf8ToWchar(const char *in, wchar_t *out, const u64 len)
Convert this utf-8-encoded string to the platform's wchar.
string< wchar_t > stringw
Typedef for wide character strings.
Definition irrString.h:1376
Everything in the Irrlicht Engine can be found in this namespace.
Definition Skylicht.h:33
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.h:58
char c8
8 bit character variable.
Definition irrTypes.h:31
unsigned long long u64
64 bit unsigned variable.
Definition irrTypes.h:82
signed int s32
32 bit signed variable.
Definition irrTypes.h:66