5 #ifndef __IRR_STRING_H_INCLUDED__ 6 #define __IRR_STRING_H_INCLUDED__ 39 static inline void locale_set (
eLocaleID id )
45 static inline u32 locale_lower (
u32 x )
47 switch ( locale_current )
54 return x >=
'A' && x <=
'Z' ? x + 0x20 : x;
58 static inline u32 locale_upper (
u32 x )
60 switch ( locale_current )
68 return x >=
'a' && x <=
'z' ? x + (
'A' -
'a' ) : x;
72 template <
typename T,
typename TAlloc = irrAllocator<T> >
81 :
array(0), allocated(1), used(1)
83 array = allocator.allocate(1);
90 :
array(0), allocated(0), used(0)
96 template <
class B,
class A>
98 :
array(0), allocated(0), used(0)
106 :
array(0), allocated(0), used(0)
109 snprintf(tmpbuf, 255,
"%0.6f", number);
116 :
array(0), allocated(0), used(0)
120 bool negative =
false;
146 tmpbuf[idx] = (
c8)(
'0' + (number % 10));
158 *
this = &tmpbuf[idx];
164 :
array(0), allocated(0), used(0)
185 tmpbuf[idx] = (
c8)(
'0' + (number % 10));
189 *
this = &tmpbuf[idx];
195 :
array(0), allocated(0), used(0)
199 bool negative =
false;
225 tmpbuf[idx] = (
c8)(
'0' + (number % 10));
237 *
this = &tmpbuf[idx];
243 :
array(0), allocated(0), used(0)
264 tmpbuf[idx] = (
c8)(
'0' + (number % 10));
268 *
this = &tmpbuf[idx];
275 :
array(0), allocated(0), used(0)
284 allocated = used = length+1;
285 array = allocator.allocate(used);
287 for (
u32 l = 0; l<length; ++l)
297 :
array(0), allocated(0), used(0)
306 allocator.deallocate(
array);
316 used = other.
size()+1;
319 allocator.deallocate(
array);
321 array = allocator.allocate(used);
324 const T* p = other.
c_str();
325 for (
u32 i=0; i<used; ++i, ++p)
332 template <
class B,
class A>
335 *
this = other.
c_str();
348 array = allocator.allocate(1);
356 if ((
void*)c == (
void*)
array)
374 array = allocator.allocate(used);
377 for (
u32 l = 0; l<len; ++l)
380 if (oldArray !=
array)
381 allocator.deallocate(oldArray);
431 for (i=0;
array[i] && str[i]; ++i)
432 if (
array[i] != str[i])
435 return (!
array[i] && !str[i]);
442 for (
u32 i=0;
array[i] && other.array[i]; ++i)
443 if (
array[i] != other.array[i])
446 return used == other.used;
451 bool operator<(const string<T,TAlloc>& other)
const 460 return (used < other.used);
467 return !(*
this == str);
474 return !(*
this == other);
490 return (
size() == 0);
524 for(
u32 i=0;
array[i] && other[i]; ++i)
525 if (locale_lower(
array[i]) != locale_lower(other[i]))
528 return used == other.used;
537 if ( (
u32) sourcePos >= used )
541 for( i=0;
array[sourcePos + i] && other[i]; ++i)
542 if (locale_lower(
array[sourcePos + i]) != locale_lower(other[i]))
545 return array[sourcePos + i] == 0 && other[i] == 0;
554 for(
u32 i=0;
array[i] && other.array[i]; ++i)
556 s32 diff = (
s32) locale_lower (
array[i] ) - (
s32) locale_lower ( other.array[i] );
561 return used < other.used;
572 for(i=0;
array[i] && other[i] && i < n; ++i)
573 if (
array[i] != other[i])
578 return (i == n) || (used == other.used);
591 for(i=0;
array[i] && str[i] && i < n; ++i)
592 if (
array[i] != str[i])
597 return (i == n) || (
array[i] == 0 && str[i] == 0);
605 if (used + 1 > allocated)
606 reallocate(used + 1);
610 array[used-2] = character;
635 if (used + len > allocated)
636 reallocate(used + len);
641 for (
u32 l=0; l<len; ++l)
642 array[l+used] = *(other+l);
654 if (other.
size() == 0)
660 if (used + len > allocated)
661 reallocate(used + len);
663 for (
u32 l=0; l<len; ++l)
664 array[used+l] = other[l];
677 if (other.
size() == 0)
680 if (other.
size() < length)
686 if (used + length > allocated)
687 reallocate(used + length);
691 for (
u32 l=0; l<length; ++l)
692 array[l+used] = other[l];
707 if (count < allocated)
720 for (
u32 i=0; i<used-1; ++i)
739 for (
u32 i=0; i<used-1; ++i)
740 for (
u32 j=0; j<count; ++j)
741 if (
array[i] == c[j])
761 for (
u32 i=0; i<used-1; ++i)
764 for (j=0; j<count; ++j)
765 if (
array[i] == c[j])
788 for (
s32 i=(
s32)(used-2); i>=0; --i)
791 for (j=0; j<count; ++j)
792 if (
array[i] == c[j])
809 for (
u32 i=startPos; i<used-1; ++i)
825 for (
s32 i=start; i>=0; --i)
844 for (
s32 i=(
s32)used-2; i>=0; --i)
845 for (
u32 j=0; j<count; ++j)
846 if (
array[i] == c[j])
871 for (
u32 i=start; i<used-len; ++i)
875 while(str[j] &&
array[i+j] == str[j])
895 if ((length <= 0) || (begin>=
size()))
898 if ((length+begin) >
size())
899 length =
size()-begin;
907 for (i=0; i<length; ++i)
908 o.array[i] =
array[i+begin];
912 for (i=0; i<length; ++i)
913 o.array[i] = locale_lower (
array[i+begin] );
1009 for (
u32 i=0; i<used-1; ++i)
1010 if (
array[i] == toReplace)
1011 array[i] = replaceWith;
1021 if (toReplace.
size() == 0)
1024 const T* other = toReplace.
c_str();
1026 const u32 other_size = toReplace.
size();
1027 const u32 replace_size = replaceWith.
size();
1030 s32 delta = replace_size - other_size;
1036 while ((pos =
find(other, pos)) != -1)
1038 for (
u32 i = 0; i < replace_size; ++i)
1039 array[pos + i] = replace[i];
1049 for (
u32 pos = 0; pos < used; ++i, ++pos)
1052 if (
array[pos] == *other)
1056 for (j = 0; j < other_size; ++j)
1058 if (
array[pos + j] != other[j])
1063 if (j == other_size)
1065 for (j = 0; j < replace_size; ++j)
1066 array[i + j] = replace[j];
1067 i += replace_size - 1;
1068 pos += other_size - 1;
1086 while ((pos =
find(other, pos)) != -1)
1093 u32 len = delta * find_count;
1094 if (used + len > allocated)
1095 reallocate(used + len);
1099 while ((pos =
find(other, pos)) != -1)
1101 T* start =
array + pos + other_size - 1;
1102 T* ptr =
array + used - 1;
1103 T* end =
array + delta + used -1;
1106 while (ptr != start)
1114 for (
u32 i = 0; i < replace_size; ++i)
1115 array[pos + i] = replace[i];
1117 pos += replace_size;
1131 for (
u32 i=0; i<used-1; ++i)
1156 for (
u32 i=0; i<used-1; ++i)
1161 if (
array[i + j] != toRemove[j])
1184 if (characters.
size() == 0)
1189 for (
u32 i=0; i<used-1; ++i)
1193 bool docontinue =
false;
1194 for (
u32 j=0; j<characters.
size(); ++j)
1196 if (characters[j] ==
array[i])
1227 return (*
this =
subString(begin, (end +1) - begin));
1239 for (
u32 i=index+1; i<used; ++i)
1250 for (
u32 i=0; i<allocated; ++i)
1260 if ( allocated > 0 )
1276 return used > 1 ?
array[used-2] : 0;
1297 template<
class container>
1298 u32 split(container& ret,
const T*
const c,
u32 count=1,
bool ignoreEmptyTokens=
true,
bool keepSeparators=
false)
const 1303 const u32 oldSize=ret.size();
1305 bool lastWasSeparator =
false;
1306 for (
u32 i=0; i<used; ++i)
1308 bool foundSeparator =
false;
1309 for (
u32 j=0; j<count; ++j)
1311 if (
array[i] == c[j])
1313 if ((!ignoreEmptyTokens || i - lastpos != 0) &&
1316 foundSeparator =
true;
1317 lastpos = (keepSeparators ? i : i + 1);
1321 lastWasSeparator = foundSeparator;
1323 if ((used - 1) > lastpos)
1325 return ret.size()-oldSize;
1331 void reallocate(
u32 new_size)
1333 T* old_array =
array;
1335 array = allocator.allocate(new_size);
1336 allocated = new_size;
1338 u32 amount = used < new_size ? used : new_size;
1339 for (
u32 i=0; i<amount; ++i)
1340 array[i] = old_array[i];
1342 if (allocated < used)
1345 allocator.deallocate(old_array);
string(long number)
Constructs a string from a long.
s32 findFirstCharNotInList(const B *const c, u32 count=1) const
Finds first position of a character not in a given list.
T & operator[](const u32 index)
Direct access operator.
u32 size() const
Returns length of the string's content.
array()
Default constructor for empty array.
string< T, TAlloc > & append(T character)
Appends a character to this string.
bool equals_substring_ignore_case(const string< T, TAlloc > &other, const s32 sourcePos=0) const
Compares the strings ignoring case.
s32 findLast(T c, s32 start=-1) const
finds last occurrence of character in string
string(const B *const c)
Constructor for unicode and ascii strings.
string< T, TAlloc > operator+(const B *const c) const
Append operator for strings, ascii and unicode.
char c8
8 bit character variable.
string(const double number)
Constructs a string from a float.
s32 find(const B *const str, const u32 start=0) const
finds another string in this string
string< T, TAlloc > & operator=(const string< B, A > &other)
Assignment operator for other string types.
Everything in the Irrlicht Engine can be found in this namespace.
void push_back(const T &element)
Adds an element at back of array.
string< T, TAlloc > & operator=(const string< T, TAlloc > &other)
Assignment operator.
bool operator==(const string< T, TAlloc > &other) const
Equality operator.
bool operator!=(const T *const str) const
Inequality operator.
void reserve(u32 count)
Reserves some memory.
s32 findLastChar(const T *const c, u32 count=1) const
finds last occurrence of a character of a list in string
s32 findFirst(T c) const
finds first occurrence of character in string
string< T, TAlloc > & make_upper()
Makes the string upper case.
string(unsigned int number)
Constructs a string from an unsigned int.
s32 findLastCharNotInList(const B *const c, u32 count=1) const
Finds last position of a character not in a given list.
signed int s32
32 bit signed variable.
string< T, TAlloc > & append(const string< T, TAlloc > &other, u32 length)
Appends a string of the length l to this string.
string< T, TAlloc > & replace(const string< T, TAlloc > &toReplace, const string< T, TAlloc > &replaceWith)
Replaces all instances of a string with another one.
const T * c_str() const
Returns character string.
u32 split(container &ret, const T *const c, u32 count=1, bool ignoreEmptyTokens=true, bool keepSeparators=false) const
split string into parts.
unsigned int u32
32 bit unsigned variable.
string(unsigned long number)
Constructs a string from an unsigned long.
bool operator==(const T *const str) const
Equality operator.
string< T, TAlloc > & make_lower()
Makes the string lower case.
T lastChar() const
gets the last char of a string or null
string< T, TAlloc > & operator=(const B *const c)
Assignment operator for strings, ascii and unicode.
string< T, TAlloc > & removeChars(const string< T, TAlloc > &characters)
Removes characters from a string.
#define _IRR_DEBUG_BREAK_IF(_CONDITION_)
define a break macro for debugging.
string< c8 > stringc
Typedef for character strings.
bool equalsn(const T *const str, u32 n) const
compares the first n characters of the strings
string< T, TAlloc > & replace(T toReplace, T replaceWith)
Replaces all characters of a special type with another one.
string()
Default constructor.
string(const string< B, A > &other)
Constructor from other string types.
string< T, TAlloc > & append(const T *const other, u32 length=0xffffffff)
Appends a char string to this string.
string< T > subString(u32 begin, s32 length, bool make_lower=false) const
Returns a substring.
Self reallocating template array (like stl vector) with additional features.
s32 findFirstChar(const T *const c, u32 count=1) const
finds first occurrence of a character of a list in string
string(const string< T, TAlloc > &other)
Constructor.
bool equals_ignore_case(const string< T, TAlloc > &other) const
Compares the strings ignoring case.
bool lower_ignore_case(const string< T, TAlloc > &other) const
Compares the strings ignoring case.
bool equalsn(const string< T, TAlloc > &other, u32 n) const
compares the first n characters of the strings
string< T, TAlloc > & operator+=(T c)
Appends a character to this string.
s32 findNext(T c, u32 startPos) const
finds next occurrence of character in string
string< T, TAlloc > & append(const string< T, TAlloc > &other)
Appends a string to this string.
string< T, TAlloc > & validate()
verify the existing string.
bool operator!=(const string< T, TAlloc > &other) const
Inequality operator.
string(const B *const c, u32 length)
Constructor for copying a string from a pointer with a given length.
string< T, TAlloc > operator+(const string< T, TAlloc > &other) const
Append operator for other strings.
eLocaleID
Very simple string class with some useful features.
string< T, TAlloc > & trim(const string< T, TAlloc > &whitespace=" \\)
Trims the string.
const T clamp(const T &value, const T &low, const T &high)
clamps a value between low and high
string< T, TAlloc > & erase(u32 index)
Erases a character from the string.
string(int number)
Constructs a string from an int.
string< wchar_t > stringw
Typedef for wide character strings.