]> git.lizzy.rs Git - irrlicht.git/blob - include/irrString.h
Fix COSOperator::getSystemMemory
[irrlicht.git] / include / irrString.h
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
4 \r
5 #ifndef __IRR_STRING_H_INCLUDED__\r
6 #define __IRR_STRING_H_INCLUDED__\r
7 \r
8 #include "irrTypes.h"\r
9 #include "irrAllocator.h"\r
10 #include "irrMath.h"\r
11 #include <stdio.h>\r
12 #include <string.h>\r
13 #include <stdlib.h>\r
14 #include <wchar.h>\r
15 \r
16 namespace irr\r
17 {\r
18 namespace core\r
19 {\r
20 \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
25 \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
31 \r
32 Helper functions for converting between UTF-8 and wchar_t are provided\r
33 outside the string class for explicit use.\r
34 */\r
35 \r
36 // forward declarations\r
37 template <typename T, typename TAlloc = irrAllocator<T> >\r
38 class string;\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
42 \r
43 //! Returns a character converted to lower case\r
44 static inline u32 locale_lower ( u32 x )\r
45 {\r
46         // ansi\r
47         return x >= 'A' && x <= 'Z' ? x + 0x20 : x;\r
48 }\r
49 \r
50 //! Returns a character converted to upper case\r
51 static inline u32 locale_upper ( u32 x )\r
52 {\r
53         // ansi\r
54         return x >= 'a' && x <= 'z' ? x + ( 'A' - 'a' ) : x;\r
55 }\r
56 \r
57 \r
58 template <typename T, typename TAlloc>\r
59 class string\r
60 {\r
61 public:\r
62 \r
63         typedef T char_type;\r
64 \r
65         //! Default constructor\r
66         string()\r
67         : array(0), allocated(1), used(1)\r
68         {\r
69                 array = allocator.allocate(1); // new T[1];\r
70                 array[0] = 0;\r
71         }\r
72 \r
73 \r
74         //! Constructor\r
75         string(const string<T,TAlloc>& other)\r
76         : array(0), allocated(0), used(0)\r
77         {\r
78                 *this = other;\r
79         }\r
80 \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
85         {\r
86                 *this = other;\r
87         }\r
88 \r
89 \r
90         //! Constructs a string from a float\r
91         explicit string(const double number)\r
92         : array(0), allocated(0), used(0)\r
93         {\r
94                 c8 tmpbuf[255];\r
95                 snprintf_irr(tmpbuf, 255, "%0.6f", number);\r
96                 *this = tmpbuf;\r
97         }\r
98 \r
99 \r
100         //! Constructs a string from an int\r
101         explicit string(int number)\r
102         : array(0), allocated(0), used(0)\r
103         {\r
104                 // store if negative and make positive\r
105 \r
106                 bool negative = false;\r
107                 if (number < 0)\r
108                 {\r
109                         number *= -1;\r
110                         negative = true;\r
111                 }\r
112 \r
113                 // temporary buffer for 16 numbers\r
114 \r
115                 c8 tmpbuf[16]={0};\r
116                 u32 idx = 15;\r
117 \r
118                 // special case '0'\r
119 \r
120                 if (!number)\r
121                 {\r
122                         tmpbuf[14] = '0';\r
123                         *this = &tmpbuf[14];\r
124                         return;\r
125                 }\r
126 \r
127                 // add numbers\r
128 \r
129                 while(number && idx)\r
130                 {\r
131                         --idx;\r
132                         tmpbuf[idx] = (c8)('0' + (number % 10));\r
133                         number /= 10;\r
134                 }\r
135 \r
136                 // add sign\r
137 \r
138                 if (negative)\r
139                 {\r
140                         --idx;\r
141                         tmpbuf[idx] = '-';\r
142                 }\r
143 \r
144                 *this = &tmpbuf[idx];\r
145         }\r
146 \r
147 \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
151         {\r
152                 // temporary buffer for 16 numbers\r
153 \r
154                 c8 tmpbuf[16]={0};\r
155                 u32 idx = 15;\r
156 \r
157                 // special case '0'\r
158 \r
159                 if (!number)\r
160                 {\r
161                         tmpbuf[14] = '0';\r
162                         *this = &tmpbuf[14];\r
163                         return;\r
164                 }\r
165 \r
166                 // add numbers\r
167 \r
168                 while(number && idx)\r
169                 {\r
170                         --idx;\r
171                         tmpbuf[idx] = (c8)('0' + (number % 10));\r
172                         number /= 10;\r
173                 }\r
174 \r
175                 *this = &tmpbuf[idx];\r
176         }\r
177 \r
178 \r
179         //! Constructs a string from a long\r
180         explicit string(long number)\r
181         : array(0), allocated(0), used(0)\r
182         {\r
183                 // store if negative and make positive\r
184 \r
185                 bool negative = false;\r
186                 if (number < 0)\r
187                 {\r
188                         number *= -1;\r
189                         negative = true;\r
190                 }\r
191 \r
192                 // temporary buffer for 16 numbers\r
193 \r
194                 c8 tmpbuf[16]={0};\r
195                 u32 idx = 15;\r
196 \r
197                 // special case '0'\r
198 \r
199                 if (!number)\r
200                 {\r
201                         tmpbuf[14] = '0';\r
202                         *this = &tmpbuf[14];\r
203                         return;\r
204                 }\r
205 \r
206                 // add numbers\r
207 \r
208                 while(number && idx)\r
209                 {\r
210                         --idx;\r
211                         tmpbuf[idx] = (c8)('0' + (number % 10));\r
212                         number /= 10;\r
213                 }\r
214 \r
215                 // add sign\r
216 \r
217                 if (negative)\r
218                 {\r
219                         --idx;\r
220                         tmpbuf[idx] = '-';\r
221                 }\r
222 \r
223                 *this = &tmpbuf[idx];\r
224         }\r
225 \r
226 \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
230         {\r
231                 // temporary buffer for 16 numbers\r
232 \r
233                 c8 tmpbuf[16]={0};\r
234                 u32 idx = 15;\r
235 \r
236                 // special case '0'\r
237 \r
238                 if (!number)\r
239                 {\r
240                         tmpbuf[14] = '0';\r
241                         *this = &tmpbuf[14];\r
242                         return;\r
243                 }\r
244 \r
245                 // add numbers\r
246 \r
247                 while(number && idx)\r
248                 {\r
249                         --idx;\r
250                         tmpbuf[idx] = (c8)('0' + (number % 10));\r
251                         number /= 10;\r
252                 }\r
253 \r
254                 *this = &tmpbuf[idx];\r
255         }\r
256 \r
257 \r
258         //! Constructor for copying a string from a pointer with a given length\r
259         template <class B>\r
260         string(const B* const c, u32 length)\r
261         : array(0), allocated(0), used(0)\r
262         {\r
263                 if (!c)\r
264                 {\r
265                         // correctly init the string to an empty one\r
266                         *this="";\r
267                         return;\r
268                 }\r
269 \r
270                 allocated = used = length+1;\r
271                 array = allocator.allocate(used); // new T[used];\r
272 \r
273                 for (u32 l = 0; l<length; ++l)\r
274                         array[l] = (T)c[l];\r
275 \r
276                 array[length] = 0;\r
277         }\r
278 \r
279 \r
280         //! Constructor for Unicode and ASCII strings\r
281         template <class B>\r
282         string(const B* const c)\r
283         : array(0), allocated(0), used(0)\r
284         {\r
285                 *this = c;\r
286         }\r
287 \r
288 \r
289         //! Destructor\r
290         ~string()\r
291         {\r
292                 allocator.deallocate(array); // delete [] array;\r
293         }\r
294 \r
295 \r
296         //! Assignment operator\r
297         string<T,TAlloc>& operator=(const string<T,TAlloc>& other)\r
298         {\r
299                 if (this == &other)\r
300                         return *this;\r
301 \r
302                 used = other.size()+1;\r
303                 if (used>allocated)\r
304                 {\r
305                         allocator.deallocate(array); // delete [] array;\r
306                         allocated = used;\r
307                         array = allocator.allocate(used); //new T[used];\r
308                 }\r
309 \r
310                 const T* p = other.c_str();\r
311                 for (u32 i=0; i<used; ++i, ++p)\r
312                         array[i] = *p;\r
313 \r
314                 return *this;\r
315         }\r
316 \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
320         {\r
321                 *this = other.c_str();\r
322                 return *this;\r
323         }\r
324 \r
325 \r
326         //! Assignment operator for strings, ASCII and Unicode\r
327         template <class B>\r
328         string<T,TAlloc>& operator=(const B* const c)\r
329         {\r
330                 if (!c)\r
331                 {\r
332                         if (!array)\r
333                         {\r
334                                 array = allocator.allocate(1); //new T[1];\r
335                                 allocated = 1;\r
336                         }\r
337                         used = 1;\r
338                         array[0] = 0x0;\r
339                         return *this;\r
340                 }\r
341 \r
342                 if ((void*)c == (void*)array)\r
343                         return *this;\r
344 \r
345                 u32 len = 0;\r
346                 const B* p = c;\r
347                 do\r
348                 {\r
349                         ++len;\r
350                 } while(*p++);\r
351 \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
355 \r
356                 used = len;\r
357                 if (used>allocated)\r
358                 {\r
359                         allocated = used;\r
360                         array = allocator.allocate(used); //new T[used];\r
361                 }\r
362 \r
363                 for (u32 l = 0; l<len; ++l)\r
364                         array[l] = (T)c[l];\r
365 \r
366                 if (oldArray != array)\r
367                         allocator.deallocate(oldArray); // delete [] oldArray;\r
368 \r
369                 return *this;\r
370         }\r
371 \r
372 \r
373         //! Append operator for other strings\r
374         string<T,TAlloc> operator+(const string<T,TAlloc>& other) const\r
375         {\r
376                 string<T,TAlloc> str(*this);\r
377                 str.append(other);\r
378 \r
379                 return str;\r
380         }\r
381 \r
382 \r
383         //! Append operator for strings, ASCII and Unicode\r
384         template <class B>\r
385         string<T,TAlloc> operator+(const B* const c) const\r
386         {\r
387                 string<T,TAlloc> str(*this);\r
388                 str.append(c);\r
389 \r
390                 return str;\r
391         }\r
392 \r
393 \r
394         //! Direct access operator\r
395         T& operator [](const u32 index)\r
396         {\r
397                 _IRR_DEBUG_BREAK_IF(index>=used) // bad index\r
398                 return array[index];\r
399         }\r
400 \r
401 \r
402         //! Direct access operator\r
403         const T& operator [](const u32 index) const\r
404         {\r
405                 _IRR_DEBUG_BREAK_IF(index>=used) // bad index\r
406                 return array[index];\r
407         }\r
408 \r
409 \r
410         //! Equality operator\r
411         bool operator==(const T* const str) const\r
412         {\r
413                 if (!str)\r
414                         return false;\r
415 \r
416                 u32 i;\r
417                 for (i=0; array[i] && str[i]; ++i)\r
418                         if (array[i] != str[i])\r
419                                 return false;\r
420 \r
421                 return (!array[i] && !str[i]);\r
422         }\r
423 \r
424 \r
425         //! Equality operator\r
426         bool operator==(const string<T,TAlloc>& other) const\r
427         {\r
428                 for (u32 i=0; array[i] && other.array[i]; ++i)\r
429                         if (array[i] != other.array[i])\r
430                                 return false;\r
431 \r
432                 return used == other.used;\r
433         }\r
434 \r
435 \r
436         //! Is smaller comparator\r
437         bool operator<(const string<T,TAlloc>& other) const\r
438         {\r
439                 for (u32 i=0; array[i] && other.array[i]; ++i)\r
440                 {\r
441                         const s32 diff = array[i] - other.array[i];\r
442                         if (diff)\r
443                                 return (diff < 0);\r
444                 }\r
445 \r
446                 return (used < other.used);\r
447         }\r
448 \r
449 \r
450         //! Inequality operator\r
451         bool operator!=(const T* const str) const\r
452         {\r
453                 return !(*this == str);\r
454         }\r
455 \r
456 \r
457         //! Inequality operator\r
458         bool operator!=(const string<T,TAlloc>& other) const\r
459         {\r
460                 return !(*this == other);\r
461         }\r
462 \r
463 \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
467         u32 size() const\r
468         {\r
469                 return used-1;\r
470         }\r
471 \r
472         //! Informs if the string is empty or not.\r
473         //! \return True if the string is empty, false if not.\r
474         bool empty() const\r
475         {\r
476                 return (size() == 0);\r
477         }\r
478 \r
479         void clear(bool releaseMemory=true)\r
480         {\r
481                 if ( releaseMemory )\r
482                 {\r
483                         reallocate(1);\r
484                 }\r
485                 array[0] = 0;\r
486                 used = 1;\r
487         }\r
488 \r
489         //! Returns character string\r
490         /** \return pointer to C-style NUL terminated string. */\r
491         const T* c_str() const\r
492         {\r
493                 return array;\r
494         }\r
495 \r
496 \r
497         //! Makes the string lower case.\r
498         string<T,TAlloc>& make_lower()\r
499         {\r
500                 for (u32 i=0; array[i]; ++i)\r
501                         array[i] = locale_lower ( array[i] );\r
502                 return *this;\r
503         }\r
504 \r
505 \r
506         //! Makes the string upper case.\r
507         string<T,TAlloc>& make_upper()\r
508         {\r
509                 for (u32 i=0; array[i]; ++i)\r
510                         array[i] = locale_upper ( array[i] );\r
511                 return *this;\r
512         }\r
513 \r
514 \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
519         {\r
520                 for(u32 i=0; array[i] && other[i]; ++i)\r
521                         if (locale_lower( array[i]) != locale_lower(other[i]))\r
522                                 return false;\r
523 \r
524                 return used == other.used;\r
525         }\r
526 \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
532         {\r
533                 if ( (u32) sourcePos >= used )\r
534                         return false;\r
535 \r
536                 u32 i;\r
537                 for( i=0; array[sourcePos + i] && other[i]; ++i)\r
538                         if (locale_lower( array[sourcePos + i]) != locale_lower(other[i]))\r
539                                 return false;\r
540 \r
541                 return array[sourcePos + i] == 0 && other[i] == 0;\r
542         }\r
543 \r
544 \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
549         {\r
550                 for(u32 i=0; array[i] && other.array[i]; ++i)\r
551                 {\r
552                         s32 diff = (s32) locale_lower ( array[i] ) - (s32) locale_lower ( other.array[i] );\r
553                         if ( diff )\r
554                                 return diff < 0;\r
555                 }\r
556 \r
557                 return used < other.used;\r
558         }\r
559 \r
560 \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
566         {\r
567                 u32 i;\r
568                 for(i=0; i < n && array[i] && other[i]; ++i)\r
569                         if (array[i] != other[i])\r
570                                 return false;\r
571 \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
575         }\r
576 \r
577 \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
583         {\r
584                 if (!str)\r
585                         return false;\r
586                 u32 i;\r
587                 for(i=0; i < n && array[i] && str[i]; ++i)\r
588                         if (array[i] != str[i])\r
589                                 return false;\r
590 \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
594         }\r
595 \r
596 \r
597         //! Appends a character to this string\r
598         /** \param character: Character to append. */\r
599         string<T,TAlloc>& append(T character)\r
600         {\r
601                 if (used + 1 > allocated)\r
602                         reallocate(used + 1);\r
603 \r
604                 ++used;\r
605 \r
606                 array[used-2] = character;\r
607                 array[used-1] = 0;\r
608 \r
609                 return *this;\r
610         }\r
611 \r
612 \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
617         {\r
618                 if (!other)\r
619                         return *this;\r
620 \r
621                 u32 len = 0;\r
622                 const T* p = other;\r
623                 while(*p)\r
624                 {\r
625                         ++len;\r
626                         ++p;\r
627                 }\r
628                 if (len > length)\r
629                         len = length;\r
630 \r
631                 if (used + len > allocated)\r
632                         reallocate(used + len);\r
633 \r
634                 --used;\r
635                 ++len;\r
636 \r
637                 for (u32 l=0; l<len; ++l)\r
638                         array[l+used] = *(other+l);\r
639 \r
640                 used += len;\r
641 \r
642                 return *this;\r
643         }\r
644 \r
645 \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
649         {\r
650                 if (other.size() == 0)\r
651                         return *this;\r
652 \r
653                 --used;\r
654                 const u32 len = other.size()+1;\r
655 \r
656                 if (used + len > allocated)\r
657                         reallocate(used + len);\r
658 \r
659                 for (u32 l=0; l<len; ++l)\r
660                         array[used+l] = other[l];\r
661 \r
662                 used += len;\r
663 \r
664                 return *this;\r
665         }\r
666 \r
667 \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
672         {\r
673                 if (other.size() == 0)\r
674                         return *this;\r
675 \r
676                 if (other.size() < length)\r
677                 {\r
678                         append(other);\r
679                         return *this;\r
680                 }\r
681 \r
682                 if (used + length > allocated)\r
683                         reallocate(used + length);\r
684 \r
685                 --used;\r
686 \r
687                 for (u32 l=0; l<length; ++l)\r
688                         array[l+used] = other[l];\r
689                 used += length;\r
690 \r
691                 // ensure proper termination\r
692                 array[used]=0;\r
693                 ++used;\r
694 \r
695                 return *this;\r
696         }\r
697 \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
703         {\r
704                 if ( pos < used )\r
705                 {\r
706                         reserve(used+n);\r
707 \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
711                         {\r
712                                 array[end-i] = array[end-(i+n)];\r
713                         }\r
714                         used += n;\r
715 \r
716                         for (u32 i=0; i<n; ++i)\r
717                         {\r
718                                 array[pos+i] = s[i];\r
719                         }\r
720                 }\r
721 \r
722                 return *this;\r
723         }\r
724 \r
725         //! Reserves some memory.\r
726         /** \param count: Amount of characters to reserve. */\r
727         void reserve(u32 count)\r
728         {\r
729                 if (count < allocated)\r
730                         return;\r
731 \r
732                 reallocate(count);\r
733         }\r
734 \r
735 \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
741         {\r
742                 for (u32 i=0; i<used-1; ++i)\r
743                         if (array[i] == c)\r
744                                 return i;\r
745 \r
746                 return -1;\r
747         }\r
748 \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
757         {\r
758                 if (!c || !count)\r
759                         return -1;\r
760 \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
764                                         return i;\r
765 \r
766                 return -1;\r
767         }\r
768 \r
769 \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
777         template <class B>\r
778         s32 findFirstCharNotInList(const B* const c, u32 count=1) const\r
779         {\r
780                 if (!c || !count)\r
781                         return -1;\r
782 \r
783                 for (u32 i=0; i<used-1; ++i)\r
784                 {\r
785                         u32 j;\r
786                         for (j=0; j<count; ++j)\r
787                                 if (array[i] == c[j])\r
788                                         break;\r
789 \r
790                         if (j==count)\r
791                                 return i;\r
792                 }\r
793 \r
794                 return -1;\r
795         }\r
796 \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
804         template <class B>\r
805         s32 findLastCharNotInList(const B* const c, u32 count=1) const\r
806         {\r
807                 if (!c || !count)\r
808                         return -1;\r
809 \r
810                 for (s32 i=(s32)(used-2); i>=0; --i)\r
811                 {\r
812                         u32 j;\r
813                         for (j=0; j<count; ++j)\r
814                                 if (array[i] == c[j])\r
815                                         break;\r
816 \r
817                         if (j==count)\r
818                                 return i;\r
819                 }\r
820 \r
821                 return -1;\r
822         }\r
823 \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
830         {\r
831                 for (u32 i=startPos; i<used-1; ++i)\r
832                         if (array[i] == c)\r
833                                 return i;\r
834 \r
835                 return -1;\r
836         }\r
837 \r
838 \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
845         {\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
848                         if (array[i] == c)\r
849                                 return i;\r
850 \r
851                 return -1;\r
852         }\r
853 \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
862         {\r
863                 if (!c || !count)\r
864                         return -1;\r
865 \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
869                                         return i;\r
870 \r
871                 return -1;\r
872         }\r
873 \r
874 \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
880         template <class B>\r
881         s32 find(const B* const str, const u32 start = 0) const\r
882         {\r
883                 if (str && *str)\r
884                 {\r
885                         u32 len = 0;\r
886 \r
887                         while (str[len])\r
888                                 ++len;\r
889 \r
890                         if (len > used-1)\r
891                                 return -1;\r
892 \r
893                         for (u32 i=start; i<used-len; ++i)\r
894                         {\r
895                                 u32 j=0;\r
896 \r
897                                 while(str[j] && array[i+j] == str[j])\r
898                                         ++j;\r
899 \r
900                                 if (!str[j])\r
901                                         return i;\r
902                         }\r
903                 }\r
904 \r
905                 return -1;\r
906         }\r
907 \r
908 \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
914         {\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
922 \r
923                 // accounting for null terminator.\r
924                 s32 substrAllocLength = length + 1;\r
925                 string<T> o;\r
926                 o.reserve(substrAllocLength);\r
927 \r
928                 if ( !make_lower )\r
929                 {\r
930                         for (s32 i=0; i<length; ++i)\r
931                                 o.array[i] = array[i+begin];\r
932                 }\r
933                 else\r
934                 {\r
935                         for (s32 i=0; i<length; ++i)\r
936                                 o.array[i] = locale_lower ( array[i+begin] );\r
937                 }\r
938 \r
939                 o.array[substrAllocLength - 1] = 0;\r
940                 o.used = length + 1;\r
941 \r
942                 return o;\r
943         }\r
944 \r
945 \r
946         //! Appends a character to this string\r
947         /** \param c Character to append. */\r
948         string<T,TAlloc>& operator += (T c)\r
949         {\r
950                 append(c);\r
951                 return *this;\r
952         }\r
953 \r
954 \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
958         {\r
959                 append(c);\r
960                 return *this;\r
961         }\r
962 \r
963 \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
967         {\r
968                 append(other);\r
969                 return *this;\r
970         }\r
971 \r
972 \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
976         {\r
977                 append(string<T,TAlloc>(i));\r
978                 return *this;\r
979         }\r
980 \r
981 \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
985         {\r
986                 append(string<T,TAlloc>(i));\r
987                 return *this;\r
988         }\r
989 \r
990 \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
994         {\r
995                 append(string<T,TAlloc>(i));\r
996                 return *this;\r
997         }\r
998 \r
999 \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
1003         {\r
1004                 append(string<T,TAlloc>(i));\r
1005                 return *this;\r
1006         }\r
1007 \r
1008 \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
1012         {\r
1013                 append(string<T,TAlloc>(i));\r
1014                 return *this;\r
1015         }\r
1016 \r
1017 \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
1021         {\r
1022                 append(string<T,TAlloc>(i));\r
1023                 return *this;\r
1024         }\r
1025 \r
1026 \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
1031         {\r
1032                 for (u32 i=0; i<used-1; ++i)\r
1033                         if (array[i] == toReplace)\r
1034                                 array[i] = replaceWith;\r
1035                 return *this;\r
1036         }\r
1037 \r
1038 \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
1043         {\r
1044                 if (toReplace.size() == 0)\r
1045                         return *this;\r
1046 \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
1051 \r
1052                 // Determine the delta.  The algorithm will change depending on the delta.\r
1053                 s32 delta = replace_size - other_size;\r
1054 \r
1055                 // A character for character replace.  The string will not shrink or grow.\r
1056                 if (delta == 0)\r
1057                 {\r
1058                         s32 pos = 0;\r
1059                         while ((pos = find(other, pos)) != -1)\r
1060                         {\r
1061                                 for (u32 i = 0; i < replace_size; ++i)\r
1062                                         array[pos + i] = replace[i];\r
1063                                 ++pos;\r
1064                         }\r
1065                         return *this;\r
1066                 }\r
1067 \r
1068                 // We are going to be removing some characters.  The string will shrink.\r
1069                 if (delta < 0)\r
1070                 {\r
1071                         u32 i = 0;\r
1072                         for (u32 pos = 0; pos < used; ++i, ++pos)\r
1073                         {\r
1074                                 // Is this potentially a match?\r
1075                                 if (array[pos] == *other)\r
1076                                 {\r
1077                                         // Check to see if we have a match.\r
1078                                         u32 j;\r
1079                                         for (j = 0; j < other_size; ++j)\r
1080                                         {\r
1081                                                 if (array[pos + j] != other[j])\r
1082                                                         break;\r
1083                                         }\r
1084 \r
1085                                         // If we have a match, replace characters.\r
1086                                         if (j == other_size)\r
1087                                         {\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
1092                                                 continue;\r
1093                                         }\r
1094                                 }\r
1095 \r
1096                                 // No match found, just copy characters.\r
1097                                 array[i] = array[pos];\r
1098                         }\r
1099                         array[i-1] = 0;\r
1100                         used = i;\r
1101 \r
1102                         return *this;\r
1103                 }\r
1104 \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
1108                 s32 pos = 0;\r
1109                 while ((pos = find(other, pos)) != -1)\r
1110                 {\r
1111                         ++find_count;\r
1112                         ++pos;\r
1113                 }\r
1114 \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
1119 \r
1120                 // Start replacing.\r
1121                 pos = 0;\r
1122                 while ((pos = find(other, pos)) != -1)\r
1123                 {\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
1127 \r
1128                         // Shift characters to make room for the string.\r
1129                         while (ptr != start)\r
1130                         {\r
1131                                 *end = *ptr;\r
1132                                 --ptr;\r
1133                                 --end;\r
1134                         }\r
1135 \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
1139 \r
1140                         pos += replace_size;\r
1141                         used += delta;\r
1142                 }\r
1143 \r
1144                 return *this;\r
1145         }\r
1146 \r
1147 \r
1148         //! Removes characters from a string.\r
1149         /** \param c: Character to remove. */\r
1150         string<T,TAlloc>& remove(T c)\r
1151         {\r
1152                 u32 pos = 0;\r
1153                 u32 found = 0;\r
1154                 for (u32 i=0; i<used-1; ++i)\r
1155                 {\r
1156                         if (array[i] == c)\r
1157                         {\r
1158                                 ++found;\r
1159                                 continue;\r
1160                         }\r
1161 \r
1162                         array[pos++] = array[i];\r
1163                 }\r
1164                 used -= found;\r
1165                 array[used-1] = 0;\r
1166                 return *this;\r
1167         }\r
1168 \r
1169 \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
1173         {\r
1174                 u32 size = toRemove.size();\r
1175                 if ( size == 0 )\r
1176                         return *this;\r
1177                 u32 pos = 0;\r
1178                 u32 found = 0;\r
1179                 for (u32 i=0; i<used-1; ++i)\r
1180                 {\r
1181                         u32 j = 0;\r
1182                         while (j < size)\r
1183                         {\r
1184                                 if (array[i + j] != toRemove[j])\r
1185                                         break;\r
1186                                 ++j;\r
1187                         }\r
1188                         if (j == size)\r
1189                         {\r
1190                                 found += size;\r
1191                                 i += size - 1;\r
1192                                 continue;\r
1193                         }\r
1194 \r
1195                         array[pos++] = array[i];\r
1196                 }\r
1197                 used -= found;\r
1198                 array[used-1] = 0;\r
1199                 return *this;\r
1200         }\r
1201 \r
1202 \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
1206         {\r
1207                 if (characters.size() == 0)\r
1208                         return *this;\r
1209 \r
1210                 u32 pos = 0;\r
1211                 u32 found = 0;\r
1212                 for (u32 i=0; i<used-1; ++i)\r
1213                 {\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
1218                         {\r
1219                                 if (characters[j] == array[i])\r
1220                                 {\r
1221                                         ++found;\r
1222                                         docontinue = true;\r
1223                                         break;\r
1224                                 }\r
1225                         }\r
1226                         if (docontinue)\r
1227                                 continue;\r
1228 \r
1229                         array[pos++] = array[i];\r
1230                 }\r
1231                 used -= found;\r
1232                 array[used-1] = 0;\r
1233 \r
1234                 return *this;\r
1235         }\r
1236 \r
1237 \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
1242         {\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
1245                 if (begin == -1)\r
1246                         return (*this="");\r
1247 \r
1248                 const s32 end = findLastCharNotInList(whitespace.c_str(), whitespace.used);\r
1249 \r
1250                 return (*this = subString(begin, (end +1) - begin));\r
1251         }\r
1252 \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
1260                                   "1." stays "1."\r
1261         */\r
1262         string<T,TAlloc>& eraseTrailingFloatZeros(char decimalPoint='.')\r
1263         {\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
1266                 {\r
1267                         u32 eraseStart=i+1;\r
1268                         u32 dot=0;\r
1269                         if( core::isdigit(array[i]) )\r
1270                         {\r
1271                                 while( --i>0 && core::isdigit(array[i]) );\r
1272                                 if ( array[i] == decimalPoint )\r
1273                                         dot = i;\r
1274                         }\r
1275                         else if ( array[i] == decimalPoint )\r
1276                         {\r
1277                                 dot = i;\r
1278                                 eraseStart = i;\r
1279                         }\r
1280                         if ( dot > 0 && core::isdigit(array[dot-1]) )\r
1281                         {\r
1282                                 array[eraseStart] = 0;\r
1283                                 used = eraseStart+1;\r
1284                         }\r
1285                 }\r
1286                 return *this;\r
1287         }\r
1288 \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
1294         {\r
1295                 _IRR_DEBUG_BREAK_IF(index>=used) // access violation\r
1296 \r
1297                 for (u32 i=index+1; i<used; ++i)\r
1298                         array[i-1] = array[i];\r
1299 \r
1300                 --used;\r
1301                 return *this;\r
1302         }\r
1303 \r
1304         //! verify the existing string.\r
1305         string<T,TAlloc>& validate()\r
1306         {\r
1307                 // terminate on existing null\r
1308                 for (u32 i=0; i<allocated; ++i)\r
1309                 {\r
1310                         if (array[i] == 0)\r
1311                         {\r
1312                                 used = i + 1;\r
1313                                 return *this;\r
1314                         }\r
1315                 }\r
1316 \r
1317                 // terminate\r
1318                 if ( allocated > 0 )\r
1319                 {\r
1320                         used = allocated;\r
1321                         array[used-1] = 0;\r
1322                 }\r
1323                 else\r
1324                 {\r
1325                         used = 0;\r
1326                 }\r
1327 \r
1328                 return *this;\r
1329         }\r
1330 \r
1331         //! gets the last char of a string or null\r
1332         T lastChar() const\r
1333         {\r
1334                 return used > 1 ? array[used-2] : 0;\r
1335         }\r
1336 \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
1341         a size method.\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
1355         */\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
1358         {\r
1359                 if (!delimiter)\r
1360                         return 0;\r
1361 \r
1362                 const u32 oldSize=ret.size();\r
1363 \r
1364                 u32 tokenStartIdx = 0;\r
1365                 for (u32 i=0; i<used; ++i)\r
1366                 {\r
1367                         for (u32 j=0; j<countDelimiters; ++j)\r
1368                         {\r
1369                                 if (array[i] == delimiter[j])\r
1370                                 {\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
1376                                         {\r
1377                                                 ret.push_back(string<T,TAlloc>(&array[i], 1));\r
1378                                         }\r
1379 \r
1380                                         tokenStartIdx = i+1;\r
1381                                         break;\r
1382                                 }\r
1383                         }\r
1384                 }\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
1389 \r
1390                 return ret.size()-oldSize;\r
1391         }\r
1392 \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
1395 \r
1396 private:\r
1397 \r
1398         //! Reallocate the array, make it bigger or smaller\r
1399         void reallocate(u32 new_size)\r
1400         {\r
1401                 T* old_array = array;\r
1402 \r
1403                 array = allocator.allocate(new_size); //new T[new_size];\r
1404                 allocated = new_size;\r
1405 \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
1409 \r
1410                 if (allocated < used)\r
1411                         used = allocated;\r
1412 \r
1413                 allocator.deallocate(old_array); // delete [] old_array;\r
1414         }\r
1415 \r
1416         //--- member variables\r
1417 \r
1418         T* array;\r
1419         u32 allocated;\r
1420         u32 used;\r
1421         TAlloc allocator;\r
1422 };\r
1423 \r
1424 \r
1425 //! Typedef for character strings\r
1426 typedef string<c8> stringc;\r
1427 \r
1428 //! Typedef for wide character strings\r
1429 typedef string<wchar_t> stringw;\r
1430 \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
1438 {\r
1439         return multibyteToWString(destination, source.c_str(), (u32)source.size());\r
1440 }\r
1441 \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
1449 {\r
1450         const u32 s = source ? (u32)strlen(source) : 0;\r
1451         return multibyteToWString(destination, source, s);\r
1452 }\r
1453 \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
1456 {\r
1457         if ( sourceSize )\r
1458         {\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
1463 #endif\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
1467 #endif\r
1468                 if ( written != (size_t)-1 )\r
1469                 {\r
1470                         destination.used = (u32)written+1;\r
1471                         destination.array[destination.used-1] = 0;\r
1472                 }\r
1473                 else\r
1474                 {\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
1478                 }\r
1479                 return written;\r
1480         }\r
1481         else\r
1482         {\r
1483                 destination.clear();\r
1484                 return 0;\r
1485         }\r
1486 }\r
1487 \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
1490 {\r
1491         return wStringToMultibyte(destination, source.c_str(), (u32)source.size());\r
1492 }\r
1493 \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
1496 {\r
1497         const u32 s = source ? (u32)wcslen(source) : 0;\r
1498         return wStringToMultibyte(destination, source, s);\r
1499 }\r
1500 \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
1503 {\r
1504         if ( sourceSize )\r
1505         {\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
1510 #endif\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
1514 #endif\r
1515                 if ( written != (size_t)-1 )\r
1516                 {\r
1517                         destination.used = (u32)written+1;\r
1518                         destination.array[destination.used-1] = 0;\r
1519                 }\r
1520                 else\r
1521                 {\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
1525                 }\r
1526                 return written;\r
1527         }\r
1528         else\r
1529         {\r
1530                 destination.clear();\r
1531                 return 0;\r
1532         }\r
1533 }\r
1534 \r
1535 \r
1536 } // end namespace core\r
1537 } // end namespace irr\r
1538 \r
1539 #endif\r
1540 \r