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> >
79 :
array(0), allocated(1), used(1)
81 array = allocator.allocate(1);
88 :
array(0), allocated(0), used(0)
94 template <
class B,
class A>
96 :
array(0), allocated(0), used(0)
104 :
array(0), allocated(0), used(0)
107 snprintf(tmpbuf, 255,
"%0.6f", number);
114 :
array(0), allocated(0), used(0)
118 bool negative =
false;
144 tmpbuf[idx] = (
c8)(
'0' + (number % 10));
156 *
this = &tmpbuf[idx];
162 :
array(0), allocated(0), used(0)
183 tmpbuf[idx] = (
c8)(
'0' + (number % 10));
187 *
this = &tmpbuf[idx];
194 :
array(0), allocated(0), used(0)
203 allocated = used = length+1;
204 array = allocator.allocate(used);
206 for (
u32 l = 0; l<length; ++l)
216 :
array(0), allocated(0), used(0)
225 allocator.deallocate(
array);
235 used = other.
size()+1;
238 allocator.deallocate(
array);
240 array = allocator.allocate(used);
243 const T* p = other.
c_str();
244 for (
u32 i=0; i<used; ++i, ++p)
251 template <
class B,
class A>
254 *
this = other.
c_str();
267 array = allocator.allocate(1);
275 if ((
void*)c == (
void*)
array)
293 array = allocator.allocate(used);
296 for (
u32 l = 0; l<len; ++l)
299 if (oldArray !=
array)
300 allocator.deallocate(oldArray);
350 for(i=0;
array[i] && str[i]; ++i)
351 if (
array[i] != str[i])
354 return !
array[i] && !str[i];
361 for(
u32 i=0;
array[i] && other.array[i]; ++i)
362 if (
array[i] != other.array[i])
365 return used == other.used;
370 bool operator <(const string<T,TAlloc>& other)
const
379 return used < other.used;
386 return !(*
this == str);
393 return !(*
this == other);
417 for (
u32 i=0; i<used; ++i)
425 for (
u32 i=0; i<used; ++i)
435 for(
u32 i=0;
array[i] && other[i]; ++i)
436 if (locale_lower(
array[i]) != locale_lower(other[i]))
439 return used == other.used;
448 if ( (
u32) sourcePos > used )
452 for( i=0;
array[sourcePos + i] && other[i]; ++i)
453 if (locale_lower(
array[sourcePos + i]) != locale_lower(other[i]))
456 return array[sourcePos + i] == 0 && other[i] == 0;
465 for(
u32 i=0;
array[i] && other.array[i]; ++i)
467 s32 diff = (
s32) locale_lower (
array[i] ) - (
s32) locale_lower ( other.array[i] );
472 return used < other.used;
483 for(i=0;
array[i] && other[i] && i < n; ++i)
484 if (
array[i] != other[i])
489 return (i == n) || (used == other.used);
502 for(i=0;
array[i] && str[i] && i < n; ++i)
503 if (
array[i] != str[i])
508 return (i == n) || (
array[i] == 0 && str[i] == 0);
516 if (used + 1 > allocated)
517 reallocate(used + 1);
521 array[used-2] = character;
541 if (used + len > allocated)
542 reallocate(used + len);
547 for (
u32 l=0; l<len; ++l)
548 array[l+used] = *(other+l);
561 if (used + len > allocated)
562 reallocate(used + len);
564 for (
u32 l=0; l<len; ++l)
565 array[used+l] = other[l];
576 if (other.
size() < length)
582 if (used + length > allocated)
583 reallocate(used + length);
587 for (
u32 l=0; l<length; ++l)
588 array[l+used] = other[l];
601 if (count < allocated)
614 for (
u32 i=0; i<used; ++i)
633 for (
u32 i=0; i<used; ++i)
634 for (
u32 j=0; j<count; ++j)
635 if (
array[i] == c[j])
652 for (
u32 i=0; i<used-1; ++i)
655 for (j=0; j<count; ++j)
656 if (
array[i] == c[j])
676 for (
s32 i=(
s32)(used-2); i>=0; --i)
679 for (j=0; j<count; ++j)
680 if (
array[i] == c[j])
697 for (
u32 i=startPos; i<used; ++i)
713 for (
s32 i=start; i>=0; --i)
732 for (
s32 i=used-1; i>=0; --i)
733 for (
u32 j=0; j<count; ++j)
734 if (
array[i] == c[j])
759 for (
u32 i=start; i<used-len; ++i)
763 while(str[j] &&
array[i+j] == str[j])
782 if ((length <= 0) || (begin>=
size()))
785 if ((length+begin) >
size())
786 length =
size()-begin;
791 for (
s32 i=0; i<length; ++i)
792 o.array[i] =
array[i+begin];
795 o.used = o.allocated;
887 for (
u32 i=0; i<used; ++i)
888 if (
array[i] == toReplace)
889 array[i] = replaceWith;
899 for (
u32 i=0; i<used; ++i)
923 for (
u32 i=0; i<used; ++i)
928 if (
array[i + j] != toRemove[j])
952 for (
u32 i=0; i<used; ++i)
956 bool docontinue =
false;
957 for (
u32 j=0; j<characters.
size(); ++j)
959 if (characters[j] ==
array[i])
988 return (*
this =
subString(begin, (end +1) - begin));
1000 for (
u32 i=index+1; i<used; ++i)
1010 for (
u32 i=0; i<allocated; ++i)
1020 if ( allocated > 0 )
1034 return used > 1 ?
array[used-2] : 0;
1055 template<
class container>
1056 u32 split(container& ret,
const T*
const c,
u32 count=1,
bool ignoreEmptyTokens=
true,
bool keepSeparators=
false)
const
1061 const u32 oldSize=ret.size();
1063 bool lastWasSeparator =
false;
1064 for (
u32 i=0; i<used; ++i)
1066 bool foundSeparator =
false;
1067 for (
u32 j=0; j<count; ++j)
1069 if (
array[i] == c[j])
1071 if ((!ignoreEmptyTokens || i - lastpos != 0) &&
1074 foundSeparator =
true;
1075 lastpos = (keepSeparators ? i : i + 1);
1079 lastWasSeparator = foundSeparator;
1081 if ((used - 1) > lastpos)
1083 return ret.size()-oldSize;
1089 void reallocate(
u32 new_size)
1091 T* old_array =
array;
1093 array = allocator.allocate(new_size);
1094 allocated = new_size;
1096 u32 amount = used < new_size ? used : new_size;
1097 for (
u32 i=0; i<amount; ++i)
1098 array[i] = old_array[i];
1100 if (allocated < used)
1103 allocator.deallocate(old_array);