/usr/include/irrlicht/irrString.h is in libirrlicht-dev 1.8.4+dfsg1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 | // Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine" and the "irrXML" project.
// For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
#ifndef __IRR_STRING_H_INCLUDED__
#define __IRR_STRING_H_INCLUDED__
#include "irrTypes.h"
#include "irrAllocator.h"
#include "irrMath.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
namespace irr
{
namespace core
{
//! Very simple string class with some useful features.
/** string<c8> and string<wchar_t> both accept Unicode AND ASCII/Latin-1,
so you can assign Unicode to string<c8> and ASCII/Latin-1 to string<wchar_t>
(and the other way round) if you want to.
However, note that the conversation between both is not done using any encoding.
This means that c8 strings are treated as ASCII/Latin-1, not UTF-8, and
are simply expanded to the equivalent wchar_t, while Unicode/wchar_t
characters are truncated to 8-bit ASCII/Latin-1 characters, discarding all
other information in the wchar_t.
*/
enum eLocaleID
{
IRR_LOCALE_ANSI = 0,
IRR_LOCALE_GERMAN = 1
};
static eLocaleID locale_current = IRR_LOCALE_ANSI;
static inline void locale_set ( eLocaleID id )
{
locale_current = id;
}
//! Returns a character converted to lower case
static inline u32 locale_lower ( u32 x )
{
switch ( locale_current )
{
case IRR_LOCALE_GERMAN:
case IRR_LOCALE_ANSI:
break;
}
// ansi
return x >= 'A' && x <= 'Z' ? x + 0x20 : x;
}
//! Returns a character converted to upper case
static inline u32 locale_upper ( u32 x )
{
switch ( locale_current )
{
case IRR_LOCALE_GERMAN:
case IRR_LOCALE_ANSI:
break;
}
// ansi
return x >= 'a' && x <= 'z' ? x + ( 'A' - 'a' ) : x;
}
template <typename T, typename TAlloc = irrAllocator<T> >
class string
{
public:
typedef T char_type;
//! Default constructor
string()
: array(0), allocated(1), used(1)
{
array = allocator.allocate(1); // new T[1];
array[0] = 0;
}
//! Constructor
string(const string<T,TAlloc>& other)
: array(0), allocated(0), used(0)
{
*this = other;
}
//! Constructor from other string types
template <class B, class A>
string(const string<B, A>& other)
: array(0), allocated(0), used(0)
{
*this = other;
}
//! Constructs a string from a float
explicit string(const double number)
: array(0), allocated(0), used(0)
{
c8 tmpbuf[255];
snprintf(tmpbuf, 255, "%0.6f", number);
*this = tmpbuf;
}
//! Constructs a string from an int
explicit string(int number)
: array(0), allocated(0), used(0)
{
// store if negative and make positive
bool negative = false;
if (number < 0)
{
number *= -1;
negative = true;
}
// temporary buffer for 16 numbers
c8 tmpbuf[16]={0};
u32 idx = 15;
// special case '0'
if (!number)
{
tmpbuf[14] = '0';
*this = &tmpbuf[14];
return;
}
// add numbers
while(number && idx)
{
--idx;
tmpbuf[idx] = (c8)('0' + (number % 10));
number /= 10;
}
// add sign
if (negative)
{
--idx;
tmpbuf[idx] = '-';
}
*this = &tmpbuf[idx];
}
//! Constructs a string from an unsigned int
explicit string(unsigned int number)
: array(0), allocated(0), used(0)
{
// temporary buffer for 16 numbers
c8 tmpbuf[16]={0};
u32 idx = 15;
// special case '0'
if (!number)
{
tmpbuf[14] = '0';
*this = &tmpbuf[14];
return;
}
// add numbers
while(number && idx)
{
--idx;
tmpbuf[idx] = (c8)('0' + (number % 10));
number /= 10;
}
*this = &tmpbuf[idx];
}
//! Constructs a string from a long
explicit string(long number)
: array(0), allocated(0), used(0)
{
// store if negative and make positive
bool negative = false;
if (number < 0)
{
number *= -1;
negative = true;
}
// temporary buffer for 16 numbers
c8 tmpbuf[16]={0};
u32 idx = 15;
// special case '0'
if (!number)
{
tmpbuf[14] = '0';
*this = &tmpbuf[14];
return;
}
// add numbers
while(number && idx)
{
--idx;
tmpbuf[idx] = (c8)('0' + (number % 10));
number /= 10;
}
// add sign
if (negative)
{
--idx;
tmpbuf[idx] = '-';
}
*this = &tmpbuf[idx];
}
//! Constructs a string from an unsigned long
explicit string(unsigned long number)
: array(0), allocated(0), used(0)
{
// temporary buffer for 16 numbers
c8 tmpbuf[16]={0};
u32 idx = 15;
// special case '0'
if (!number)
{
tmpbuf[14] = '0';
*this = &tmpbuf[14];
return;
}
// add numbers
while(number && idx)
{
--idx;
tmpbuf[idx] = (c8)('0' + (number % 10));
number /= 10;
}
*this = &tmpbuf[idx];
}
//! Constructor for copying a string from a pointer with a given length
template <class B>
string(const B* const c, u32 length)
: array(0), allocated(0), used(0)
{
if (!c)
{
// correctly init the string to an empty one
*this="";
return;
}
allocated = used = length+1;
array = allocator.allocate(used); // new T[used];
for (u32 l = 0; l<length; ++l)
array[l] = (T)c[l];
array[length] = 0;
}
//! Constructor for unicode and ascii strings
template <class B>
string(const B* const c)
: array(0), allocated(0), used(0)
{
*this = c;
}
//! Destructor
~string()
{
allocator.deallocate(array); // delete [] array;
}
//! Assignment operator
string<T,TAlloc>& operator=(const string<T,TAlloc>& other)
{
if (this == &other)
return *this;
used = other.size()+1;
if (used>allocated)
{
allocator.deallocate(array); // delete [] array;
allocated = used;
array = allocator.allocate(used); //new T[used];
}
const T* p = other.c_str();
for (u32 i=0; i<used; ++i, ++p)
array[i] = *p;
return *this;
}
//! Assignment operator for other string types
template <class B, class A>
string<T,TAlloc>& operator=(const string<B,A>& other)
{
*this = other.c_str();
return *this;
}
//! Assignment operator for strings, ascii and unicode
template <class B>
string<T,TAlloc>& operator=(const B* const c)
{
if (!c)
{
if (!array)
{
array = allocator.allocate(1); //new T[1];
allocated = 1;
}
used = 1;
array[0] = 0x0;
return *this;
}
if ((void*)c == (void*)array)
return *this;
u32 len = 0;
const B* p = c;
do
{
++len;
} while(*p++);
// we'll keep the old string for a while, because the new
// string could be a part of the current string.
T* oldArray = array;
used = len;
if (used>allocated)
{
allocated = used;
array = allocator.allocate(used); //new T[used];
}
for (u32 l = 0; l<len; ++l)
array[l] = (T)c[l];
if (oldArray != array)
allocator.deallocate(oldArray); // delete [] oldArray;
return *this;
}
//! Append operator for other strings
string<T,TAlloc> operator+(const string<T,TAlloc>& other) const
{
string<T,TAlloc> str(*this);
str.append(other);
return str;
}
//! Append operator for strings, ascii and unicode
template <class B>
string<T,TAlloc> operator+(const B* const c) const
{
string<T,TAlloc> str(*this);
str.append(c);
return str;
}
//! Direct access operator
T& operator [](const u32 index)
{
_IRR_DEBUG_BREAK_IF(index>=used) // bad index
return array[index];
}
//! Direct access operator
const T& operator [](const u32 index) const
{
_IRR_DEBUG_BREAK_IF(index>=used) // bad index
return array[index];
}
//! Equality operator
bool operator==(const T* const str) const
{
if (!str)
return false;
u32 i;
for (i=0; array[i] && str[i]; ++i)
if (array[i] != str[i])
return false;
return (!array[i] && !str[i]);
}
//! Equality operator
bool operator==(const string<T,TAlloc>& other) const
{
for (u32 i=0; array[i] && other.array[i]; ++i)
if (array[i] != other.array[i])
return false;
return used == other.used;
}
//! Is smaller comparator
bool operator<(const string<T,TAlloc>& other) const
{
for (u32 i=0; array[i] && other.array[i]; ++i)
{
const s32 diff = array[i] - other.array[i];
if (diff)
return (diff < 0);
}
return (used < other.used);
}
//! Inequality operator
bool operator!=(const T* const str) const
{
return !(*this == str);
}
//! Inequality operator
bool operator!=(const string<T,TAlloc>& other) const
{
return !(*this == other);
}
//! Returns length of the string's content
/** \return Length of the string's content in characters, excluding
the trailing NUL. */
u32 size() const
{
return used-1;
}
//! Informs if the string is empty or not.
//! \return True if the string is empty, false if not.
bool empty() const
{
return (size() == 0);
}
//! Returns character string
/** \return pointer to C-style NUL terminated string. */
const T* c_str() const
{
return array;
}
//! Makes the string lower case.
string<T,TAlloc>& make_lower()
{
for (u32 i=0; array[i]; ++i)
array[i] = locale_lower ( array[i] );
return *this;
}
//! Makes the string upper case.
string<T,TAlloc>& make_upper()
{
for (u32 i=0; array[i]; ++i)
array[i] = locale_upper ( array[i] );
return *this;
}
//! Compares the strings ignoring case.
/** \param other: Other string to compare.
\return True if the strings are equal ignoring case. */
bool equals_ignore_case(const string<T,TAlloc>& other) const
{
for(u32 i=0; array[i] && other[i]; ++i)
if (locale_lower( array[i]) != locale_lower(other[i]))
return false;
return used == other.used;
}
//! Compares the strings ignoring case.
/** \param other: Other string to compare.
\param sourcePos: where to start to compare in the string
\return True if the strings are equal ignoring case. */
bool equals_substring_ignore_case(const string<T,TAlloc>&other, const s32 sourcePos = 0 ) const
{
if ( (u32) sourcePos >= used )
return false;
u32 i;
for( i=0; array[sourcePos + i] && other[i]; ++i)
if (locale_lower( array[sourcePos + i]) != locale_lower(other[i]))
return false;
return array[sourcePos + i] == 0 && other[i] == 0;
}
//! Compares the strings ignoring case.
/** \param other: Other string to compare.
\return True if this string is smaller ignoring case. */
bool lower_ignore_case(const string<T,TAlloc>& other) const
{
for(u32 i=0; array[i] && other.array[i]; ++i)
{
s32 diff = (s32) locale_lower ( array[i] ) - (s32) locale_lower ( other.array[i] );
if ( diff )
return diff < 0;
}
return used < other.used;
}
//! compares the first n characters of the strings
/** \param other Other string to compare.
\param n Number of characters to compare
\return True if the n first characters of both strings are equal. */
bool equalsn(const string<T,TAlloc>& other, u32 n) const
{
u32 i;
for(i=0; array[i] && other[i] && i < n; ++i)
if (array[i] != other[i])
return false;
// if one (or both) of the strings was smaller then they
// are only equal if they have the same length
return (i == n) || (used == other.used);
}
//! compares the first n characters of the strings
/** \param str Other string to compare.
\param n Number of characters to compare
\return True if the n first characters of both strings are equal. */
bool equalsn(const T* const str, u32 n) const
{
if (!str)
return false;
u32 i;
for(i=0; array[i] && str[i] && i < n; ++i)
if (array[i] != str[i])
return false;
// if one (or both) of the strings was smaller then they
// are only equal if they have the same length
return (i == n) || (array[i] == 0 && str[i] == 0);
}
//! Appends a character to this string
/** \param character: Character to append. */
string<T,TAlloc>& append(T character)
{
if (used + 1 > allocated)
reallocate(used + 1);
++used;
array[used-2] = character;
array[used-1] = 0;
return *this;
}
//! Appends a char string to this string
/** \param other: Char string to append. */
/** \param length: The length of the string to append. */
string<T,TAlloc>& append(const T* const other, u32 length=0xffffffff)
{
if (!other)
return *this;
u32 len = 0;
const T* p = other;
while(*p)
{
++len;
++p;
}
if (len > length)
len = length;
if (used + len > allocated)
reallocate(used + len);
--used;
++len;
for (u32 l=0; l<len; ++l)
array[l+used] = *(other+l);
used += len;
return *this;
}
//! Appends a string to this string
/** \param other: String to append. */
string<T,TAlloc>& append(const string<T,TAlloc>& other)
{
if (other.size() == 0)
return *this;
--used;
u32 len = other.size()+1;
if (used + len > allocated)
reallocate(used + len);
for (u32 l=0; l<len; ++l)
array[used+l] = other[l];
used += len;
return *this;
}
//! Appends a string of the length l to this string.
/** \param other: other String to append to this string.
\param length: How much characters of the other string to add to this one. */
string<T,TAlloc>& append(const string<T,TAlloc>& other, u32 length)
{
if (other.size() == 0)
return *this;
if (other.size() < length)
{
append(other);
return *this;
}
if (used + length > allocated)
reallocate(used + length);
--used;
for (u32 l=0; l<length; ++l)
array[l+used] = other[l];
used += length;
// ensure proper termination
array[used]=0;
++used;
return *this;
}
//! Reserves some memory.
/** \param count: Amount of characters to reserve. */
void reserve(u32 count)
{
if (count < allocated)
return;
reallocate(count);
}
//! finds first occurrence of character in string
/** \param c: Character to search for.
\return Position where the character has been found,
or -1 if not found. */
s32 findFirst(T c) const
{
for (u32 i=0; i<used-1; ++i)
if (array[i] == c)
return i;
return -1;
}
//! finds first occurrence of a character of a list in string
/** \param c: List of characters to find. For example if the method
should find the first occurrence of 'a' or 'b', this parameter should be "ab".
\param count: Amount of characters in the list. Usually,
this should be strlen(c)
\return Position where one of the characters has been found,
or -1 if not found. */
s32 findFirstChar(const T* const c, u32 count=1) const
{
if (!c || !count)
return -1;
for (u32 i=0; i<used-1; ++i)
for (u32 j=0; j<count; ++j)
if (array[i] == c[j])
return i;
return -1;
}
//! Finds first position of a character not in a given list.
/** \param c: List of characters not to find. For example if the method
should find the first occurrence of a character not 'a' or 'b', this parameter should be "ab".
\param count: Amount of characters in the list. Usually,
this should be strlen(c)
\return Position where the character has been found,
or -1 if not found. */
template <class B>
s32 findFirstCharNotInList(const B* const c, u32 count=1) const
{
if (!c || !count)
return -1;
for (u32 i=0; i<used-1; ++i)
{
u32 j;
for (j=0; j<count; ++j)
if (array[i] == c[j])
break;
if (j==count)
return i;
}
return -1;
}
//! Finds last position of a character not in a given list.
/** \param c: List of characters not to find. For example if the method
should find the first occurrence of a character not 'a' or 'b', this parameter should be "ab".
\param count: Amount of characters in the list. Usually,
this should be strlen(c)
\return Position where the character has been found,
or -1 if not found. */
template <class B>
s32 findLastCharNotInList(const B* const c, u32 count=1) const
{
if (!c || !count)
return -1;
for (s32 i=(s32)(used-2); i>=0; --i)
{
u32 j;
for (j=0; j<count; ++j)
if (array[i] == c[j])
break;
if (j==count)
return i;
}
return -1;
}
//! finds next occurrence of character in string
/** \param c: Character to search for.
\param startPos: Position in string to start searching.
\return Position where the character has been found,
or -1 if not found. */
s32 findNext(T c, u32 startPos) const
{
for (u32 i=startPos; i<used-1; ++i)
if (array[i] == c)
return i;
return -1;
}
//! finds last occurrence of character in string
/** \param c: Character to search for.
\param start: start to search reverse ( default = -1, on end )
\return Position where the character has been found,
or -1 if not found. */
s32 findLast(T c, s32 start = -1) const
{
start = core::clamp ( start < 0 ? (s32)(used) - 2 : start, 0, (s32)(used) - 2 );
for (s32 i=start; i>=0; --i)
if (array[i] == c)
return i;
return -1;
}
//! finds last occurrence of a character of a list in string
/** \param c: List of strings to find. For example if the method
should find the last occurrence of 'a' or 'b', this parameter should be "ab".
\param count: Amount of characters in the list. Usually,
this should be strlen(c)
\return Position where one of the characters has been found,
or -1 if not found. */
s32 findLastChar(const T* const c, u32 count=1) const
{
if (!c || !count)
return -1;
for (s32 i=(s32)used-2; i>=0; --i)
for (u32 j=0; j<count; ++j)
if (array[i] == c[j])
return i;
return -1;
}
//! finds another string in this string
/** \param str: Another string
\param start: Start position of the search
\return Positions where the string has been found,
or -1 if not found. */
template <class B>
s32 find(const B* const str, const u32 start = 0) const
{
if (str && *str)
{
u32 len = 0;
while (str[len])
++len;
if (len > used-1)
return -1;
for (u32 i=start; i<used-len; ++i)
{
u32 j=0;
while(str[j] && array[i+j] == str[j])
++j;
if (!str[j])
return i;
}
}
return -1;
}
//! Returns a substring
/** \param begin Start of substring.
\param length Length of substring.
\param make_lower copy only lower case */
string<T> subString(u32 begin, s32 length, bool make_lower = false ) const
{
// if start after string
// or no proper substring length
if ((length <= 0) || (begin>=size()))
return string<T>("");
// clamp length to maximal value
if ((length+begin) > size())
length = size()-begin;
string<T> o;
o.reserve(length+1);
s32 i;
if ( !make_lower )
{
for (i=0; i<length; ++i)
o.array[i] = array[i+begin];
}
else
{
for (i=0; i<length; ++i)
o.array[i] = locale_lower ( array[i+begin] );
}
o.array[length] = 0;
o.used = length + 1;
return o;
}
//! Appends a character to this string
/** \param c Character to append. */
string<T,TAlloc>& operator += (T c)
{
append(c);
return *this;
}
//! Appends a char string to this string
/** \param c Char string to append. */
string<T,TAlloc>& operator += (const T* const c)
{
append(c);
return *this;
}
//! Appends a string to this string
/** \param other String to append. */
string<T,TAlloc>& operator += (const string<T,TAlloc>& other)
{
append(other);
return *this;
}
//! Appends a string representation of a number to this string
/** \param i Number to append. */
string<T,TAlloc>& operator += (const int i)
{
append(string<T,TAlloc>(i));
return *this;
}
//! Appends a string representation of a number to this string
/** \param i Number to append. */
string<T,TAlloc>& operator += (const unsigned int i)
{
append(string<T,TAlloc>(i));
return *this;
}
//! Appends a string representation of a number to this string
/** \param i Number to append. */
string<T,TAlloc>& operator += (const long i)
{
append(string<T,TAlloc>(i));
return *this;
}
//! Appends a string representation of a number to this string
/** \param i Number to append. */
string<T,TAlloc>& operator += (const unsigned long i)
{
append(string<T,TAlloc>(i));
return *this;
}
//! Appends a string representation of a number to this string
/** \param i Number to append. */
string<T,TAlloc>& operator += (const double i)
{
append(string<T,TAlloc>(i));
return *this;
}
//! Appends a string representation of a number to this string
/** \param i Number to append. */
string<T,TAlloc>& operator += (const float i)
{
append(string<T,TAlloc>(i));
return *this;
}
//! Replaces all characters of a special type with another one
/** \param toReplace Character to replace.
\param replaceWith Character replacing the old one. */
string<T,TAlloc>& replace(T toReplace, T replaceWith)
{
for (u32 i=0; i<used-1; ++i)
if (array[i] == toReplace)
array[i] = replaceWith;
return *this;
}
//! Replaces all instances of a string with another one.
/** \param toReplace The string to replace.
\param replaceWith The string replacing the old one. */
string<T,TAlloc>& replace(const string<T,TAlloc>& toReplace, const string<T,TAlloc>& replaceWith)
{
if (toReplace.size() == 0)
return *this;
const T* other = toReplace.c_str();
const T* replace = replaceWith.c_str();
const u32 other_size = toReplace.size();
const u32 replace_size = replaceWith.size();
// Determine the delta. The algorithm will change depending on the delta.
s32 delta = replace_size - other_size;
// A character for character replace. The string will not shrink or grow.
if (delta == 0)
{
s32 pos = 0;
while ((pos = find(other, pos)) != -1)
{
for (u32 i = 0; i < replace_size; ++i)
array[pos + i] = replace[i];
++pos;
}
return *this;
}
// We are going to be removing some characters. The string will shrink.
if (delta < 0)
{
u32 i = 0;
for (u32 pos = 0; pos < used; ++i, ++pos)
{
// Is this potentially a match?
if (array[pos] == *other)
{
// Check to see if we have a match.
u32 j;
for (j = 0; j < other_size; ++j)
{
if (array[pos + j] != other[j])
break;
}
// If we have a match, replace characters.
if (j == other_size)
{
for (j = 0; j < replace_size; ++j)
array[i + j] = replace[j];
i += replace_size - 1;
pos += other_size - 1;
continue;
}
}
// No match found, just copy characters.
array[i] = array[pos];
}
array[i-1] = 0;
used = i;
return *this;
}
// We are going to be adding characters, so the string size will increase.
// Count the number of times toReplace exists in the string so we can allocate the new size.
u32 find_count = 0;
s32 pos = 0;
while ((pos = find(other, pos)) != -1)
{
++find_count;
++pos;
}
// Re-allocate the string now, if needed.
u32 len = delta * find_count;
if (used + len > allocated)
reallocate(used + len);
// Start replacing.
pos = 0;
while ((pos = find(other, pos)) != -1)
{
T* start = array + pos + other_size - 1;
T* ptr = array + used - 1;
T* end = array + delta + used -1;
// Shift characters to make room for the string.
while (ptr != start)
{
*end = *ptr;
--ptr;
--end;
}
// Add the new string now.
for (u32 i = 0; i < replace_size; ++i)
array[pos + i] = replace[i];
pos += replace_size;
used += delta;
}
return *this;
}
//! Removes characters from a string.
/** \param c: Character to remove. */
string<T,TAlloc>& remove(T c)
{
u32 pos = 0;
u32 found = 0;
for (u32 i=0; i<used-1; ++i)
{
if (array[i] == c)
{
++found;
continue;
}
array[pos++] = array[i];
}
used -= found;
array[used-1] = 0;
return *this;
}
//! Removes a string from the string.
/** \param toRemove: String to remove. */
string<T,TAlloc>& remove(const string<T,TAlloc>& toRemove)
{
u32 size = toRemove.size();
if ( size == 0 )
return *this;
u32 pos = 0;
u32 found = 0;
for (u32 i=0; i<used-1; ++i)
{
u32 j = 0;
while (j < size)
{
if (array[i + j] != toRemove[j])
break;
++j;
}
if (j == size)
{
found += size;
i += size - 1;
continue;
}
array[pos++] = array[i];
}
used -= found;
array[used-1] = 0;
return *this;
}
//! Removes characters from a string.
/** \param characters: Characters to remove. */
string<T,TAlloc>& removeChars(const string<T,TAlloc> & characters)
{
if (characters.size() == 0)
return *this;
u32 pos = 0;
u32 found = 0;
for (u32 i=0; i<used-1; ++i)
{
// Don't use characters.findFirst as it finds the \0,
// causing used to become incorrect.
bool docontinue = false;
for (u32 j=0; j<characters.size(); ++j)
{
if (characters[j] == array[i])
{
++found;
docontinue = true;
break;
}
}
if (docontinue)
continue;
array[pos++] = array[i];
}
used -= found;
array[used-1] = 0;
return *this;
}
//! Trims the string.
/** Removes the specified characters (by default, Latin-1 whitespace)
from the begining and the end of the string. */
string<T,TAlloc>& trim(const string<T,TAlloc> & whitespace = " \t\n\r")
{
// find start and end of the substring without the specified characters
const s32 begin = findFirstCharNotInList(whitespace.c_str(), whitespace.used);
if (begin == -1)
return (*this="");
const s32 end = findLastCharNotInList(whitespace.c_str(), whitespace.used);
return (*this = subString(begin, (end +1) - begin));
}
//! Erases a character from the string.
/** May be slow, because all elements
following after the erased element have to be copied.
\param index: Index of element to be erased. */
string<T,TAlloc>& erase(u32 index)
{
_IRR_DEBUG_BREAK_IF(index>=used) // access violation
for (u32 i=index+1; i<used; ++i)
array[i-1] = array[i];
--used;
return *this;
}
//! verify the existing string.
string<T,TAlloc>& validate()
{
// terminate on existing null
for (u32 i=0; i<allocated; ++i)
{
if (array[i] == 0)
{
used = i + 1;
return *this;
}
}
// terminate
if ( allocated > 0 )
{
used = allocated;
array[used-1] = 0;
}
else
{
used = 0;
}
return *this;
}
//! gets the last char of a string or null
T lastChar() const
{
return used > 1 ? array[used-2] : 0;
}
//! split string into parts.
/** This method will split a string at certain delimiter characters
into the container passed in as reference. The type of the container
has to be given as template parameter. It must provide a push_back and
a size method.
\param ret The result container
\param c C-style string of delimiter characters
\param count Number of delimiter characters
\param ignoreEmptyTokens Flag to avoid empty substrings in the result
container. If two delimiters occur without a character in between, an
empty substring would be placed in the result. If this flag is set,
only non-empty strings are stored.
\param keepSeparators Flag which allows to add the separator to the
result string. If this flag is true, the concatenation of the
substrings results in the original string. Otherwise, only the
characters between the delimiters are returned.
\return The number of resulting substrings
*/
template<class container>
u32 split(container& ret, const T* const c, u32 count=1, bool ignoreEmptyTokens=true, bool keepSeparators=false) const
{
if (!c)
return 0;
const u32 oldSize=ret.size();
u32 lastpos = 0;
bool lastWasSeparator = false;
for (u32 i=0; i<used; ++i)
{
bool foundSeparator = false;
for (u32 j=0; j<count; ++j)
{
if (array[i] == c[j])
{
if ((!ignoreEmptyTokens || i - lastpos != 0) &&
!lastWasSeparator)
ret.push_back(string<T,TAlloc>(&array[lastpos], i - lastpos));
foundSeparator = true;
lastpos = (keepSeparators ? i : i + 1);
break;
}
}
lastWasSeparator = foundSeparator;
}
if ((used - 1) > lastpos)
ret.push_back(string<T,TAlloc>(&array[lastpos], (used - 1) - lastpos));
return ret.size()-oldSize;
}
private:
//! Reallocate the array, make it bigger or smaller
void reallocate(u32 new_size)
{
T* old_array = array;
array = allocator.allocate(new_size); //new T[new_size];
allocated = new_size;
u32 amount = used < new_size ? used : new_size;
for (u32 i=0; i<amount; ++i)
array[i] = old_array[i];
if (allocated < used)
used = allocated;
allocator.deallocate(old_array); // delete [] old_array;
}
//--- member variables
T* array;
u32 allocated;
u32 used;
TAlloc allocator;
};
//! Typedef for character strings
typedef string<c8> stringc;
//! Typedef for wide character strings
typedef string<wchar_t> stringw;
} // end namespace core
} // end namespace irr
#endif
|