5 #ifndef __IRR_STRING_H_INCLUDED__ 6 #define __IRR_STRING_H_INCLUDED__ 36 template <
typename T,
typename TAlloc = irrAllocator<T> >
48 static inline void locale_set (
eLocaleID id )
54 static inline u32 locale_lower (
u32 x )
56 switch ( locale_current )
63 return x >=
'A' &&
x <=
'Z' ?
x + 0x20 :
x;
67 static inline u32 locale_upper (
u32 x )
69 switch ( locale_current )
77 return x >=
'a' &&
x <=
'z' ?
x + (
'A' -
'a' ) :
x;
93 template <
typename T,
typename TAlloc>
102 :
array(0), allocated(1), used(1)
104 array = allocator.allocate(1);
111 :
array(0), allocated(0), used(0)
117 template <
class B,
class A>
119 :
array(0), allocated(0), used(0)
127 :
array(0), allocated(0), used(0)
137 :
array(0), allocated(0), used(0)
141 bool negative =
false;
167 tmpbuf[idx] = (
c8)(
'0' + (number % 10));
179 *
this = &tmpbuf[idx];
185 :
array(0), allocated(0), used(0)
206 tmpbuf[idx] = (
c8)(
'0' + (number % 10));
210 *
this = &tmpbuf[idx];
216 :
array(0), allocated(0), used(0)
220 bool negative =
false;
246 tmpbuf[idx] = (
c8)(
'0' + (number % 10));
258 *
this = &tmpbuf[idx];
264 :
array(0), allocated(0), used(0)
285 tmpbuf[idx] = (
c8)(
'0' + (number % 10));
289 *
this = &tmpbuf[idx];
296 :
array(0), allocated(0), used(0)
305 allocated = used =
length+1;
306 array = allocator.allocate(used);
318 :
array(0), allocated(0), used(0)
327 allocator.deallocate(
array);
337 used = other.size()+1;
340 allocator.deallocate(
array);
342 array = allocator.allocate(used);
345 const T*
p = other.c_str();
346 for (
u32 i=0; i<used; ++i, ++
p)
353 template <
class B,
class A>
356 *
this = other.c_str();
369 array = allocator.allocate(1);
377 if ((
void*)
c == (
void*)
array)
395 array = allocator.allocate(used);
398 for (
u32 l = 0; l<
len; ++l)
401 if (oldArray !=
array)
402 allocator.deallocate(oldArray);
452 for (i=0;
array[i] && str[i]; ++i)
453 if (
array[i] != str[i])
456 return (!
array[i] && !str[i]);
463 for (
u32 i=0;
array[i] && other.array[i]; ++i)
464 if (
array[i] != other.array[i])
467 return used == other.used;
474 for (
u32 i=0;
array[i] && other.array[i]; ++i)
476 const s32 diff =
array[i] - other.array[i];
481 return (used < other.used);
488 return !(*
this == str);
495 return !(*
this == other);
511 return (
size() == 0);
555 for(
u32 i=0;
array[i] && other[i]; ++i)
556 if (locale_lower(
array[i]) != locale_lower(other[i]))
559 return used == other.used;
568 if ( (
u32) sourcePos >= used )
572 for( i=0;
array[sourcePos + i] && other[i]; ++i)
573 if (locale_lower(
array[sourcePos + i]) != locale_lower(other[i]))
576 return array[sourcePos + i] == 0 && other[i] == 0;
585 for(
u32 i=0;
array[i] && other.array[i]; ++i)
587 s32 diff = (
s32) locale_lower (
array[i] ) - (
s32) locale_lower ( other.array[i] );
592 return used < other.used;
603 for(i=0; i <
n &&
array[i] && other[i]; ++i)
604 if (
array[i] != other[i])
609 return (i ==
n) || (used == other.used);
622 for(i=0; i <
n &&
array[i] && str[i]; ++i)
623 if (
array[i] != str[i])
628 return (i ==
n) || (
array[i] == 0 && str[i] == 0);
636 if (used + 1 > allocated)
637 reallocate(used + 1);
641 array[used-2] = character;
666 if (used +
len > allocated)
667 reallocate(used +
len);
673 array[l+used] = *(other+l);
685 if (other.size() == 0)
689 const u32 len = other.size()+1;
691 if (used +
len > allocated)
692 reallocate(used +
len);
695 array[used+l] = other[l];
708 if (other.size() == 0)
711 if (other.size() <
length)
717 if (used +
length > allocated)
718 reallocate(used +
length);
723 array[l+used] = other[l];
745 for (
u32 i=0; i<used-pos; ++i)
751 for (
u32 i=0; i<
n; ++i)
764 if (
count < allocated)
777 for (
u32 i=0; i<used-1; ++i)
796 for (
u32 i=0; i<used-1; ++i)
818 for (
u32 i=0; i<used-1; ++i)
821 for (j=0; j<
count; ++j)
845 for (
s32 i=(
s32)(used-2); i>=0; --i)
848 for (j=0; j<
count; ++j)
866 for (
u32 i=startPos; i<used-1; ++i)
901 for (
s32 i=(
s32)used-2; i>=0; --i)
932 while(str[j] &&
array[i+j] == str[j])
964 o.array[i] =
array[i+begin];
969 o.array[i] = locale_lower (
array[i+begin] );
1065 for (
u32 i=0; i<used-1; ++i)
1066 if (
array[i] == toReplace)
1067 array[i] = replaceWith;
1077 if (toReplace.size() == 0)
1080 const T* other = toReplace.c_str();
1081 const T*
replace = replaceWith.c_str();
1082 const u32 other_size = toReplace.size();
1083 const u32 replace_size = replaceWith.size();
1086 s32 delta = replace_size - other_size;
1092 while ((pos =
find(other, pos)) != -1)
1094 for (
u32 i = 0; i < replace_size; ++i)
1105 for (
u32 pos = 0; pos < used; ++i, ++pos)
1108 if (
array[pos] == *other)
1112 for (j = 0; j < other_size; ++j)
1114 if (
array[pos + j] != other[j])
1119 if (j == other_size)
1121 for (j = 0; j < replace_size; ++j)
1123 i += replace_size - 1;
1124 pos += other_size - 1;
1142 while ((pos =
find(other, pos)) != -1)
1149 u32 len = delta * find_count;
1150 if (used +
len > allocated)
1151 reallocate(used +
len);
1155 while ((pos =
find(other, pos)) != -1)
1158 T* ptr =
array + used - 1;
1162 while (ptr !=
start)
1170 for (
u32 i = 0; i < replace_size; ++i)
1173 pos += replace_size;
1187 for (
u32 i=0; i<used-1; ++i)
1212 for (
u32 i=0; i<used-1; ++i)
1217 if (
array[i + j] != toRemove[j])
1240 if (characters.size() == 0)
1245 for (
u32 i=0; i<used-1; ++i)
1249 bool docontinue =
false;
1250 for (
u32 j=0; j<characters.size(); ++j)
1252 if (characters[j] ==
array[i])
1298 if ( i > 0 && (
u32)i < used-2 )
1305 if (
array[i] == decimalPoint )
1308 else if (
array[i] == decimalPoint )
1315 array[eraseStart] = 0;
1316 used = eraseStart+1;
1341 for (
u32 i=0; i<allocated; ++i)
1351 if ( allocated > 0 )
1367 return used > 1 ?
array[used-2] : 0;
1389 template<
class container>
1390 u32 split(container& ret,
const T*
const delimiter,
u32 countDelimiters=1,
bool ignoreEmptyTokens=
true,
bool keepSeparators=
false)
const 1395 const u32 oldSize=ret.size();
1397 u32 tokenStartIdx = 0;
1398 for (
u32 i=0; i<used; ++i)
1400 for (
u32 j=0; j<countDelimiters; ++j)
1402 if (
array[i] == delimiter[j])
1404 if (i - tokenStartIdx > 0)
1406 else if ( !ignoreEmptyTokens )
1408 if ( keepSeparators )
1413 tokenStartIdx = i+1;
1418 if ((used - 1) > tokenStartIdx)
1420 else if ( !ignoreEmptyTokens )
1423 return ret.size()-oldSize;
1431 void reallocate(
u32 new_size)
1433 T* old_array =
array;
1435 array = allocator.allocate(new_size);
1436 allocated = new_size;
1438 const u32 amount = used < new_size ? used : new_size;
1439 for (
u32 i=0; i<amount; ++i)
1440 array[i] = old_array[i];
1442 if (allocated < used)
1445 allocator.deallocate(old_array);
1471 return multibyteToWString(destination,
source.c_str(), (
u32)
source.size());
1483 return multibyteToWString(destination,
source,
s);
1491 destination.reserve(sourceSize+1);
1492 #if defined(_MSC_VER) 1493 #pragma warning(push) 1494 #pragma warning(disable: 4996) // 'mbstowcs': This function or variable may be unsafe. Consider using mbstowcs_s instead. 1496 const size_t written = mbstowcs(destination.array,
source, (
size_t)sourceSize);
1497 #if defined(_MSC_VER) 1498 #pragma warning(pop) 1500 if ( written != (
size_t)-1 )
1502 destination.used = (
u32)written+1;
1503 destination.array[destination.used-1] = 0;
1509 destination.clear();
1515 destination.clear();
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.
unsigned long long u64
64 bit unsigned variable.
IRRLICHT_API void utf8ToWchar(const char *in, wchar_t *out, const u64 len)
Convert this utf-8-encoded string to the platform's wchar.
u32 split(container &ret, const T *const delimiter, u32 countDelimiters=1, bool ignoreEmptyTokens=true, bool keepSeparators=false) const
Split string into parts (tokens).
T & operator [](const u32 index)
Direct access operator.
u32 size() const
Returns length of the string's content.
GLsizei const GLchar *const * string
string< T, TAlloc > & append(T character)
Appends a character to this string.
string< T, TAlloc > & remove(T c)
Removes characters from a 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
GLuint GLuint GLsizei count
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
#define IRRLICHT_API
Set FPU settings.
s32 isdigit(s32 c)
some standard function ( to remove dependencies )
string< T, TAlloc > & operator=(const string< B, A > &other)
Assignment operator for other string types.
const T & operator [](const u32 index) const
Direct access operator.
Everything in the Irrlicht Engine can be found in this namespace.
Very simple string class with some useful features.
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.
string< T, TAlloc > & eraseTrailingFloatZeros(char decimalPoint='.')
Erase 0's at the end when a string ends with a floating point number.
string< T, TAlloc > & trim(const string< T, TAlloc > &whitespace=" \t\n\r")
Trims the string.
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.
void clear(bool releaseMemory=true)
const T * c_str() const
Returns character string.
unsigned int u32
32 bit unsigned variable.
string(unsigned long number)
Constructs a string from an unsigned long.
IRRLICHT_API void wcharToUtf8(const wchar_t *in, char *out, const u64 len)
Convert this wchar string to utf-8.
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.
GLsizei GLsizei GLchar * source
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.
string< T, TAlloc > & remove(const string< T, TAlloc > &toRemove)
Removes a string from the string.
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.
bool operator<(const string< T, TAlloc > &other) const
Is smaller comparator.
string< T > subString(u32 begin, s32 length, bool make_lower=false) const
Returns a substring.
string< T, TAlloc > & insert(u32 pos, const char *s, u32 n)
Insert a certain amount of characters into the string before the given index.
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
GLint GLint GLint GLint GLint x
string(const string< T, TAlloc > &other)
Constructor.
friend size_t multibyteToWString(string< wchar_t > &destination, const char *source, u32 sourceSize)
Internally used by the other multibyteToWString functions.
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.
GLuint GLsizei GLsizei * length
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.
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.