1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine" and the "irrXML" project.
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
\r
5 #ifndef __IRR_STRING_H_INCLUDED__
\r
6 #define __IRR_STRING_H_INCLUDED__
\r
8 #include "irrTypes.h"
\r
9 #include "irrAllocator.h"
\r
10 #include "irrMath.h"
\r
21 //! Very simple string class with some useful features.
\r
22 /** string<c8> and string<wchar_t> both accept Unicode AND ASCII/Latin-1,
\r
23 so you can assign Unicode to string<c8> and ASCII/Latin-1 to string<wchar_t>
\r
24 (and the other way round) if you want to.
\r
26 However, note that the conversation between both is not done using any encoding.
\r
27 This means that c8 strings are treated as ASCII/Latin-1, not UTF-8, and
\r
28 are simply expanded to the equivalent wchar_t, while Unicode/wchar_t
\r
29 characters are truncated to 8-bit ASCII/Latin-1 characters, discarding all
\r
30 other information in the wchar_t.
\r
32 Helper functions for converting between UTF-8 and wchar_t are provided
\r
33 outside the string class for explicit use.
\r
36 // forward declarations
\r
37 template <typename T, typename TAlloc = irrAllocator<T> >
\r
39 static size_t multibyteToWString(string<wchar_t>& destination, const char* source, u32 sourceSize);
\r
40 static size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source, u32 sourceSize);
\r
41 inline bool isdigit(s32 c);
\r
43 //! Returns a character converted to lower case
\r
44 static inline u32 locale_lower ( u32 x )
\r
47 return x >= 'A' && x <= 'Z' ? x + 0x20 : x;
\r
50 //! Returns a character converted to upper case
\r
51 static inline u32 locale_upper ( u32 x )
\r
54 return x >= 'a' && x <= 'z' ? x + ( 'A' - 'a' ) : x;
\r
58 template <typename T, typename TAlloc>
\r
63 typedef T char_type;
\r
65 //! Default constructor
\r
67 : array(0), allocated(1), used(1)
\r
69 array = allocator.allocate(1); // new T[1];
\r
75 string(const string<T,TAlloc>& other)
\r
76 : array(0), allocated(0), used(0)
\r
81 //! Constructor from other string types
\r
82 template <class B, class A>
\r
83 string(const string<B, A>& other)
\r
84 : array(0), allocated(0), used(0)
\r
90 //! Constructs a string from a float
\r
91 explicit string(const double number)
\r
92 : array(0), allocated(0), used(0)
\r
95 snprintf_irr(tmpbuf, 255, "%0.6f", number);
\r
100 //! Constructs a string from an int
\r
101 explicit string(int number)
\r
102 : array(0), allocated(0), used(0)
\r
104 // store if negative and make positive
\r
106 bool negative = false;
\r
113 // temporary buffer for 16 numbers
\r
118 // special case '0'
\r
123 *this = &tmpbuf[14];
\r
129 while(number && idx)
\r
132 tmpbuf[idx] = (c8)('0' + (number % 10));
\r
144 *this = &tmpbuf[idx];
\r
148 //! Constructs a string from an unsigned int
\r
149 explicit string(unsigned int number)
\r
150 : array(0), allocated(0), used(0)
\r
152 // temporary buffer for 16 numbers
\r
157 // special case '0'
\r
162 *this = &tmpbuf[14];
\r
168 while(number && idx)
\r
171 tmpbuf[idx] = (c8)('0' + (number % 10));
\r
175 *this = &tmpbuf[idx];
\r
179 //! Constructs a string from a long
\r
180 explicit string(long number)
\r
181 : array(0), allocated(0), used(0)
\r
183 // store if negative and make positive
\r
185 bool negative = false;
\r
192 // temporary buffer for 16 numbers
\r
197 // special case '0'
\r
202 *this = &tmpbuf[14];
\r
208 while(number && idx)
\r
211 tmpbuf[idx] = (c8)('0' + (number % 10));
\r
223 *this = &tmpbuf[idx];
\r
227 //! Constructs a string from an unsigned long
\r
228 explicit string(unsigned long number)
\r
229 : array(0), allocated(0), used(0)
\r
231 // temporary buffer for 16 numbers
\r
236 // special case '0'
\r
241 *this = &tmpbuf[14];
\r
247 while(number && idx)
\r
250 tmpbuf[idx] = (c8)('0' + (number % 10));
\r
254 *this = &tmpbuf[idx];
\r
258 //! Constructor for copying a string from a pointer with a given length
\r
260 string(const B* const c, u32 length)
\r
261 : array(0), allocated(0), used(0)
\r
265 // correctly init the string to an empty one
\r
270 allocated = used = length+1;
\r
271 array = allocator.allocate(used); // new T[used];
\r
273 for (u32 l = 0; l<length; ++l)
\r
274 array[l] = (T)c[l];
\r
280 //! Constructor for Unicode and ASCII strings
\r
282 string(const B* const c)
\r
283 : array(0), allocated(0), used(0)
\r
292 allocator.deallocate(array); // delete [] array;
\r
296 //! Assignment operator
\r
297 string<T,TAlloc>& operator=(const string<T,TAlloc>& other)
\r
299 if (this == &other)
\r
302 used = other.size()+1;
\r
303 if (used>allocated)
\r
305 allocator.deallocate(array); // delete [] array;
\r
307 array = allocator.allocate(used); //new T[used];
\r
310 const T* p = other.c_str();
\r
311 for (u32 i=0; i<used; ++i, ++p)
\r
317 //! Assignment operator for other string types
\r
318 template <class B, class A>
\r
319 string<T,TAlloc>& operator=(const string<B,A>& other)
\r
321 *this = other.c_str();
\r
326 //! Assignment operator for strings, ASCII and Unicode
\r
328 string<T,TAlloc>& operator=(const B* const c)
\r
334 array = allocator.allocate(1); //new T[1];
\r
342 if ((void*)c == (void*)array)
\r
352 // we'll keep the old string for a while, because the new
\r
353 // string could be a part of the current string.
\r
354 T* oldArray = array;
\r
357 if (used>allocated)
\r
360 array = allocator.allocate(used); //new T[used];
\r
363 for (u32 l = 0; l<len; ++l)
\r
364 array[l] = (T)c[l];
\r
366 if (oldArray != array)
\r
367 allocator.deallocate(oldArray); // delete [] oldArray;
\r
373 //! Append operator for other strings
\r
374 string<T,TAlloc> operator+(const string<T,TAlloc>& other) const
\r
376 string<T,TAlloc> str(*this);
\r
383 //! Append operator for strings, ASCII and Unicode
\r
385 string<T,TAlloc> operator+(const B* const c) const
\r
387 string<T,TAlloc> str(*this);
\r
394 //! Direct access operator
\r
395 T& operator [](const u32 index)
\r
397 _IRR_DEBUG_BREAK_IF(index>=used) // bad index
\r
398 return array[index];
\r
402 //! Direct access operator
\r
403 const T& operator [](const u32 index) const
\r
405 _IRR_DEBUG_BREAK_IF(index>=used) // bad index
\r
406 return array[index];
\r
410 //! Equality operator
\r
411 bool operator==(const T* const str) const
\r
417 for (i=0; array[i] && str[i]; ++i)
\r
418 if (array[i] != str[i])
\r
421 return (!array[i] && !str[i]);
\r
425 //! Equality operator
\r
426 bool operator==(const string<T,TAlloc>& other) const
\r
428 for (u32 i=0; array[i] && other.array[i]; ++i)
\r
429 if (array[i] != other.array[i])
\r
432 return used == other.used;
\r
436 //! Is smaller comparator
\r
437 bool operator<(const string<T,TAlloc>& other) const
\r
439 for (u32 i=0; array[i] && other.array[i]; ++i)
\r
441 const s32 diff = array[i] - other.array[i];
\r
446 return (used < other.used);
\r
450 //! Inequality operator
\r
451 bool operator!=(const T* const str) const
\r
453 return !(*this == str);
\r
457 //! Inequality operator
\r
458 bool operator!=(const string<T,TAlloc>& other) const
\r
460 return !(*this == other);
\r
464 //! Returns length of the string's content
\r
465 /** \return Length of the string's content in characters, excluding
\r
466 the trailing NUL. */
\r
472 //! Informs if the string is empty or not.
\r
473 //! \return True if the string is empty, false if not.
\r
476 return (size() == 0);
\r
479 void clear(bool releaseMemory=true)
\r
481 if ( releaseMemory )
\r
489 //! Returns character string
\r
490 /** \return pointer to C-style NUL terminated string. */
\r
491 const T* c_str() const
\r
497 //! Makes the string lower case.
\r
498 string<T,TAlloc>& make_lower()
\r
500 for (u32 i=0; array[i]; ++i)
\r
501 array[i] = locale_lower ( array[i] );
\r
506 //! Makes the string upper case.
\r
507 string<T,TAlloc>& make_upper()
\r
509 for (u32 i=0; array[i]; ++i)
\r
510 array[i] = locale_upper ( array[i] );
\r
515 //! Compares the strings ignoring case.
\r
516 /** \param other: Other string to compare.
\r
517 \return True if the strings are equal ignoring case. */
\r
518 bool equals_ignore_case(const string<T,TAlloc>& other) const
\r
520 for(u32 i=0; array[i] && other[i]; ++i)
\r
521 if (locale_lower( array[i]) != locale_lower(other[i]))
\r
524 return used == other.used;
\r
527 //! Compares the strings ignoring case.
\r
528 /** \param other: Other string to compare.
\r
529 \param sourcePos: where to start to compare in the string
\r
530 \return True if the strings are equal ignoring case. */
\r
531 bool equals_substring_ignore_case(const string<T,TAlloc>&other, const s32 sourcePos = 0 ) const
\r
533 if ( (u32) sourcePos >= used )
\r
537 for( i=0; array[sourcePos + i] && other[i]; ++i)
\r
538 if (locale_lower( array[sourcePos + i]) != locale_lower(other[i]))
\r
541 return array[sourcePos + i] == 0 && other[i] == 0;
\r
545 //! Compares the strings ignoring case.
\r
546 /** \param other: Other string to compare.
\r
547 \return True if this string is smaller ignoring case. */
\r
548 bool lower_ignore_case(const string<T,TAlloc>& other) const
\r
550 for(u32 i=0; array[i] && other.array[i]; ++i)
\r
552 s32 diff = (s32) locale_lower ( array[i] ) - (s32) locale_lower ( other.array[i] );
\r
557 return used < other.used;
\r
561 //! compares the first n characters of the strings
\r
562 /** \param other Other string to compare.
\r
563 \param n Number of characters to compare
\r
564 \return True if the n first characters of both strings are equal. */
\r
565 bool equalsn(const string<T,TAlloc>& other, u32 n) const
\r
568 for(i=0; i < n && array[i] && other[i]; ++i)
\r
569 if (array[i] != other[i])
\r
572 // if one (or both) of the strings was smaller then they
\r
573 // are only equal if they have the same length
\r
574 return (i == n) || (used == other.used);
\r
578 //! compares the first n characters of the strings
\r
579 /** \param str Other string to compare.
\r
580 \param n Number of characters to compare
\r
581 \return True if the n first characters of both strings are equal. */
\r
582 bool equalsn(const T* const str, u32 n) const
\r
587 for(i=0; i < n && array[i] && str[i]; ++i)
\r
588 if (array[i] != str[i])
\r
591 // if one (or both) of the strings was smaller then they
\r
592 // are only equal if they have the same length
\r
593 return (i == n) || (array[i] == 0 && str[i] == 0);
\r
597 //! Appends a character to this string
\r
598 /** \param character: Character to append. */
\r
599 string<T,TAlloc>& append(T character)
\r
601 if (used + 1 > allocated)
\r
602 reallocate(used + 1);
\r
606 array[used-2] = character;
\r
613 //! Appends a char string to this string
\r
614 /** \param other: Char string to append. */
\r
615 /** \param length: The length of the string to append. */
\r
616 string<T,TAlloc>& append(const T* const other, u32 length=0xffffffff)
\r
622 const T* p = other;
\r
631 if (used + len > allocated)
\r
632 reallocate(used + len);
\r
637 for (u32 l=0; l<len; ++l)
\r
638 array[l+used] = *(other+l);
\r
646 //! Appends a string to this string
\r
647 /** \param other: String to append. */
\r
648 string<T,TAlloc>& append(const string<T,TAlloc>& other)
\r
650 if (other.size() == 0)
\r
654 const u32 len = other.size()+1;
\r
656 if (used + len > allocated)
\r
657 reallocate(used + len);
\r
659 for (u32 l=0; l<len; ++l)
\r
660 array[used+l] = other[l];
\r
668 //! Appends a string of the length l to this string.
\r
669 /** \param other: other String to append to this string.
\r
670 \param length: How much characters of the other string to add to this one. */
\r
671 string<T,TAlloc>& append(const string<T,TAlloc>& other, u32 length)
\r
673 if (other.size() == 0)
\r
676 if (other.size() < length)
\r
682 if (used + length > allocated)
\r
683 reallocate(used + length);
\r
687 for (u32 l=0; l<length; ++l)
\r
688 array[l+used] = other[l];
\r
691 // ensure proper termination
\r
698 //! Insert a certain amount of characters into the string before the given index
\r
699 //\param pos Insert the characters before this index
\r
700 //\param s String to insert. Must be at least of size n
\r
701 //\param n Number of characters from string s to use.
\r
702 string<T,TAlloc>& insert(u32 pos, const char* s, u32 n)
\r
708 // move stuff behind insert point
\r
709 const u32 end = used+n-1;
\r
710 for (u32 i=0; i<used-pos; ++i)
\r
712 array[end-i] = array[end-(i+n)];
\r
716 for (u32 i=0; i<n; ++i)
\r
718 array[pos+i] = s[i];
\r
725 //! Reserves some memory.
\r
726 /** \param count: Amount of characters to reserve. */
\r
727 void reserve(u32 count)
\r
729 if (count < allocated)
\r
736 //! finds first occurrence of character in string
\r
737 /** \param c: Character to search for.
\r
738 \return Position where the character has been found,
\r
739 or -1 if not found. */
\r
740 s32 findFirst(T c) const
\r
742 for (u32 i=0; i<used-1; ++i)
\r
749 //! finds first occurrence of a character of a list in string
\r
750 /** \param c: List of characters to find. For example if the method
\r
751 should find the first occurrence of 'a' or 'b', this parameter should be "ab".
\r
752 \param count: Amount of characters in the list. Usually,
\r
753 this should be strlen(c)
\r
754 \return Position where one of the characters has been found,
\r
755 or -1 if not found. */
\r
756 s32 findFirstChar(const T* const c, u32 count=1) const
\r
761 for (u32 i=0; i<used-1; ++i)
\r
762 for (u32 j=0; j<count; ++j)
\r
763 if (array[i] == c[j])
\r
770 //! Finds first position of a character not in a given list.
\r
771 /** \param c: List of characters not to find. For example if the method
\r
772 should find the first occurrence of a character not 'a' or 'b', this parameter should be "ab".
\r
773 \param count: Amount of characters in the list. Usually,
\r
774 this should be strlen(c)
\r
775 \return Position where the character has been found,
\r
776 or -1 if not found. */
\r
778 s32 findFirstCharNotInList(const B* const c, u32 count=1) const
\r
783 for (u32 i=0; i<used-1; ++i)
\r
786 for (j=0; j<count; ++j)
\r
787 if (array[i] == c[j])
\r
797 //! Finds last position of a character not in a given list.
\r
798 /** \param c: List of characters not to find. For example if the method
\r
799 should find the first occurrence of a character not 'a' or 'b', this parameter should be "ab".
\r
800 \param count: Amount of characters in the list. Usually,
\r
801 this should be strlen(c)
\r
802 \return Position where the character has been found,
\r
803 or -1 if not found. */
\r
805 s32 findLastCharNotInList(const B* const c, u32 count=1) const
\r
810 for (s32 i=(s32)(used-2); i>=0; --i)
\r
813 for (j=0; j<count; ++j)
\r
814 if (array[i] == c[j])
\r
824 //! finds next occurrence of character in string
\r
825 /** \param c: Character to search for.
\r
826 \param startPos: Position in string to start searching.
\r
827 \return Position where the character has been found,
\r
828 or -1 if not found. */
\r
829 s32 findNext(T c, u32 startPos) const
\r
831 for (u32 i=startPos; i<used-1; ++i)
\r
839 //! finds last occurrence of character in string
\r
840 /** \param c: Character to search for.
\r
841 \param start: start to search reverse ( default = -1, on end )
\r
842 \return Position where the character has been found,
\r
843 or -1 if not found. */
\r
844 s32 findLast(T c, s32 start = -1) const
\r
846 start = core::clamp ( start < 0 ? (s32)(used) - 2 : start, 0, (s32)(used) - 2 );
\r
847 for (s32 i=start; i>=0; --i)
\r
854 //! finds last occurrence of a character of a list in string
\r
855 /** \param c: List of strings to find. For example if the method
\r
856 should find the last occurrence of 'a' or 'b', this parameter should be "ab".
\r
857 \param count: Amount of characters in the list. Usually,
\r
858 this should be strlen(c)
\r
859 \return Position where one of the characters has been found,
\r
860 or -1 if not found. */
\r
861 s32 findLastChar(const T* const c, u32 count=1) const
\r
866 for (s32 i=(s32)used-2; i>=0; --i)
\r
867 for (u32 j=0; j<count; ++j)
\r
868 if (array[i] == c[j])
\r
875 //! finds another string in this string
\r
876 /** \param str: Another string
\r
877 \param start: Start position of the search
\r
878 \return Positions where the string has been found,
\r
879 or -1 if not found. */
\r
881 s32 find(const B* const str, const u32 start = 0) const
\r
893 for (u32 i=start; i<used-len; ++i)
\r
897 while(str[j] && array[i+j] == str[j])
\r
909 //! Returns a substring
\r
910 /** \param begin Start of substring.
\r
911 \param length Length of substring.
\r
912 \param make_lower copy only lower case */
\r
913 string<T> subString(u32 begin, s32 length, bool make_lower = false ) const
\r
915 // if start after string
\r
916 // or no proper substring length
\r
917 if ((length <= 0) || (begin>=size()))
\r
918 return string<T>("");
\r
919 // clamp length to maximal value
\r
920 if ((length+begin) > size())
\r
921 length = size()-begin;
\r
923 // accounting for null terminator.
\r
924 s32 substrAllocLength = length + 1;
\r
926 o.reserve(substrAllocLength);
\r
930 for (s32 i=0; i<length; ++i)
\r
931 o.array[i] = array[i+begin];
\r
935 for (s32 i=0; i<length; ++i)
\r
936 o.array[i] = locale_lower ( array[i+begin] );
\r
939 o.array[substrAllocLength - 1] = 0;
\r
940 o.used = length + 1;
\r
946 //! Appends a character to this string
\r
947 /** \param c Character to append. */
\r
948 string<T,TAlloc>& operator += (T c)
\r
955 //! Appends a char string to this string
\r
956 /** \param c Char string to append. */
\r
957 string<T,TAlloc>& operator += (const T* const c)
\r
964 //! Appends a string to this string
\r
965 /** \param other String to append. */
\r
966 string<T,TAlloc>& operator += (const string<T,TAlloc>& other)
\r
973 //! Appends a string representation of a number to this string
\r
974 /** \param i Number to append. */
\r
975 string<T,TAlloc>& operator += (const int i)
\r
977 append(string<T,TAlloc>(i));
\r
982 //! Appends a string representation of a number to this string
\r
983 /** \param i Number to append. */
\r
984 string<T,TAlloc>& operator += (const unsigned int i)
\r
986 append(string<T,TAlloc>(i));
\r
991 //! Appends a string representation of a number to this string
\r
992 /** \param i Number to append. */
\r
993 string<T,TAlloc>& operator += (const long i)
\r
995 append(string<T,TAlloc>(i));
\r
1000 //! Appends a string representation of a number to this string
\r
1001 /** \param i Number to append. */
\r
1002 string<T,TAlloc>& operator += (const unsigned long i)
\r
1004 append(string<T,TAlloc>(i));
\r
1009 //! Appends a string representation of a number to this string
\r
1010 /** \param i Number to append. */
\r
1011 string<T,TAlloc>& operator += (const double i)
\r
1013 append(string<T,TAlloc>(i));
\r
1018 //! Appends a string representation of a number to this string
\r
1019 /** \param i Number to append. */
\r
1020 string<T,TAlloc>& operator += (const float i)
\r
1022 append(string<T,TAlloc>(i));
\r
1027 //! Replaces all characters of a special type with another one
\r
1028 /** \param toReplace Character to replace.
\r
1029 \param replaceWith Character replacing the old one. */
\r
1030 string<T,TAlloc>& replace(T toReplace, T replaceWith)
\r
1032 for (u32 i=0; i<used-1; ++i)
\r
1033 if (array[i] == toReplace)
\r
1034 array[i] = replaceWith;
\r
1039 //! Replaces all instances of a string with another one.
\r
1040 /** \param toReplace The string to replace.
\r
1041 \param replaceWith The string replacing the old one. */
\r
1042 string<T,TAlloc>& replace(const string<T,TAlloc>& toReplace, const string<T,TAlloc>& replaceWith)
\r
1044 if (toReplace.size() == 0)
\r
1047 const T* other = toReplace.c_str();
\r
1048 const T* replace = replaceWith.c_str();
\r
1049 const u32 other_size = toReplace.size();
\r
1050 const u32 replace_size = replaceWith.size();
\r
1052 // Determine the delta. The algorithm will change depending on the delta.
\r
1053 s32 delta = replace_size - other_size;
\r
1055 // A character for character replace. The string will not shrink or grow.
\r
1059 while ((pos = find(other, pos)) != -1)
\r
1061 for (u32 i = 0; i < replace_size; ++i)
\r
1062 array[pos + i] = replace[i];
\r
1068 // We are going to be removing some characters. The string will shrink.
\r
1072 for (u32 pos = 0; pos < used; ++i, ++pos)
\r
1074 // Is this potentially a match?
\r
1075 if (array[pos] == *other)
\r
1077 // Check to see if we have a match.
\r
1079 for (j = 0; j < other_size; ++j)
\r
1081 if (array[pos + j] != other[j])
\r
1085 // If we have a match, replace characters.
\r
1086 if (j == other_size)
\r
1088 for (j = 0; j < replace_size; ++j)
\r
1089 array[i + j] = replace[j];
\r
1090 i += replace_size - 1;
\r
1091 pos += other_size - 1;
\r
1096 // No match found, just copy characters.
\r
1097 array[i] = array[pos];
\r
1105 // We are going to be adding characters, so the string size will increase.
\r
1106 // Count the number of times toReplace exists in the string so we can allocate the new size.
\r
1107 u32 find_count = 0;
\r
1109 while ((pos = find(other, pos)) != -1)
\r
1115 // Re-allocate the string now, if needed.
\r
1116 u32 len = delta * find_count;
\r
1117 if (used + len > allocated)
\r
1118 reallocate(used + len);
\r
1120 // Start replacing.
\r
1122 while ((pos = find(other, pos)) != -1)
\r
1124 T* start = array + pos + other_size - 1;
\r
1125 T* ptr = array + used - 1;
\r
1126 T* end = array + delta + used -1;
\r
1128 // Shift characters to make room for the string.
\r
1129 while (ptr != start)
\r
1136 // Add the new string now.
\r
1137 for (u32 i = 0; i < replace_size; ++i)
\r
1138 array[pos + i] = replace[i];
\r
1140 pos += replace_size;
\r
1148 //! Removes characters from a string.
\r
1149 /** \param c: Character to remove. */
\r
1150 string<T,TAlloc>& remove(T c)
\r
1154 for (u32 i=0; i<used-1; ++i)
\r
1156 if (array[i] == c)
\r
1162 array[pos++] = array[i];
\r
1165 array[used-1] = 0;
\r
1170 //! Removes a string from the string.
\r
1171 /** \param toRemove: String to remove. */
\r
1172 string<T,TAlloc>& remove(const string<T,TAlloc>& toRemove)
\r
1174 u32 size = toRemove.size();
\r
1179 for (u32 i=0; i<used-1; ++i)
\r
1184 if (array[i + j] != toRemove[j])
\r
1195 array[pos++] = array[i];
\r
1198 array[used-1] = 0;
\r
1203 //! Removes characters from a string.
\r
1204 /** \param characters: Characters to remove. */
\r
1205 string<T,TAlloc>& removeChars(const string<T,TAlloc> & characters)
\r
1207 if (characters.size() == 0)
\r
1212 for (u32 i=0; i<used-1; ++i)
\r
1214 // Don't use characters.findFirst as it finds the \0,
\r
1215 // causing used to become incorrect.
\r
1216 bool docontinue = false;
\r
1217 for (u32 j=0; j<characters.size(); ++j)
\r
1219 if (characters[j] == array[i])
\r
1222 docontinue = true;
\r
1229 array[pos++] = array[i];
\r
1232 array[used-1] = 0;
\r
1238 //! Trims the string.
\r
1239 /** Removes the specified characters (by default, Latin-1 whitespace)
\r
1240 from the beginning and the end of the string. */
\r
1241 string<T,TAlloc>& trim(const string<T,TAlloc> & whitespace = " \t\n\r")
\r
1243 // find start and end of the substring without the specified characters
\r
1244 const s32 begin = findFirstCharNotInList(whitespace.c_str(), whitespace.used);
\r
1246 return (*this="");
\r
1248 const s32 end = findLastCharNotInList(whitespace.c_str(), whitespace.used);
\r
1250 return (*this = subString(begin, (end +1) - begin));
\r
1253 //! Erase 0's at the end when a string ends with a floating point number
\r
1254 /** After generating strings from floats we often end up with strings
\r
1255 ending up with lots of zeros which don't add any value. Erase 'em all.
\r
1256 Examples: "0.100000" becomes "0.1"
\r
1257 "10.000000" becomes "10"
\r
1258 "foo 3.140000" becomes "foo 3.14"
\r
1259 "no_num.000" stays "no_num.000"
\r
1262 string<T,TAlloc>& eraseTrailingFloatZeros(char decimalPoint='.')
\r
1264 s32 i=findLastCharNotInList("0", 1);
\r
1265 if ( i > 0 && (u32)i < used-2 ) // non 0 must be found and not last char (also used is at least 2 when i > 0)
\r
1267 u32 eraseStart=i+1;
\r
1269 if( core::isdigit(array[i]) )
\r
1271 while( --i>0 && core::isdigit(array[i]) );
\r
1272 if ( array[i] == decimalPoint )
\r
1275 else if ( array[i] == decimalPoint )
\r
1280 if ( dot > 0 && core::isdigit(array[dot-1]) )
\r
1282 array[eraseStart] = 0;
\r
1283 used = eraseStart+1;
\r
1289 //! Erases a character from the string.
\r
1290 /** May be slow, because all elements
\r
1291 following after the erased element have to be copied.
\r
1292 \param index: Index of element to be erased. */
\r
1293 string<T,TAlloc>& erase(u32 index)
\r
1295 _IRR_DEBUG_BREAK_IF(index>=used) // access violation
\r
1297 for (u32 i=index+1; i<used; ++i)
\r
1298 array[i-1] = array[i];
\r
1304 //! verify the existing string.
\r
1305 string<T,TAlloc>& validate()
\r
1307 // terminate on existing null
\r
1308 for (u32 i=0; i<allocated; ++i)
\r
1310 if (array[i] == 0)
\r
1318 if ( allocated > 0 )
\r
1321 array[used-1] = 0;
\r
1331 //! gets the last char of a string or null
\r
1332 T lastChar() const
\r
1334 return used > 1 ? array[used-2] : 0;
\r
1337 //! Split string into parts (tokens).
\r
1338 /** This method will split a string at certain delimiter characters
\r
1339 into the container passed in as reference. The type of the container
\r
1340 has to be given as template parameter. It must provide a push_back and
\r
1342 \param ret The result container. Tokens are added, the container is not cleared.
\r
1343 \param delimiter C-style string of delimiter characters
\r
1344 \param countDelimiters Number of delimiter characters
\r
1345 \param ignoreEmptyTokens Flag to avoid empty substrings in the result
\r
1346 container. If two delimiters occur without a character in between or an
\r
1347 empty substring would be placed in the result. Or if a delimiter is the last
\r
1348 character an empty substring would be added at the end. If this flag is set,
\r
1349 only non-empty strings are stored.
\r
1350 \param keepSeparators Flag which allows to add the separator to the
\r
1351 result string. If this flag is true, the concatenation of the
\r
1352 substrings results in the original string. Otherwise, only the
\r
1353 characters between the delimiters are returned.
\r
1354 \return The number of resulting substrings
\r
1356 template<class container>
\r
1357 u32 split(container& ret, const T* const delimiter, u32 countDelimiters=1, bool ignoreEmptyTokens=true, bool keepSeparators=false) const
\r
1362 const u32 oldSize=ret.size();
\r
1364 u32 tokenStartIdx = 0;
\r
1365 for (u32 i=0; i<used; ++i)
\r
1367 for (u32 j=0; j<countDelimiters; ++j)
\r
1369 if (array[i] == delimiter[j])
\r
1371 if (i - tokenStartIdx > 0)
\r
1372 ret.push_back(string<T,TAlloc>(&array[tokenStartIdx], i - tokenStartIdx));
\r
1373 else if ( !ignoreEmptyTokens )
\r
1374 ret.push_back(string<T,TAlloc>());
\r
1375 if ( keepSeparators )
\r
1377 ret.push_back(string<T,TAlloc>(&array[i], 1));
\r
1380 tokenStartIdx = i+1;
\r
1385 if ((used - 1) > tokenStartIdx)
\r
1386 ret.push_back(string<T,TAlloc>(&array[tokenStartIdx], (used - 1) - tokenStartIdx));
\r
1387 else if ( !ignoreEmptyTokens )
\r
1388 ret.push_back(string<T,TAlloc>());
\r
1390 return ret.size()-oldSize;
\r
1393 friend size_t multibyteToWString(string<wchar_t>& destination, const char* source, u32 sourceSize);
\r
1394 friend size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source, u32 sourceSize);
\r
1398 //! Reallocate the array, make it bigger or smaller
\r
1399 void reallocate(u32 new_size)
\r
1401 T* old_array = array;
\r
1403 array = allocator.allocate(new_size); //new T[new_size];
\r
1404 allocated = new_size;
\r
1406 const u32 amount = used < new_size ? used : new_size;
\r
1407 for (u32 i=0; i<amount; ++i)
\r
1408 array[i] = old_array[i];
\r
1410 if (allocated < used)
\r
1413 allocator.deallocate(old_array); // delete [] old_array;
\r
1416 //--- member variables
\r
1425 //! Typedef for character strings
\r
1426 typedef string<c8> stringc;
\r
1428 //! Typedef for wide character strings
\r
1429 typedef string<wchar_t> stringw;
\r
1431 //! Convert multibyte string to wide-character string
\r
1432 /** Wrapper around mbstowcs from standard library, but directly using Irrlicht string class.
\r
1433 What the function does exactly depends on the LC_CTYPE of the current c locale.
\r
1434 \param destination Wide-character string receiving the converted source
\r
1435 \param source multibyte string
\r
1436 \return The number of wide characters written to destination, not including the eventual terminating null character or -1 when conversion failed */
\r
1437 static inline size_t multibyteToWString(string<wchar_t>& destination, const core::string<c8>& source)
\r
1439 return multibyteToWString(destination, source.c_str(), (u32)source.size());
\r
1442 //! Convert multibyte string to wide-character string
\r
1443 /** Wrapper around mbstowcs from standard library, but directly writing to Irrlicht string class.
\r
1444 What the function does exactly depends on the LC_CTYPE of the current c locale.
\r
1445 \param destination Wide-character string receiving the converted source
\r
1446 \param source multibyte string
\r
1447 \return The number of wide characters written to destination, not including the eventual terminating null character or -1 when conversion failed. */
\r
1448 static inline size_t multibyteToWString(string<wchar_t>& destination, const char* source)
\r
1450 const u32 s = source ? (u32)strlen(source) : 0;
\r
1451 return multibyteToWString(destination, source, s);
\r
1454 //! Internally used by the other multibyteToWString functions
\r
1455 static size_t multibyteToWString(string<wchar_t>& destination, const char* source, u32 sourceSize)
\r
1459 destination.reserve(sourceSize+1);
\r
1460 #if defined(_MSC_VER)
\r
1461 #pragma warning(push)
\r
1462 #pragma warning(disable: 4996) // 'mbstowcs': This function or variable may be unsafe. Consider using mbstowcs_s instead.
\r
1464 const size_t written = mbstowcs(destination.array, source, (size_t)sourceSize);
\r
1465 #if defined(_MSC_VER)
\r
1466 #pragma warning(pop)
\r
1468 if ( written != (size_t)-1 )
\r
1470 destination.used = (u32)written+1;
\r
1471 destination.array[destination.used-1] = 0;
\r
1475 // Likely character which got converted until the invalid character was encountered are in destination now.
\r
1476 // And it seems even 0-terminated, but I found no documentation anywhere that this (the 0-termination) is guaranteed :-(
\r
1477 destination.clear();
\r
1483 destination.clear();
\r
1488 //! Same as multibyteToWString, but the other way around
\r
1489 static inline size_t wStringToMultibyte(string<c8>& destination, const core::string<wchar_t>& source)
\r
1491 return wStringToMultibyte(destination, source.c_str(), (u32)source.size());
\r
1494 //! Same as multibyteToWString, but the other way around
\r
1495 static inline size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source)
\r
1497 const u32 s = source ? (u32)wcslen(source) : 0;
\r
1498 return wStringToMultibyte(destination, source, s);
\r
1501 //! Same as multibyteToWString, but the other way around
\r
1502 static size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source, u32 sourceSize)
\r
1506 destination.reserve(sourceSize+1);
\r
1507 #if defined(_MSC_VER)
\r
1508 #pragma warning(push)
\r
1509 #pragma warning(disable: 4996) // 'wcstombs': This function or variable may be unsafe. Consider using wcstombs_s instead.
\r
1511 const size_t written = wcstombs(destination.array, source, (size_t)sourceSize);
\r
1512 #if defined(_MSC_VER)
\r
1513 #pragma warning(pop)
\r
1515 if ( written != (size_t)-1 )
\r
1517 destination.used = (u32)written+1;
\r
1518 destination.array[destination.used-1] = 0;
\r
1522 // Likely character which got converted until the invalid character was encountered are in destination now.
\r
1523 // And it seems even 0-terminated, but I found no documentation anywhere that this (the 0-termination) is guaranteed :-(
\r
1524 destination.clear();
\r
1530 destination.clear();
\r
1536 } // end namespace core
\r
1537 } // end namespace irr
\r