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 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_DATATYPE_STRING_H
#define BALL_DATATYPE_STRING_H
#ifndef BALL_CONFIG_CONFIG_H
# include <BALL/CONFIG/config.h>
#endif
#ifndef BALL_COMMON_GLOBAL_H
# include <BALL/COMMON/global.h>
#endif
#ifndef BALL_COMMON_CREATE_H
# include <BALL/COMMON/create.h>
#endif
#ifndef BALL_COMMON_MACROS_H
# include <BALL/COMMON/macros.h>
#endif
#ifndef BALL_COMMON_EXCEPTION_H
# include <BALL/COMMON/exception.h>
#endif
#ifndef BALL_COMMON_DEBUG_H
# include <BALL/COMMON/debug.h>
#endif
#include <string>
#include <cctype>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
using std::string;
class QString;
class QByteArray;
namespace BALL
{
// forward declaration
class Substring;
/** \defgroup String String
An improved version of STL string.
\ingroup DatatypeMiscellaneous
*/
//@{
/** Extended String class.
\ingroup String
*/
class BALL_EXPORT String
{
///
friend class Substring;
public:
// String has no copy constructor taking String&, bool as arguments.
// the compiler would confuse it with another copy constructor,
// cast true to 1 and copy only the string from the second character
// on! We could use BALL_CREATE_NODEEP, but this leads to trouble with
// inline constructors, so we code it by hand (here and in string.C)
virtual void* create(bool /* deep */ = true, bool empty = false) const;
/** @name Enums and Constants */
//@{
/** Constants to set the compare mode.
Use one of these constants to set the mode you need.
These modes affect all \link compare compare \endlink methods. As these
methods are also used in the implementation of comparison operators,
all comparison operations will get affected from a change. \par
You may change the comparison mode by invoking setCompareMode. \par
*/
enum CompareMode
{
/// Constant to set to case sensitive comparisons (default)
CASE_SENSITIVE = 0,
/// Constant to set to case insensitive comparisons
CASE_INSENSITIVE = 1
};
/** Constant indicating the end of the string.
Use this constant instead of <tt>string::npos</tt> to indicate an invalid
position inside the string or the end of the string in those methods
requiring indices.
*/
static const Size EndPos;
/** Constant with a value of the greatest posible value for an element of type size_t.
*/
static const size_t npos = -1;
//@}
/** @name Predefined character classes
There exist several predefined character classes, that may
be used in several functions (e.g. trim methods) to represent
a set of characters.
*/
//@{
/// Character class containing all letters (lower and upper case)
static const char* CHARACTER_CLASS__ASCII_ALPHA;
/// Character class containing all letters and digits
static const char* CHARACTER_CLASS__ASCII_ALPHANUMERIC;
/// Character class containing all lower case letters
static const char* CHARACTER_CLASS__ASCII_LOWER;
/// Character class containing all upper case letters
static const char* CHARACTER_CLASS__ASCII_UPPER;
/// Character class containing the digits from 0 to 9
static const char* CHARACTER_CLASS__ASCII_NUMERIC;
/// Character class containing the digits from 0 to 9 and a dot
static const char* CHARACTER_CLASS__ASCII_FLOAT;
/** Character class containing all whitespace characters.
Whitespace characters are: \par
- blank " "
- horizontal tab $ "\backslash t" $
- new-line $ "\backslash n" $
- line-feed $ "\backslash r" $
- vertical tab $ "\backslash v" $
- form-feed $ "\backslash f" $
*/
static const char* CHARACTER_CLASS__WHITESPACE;
/** Character class containing double quotes.
*/
static const char* CHARACTER_CLASS__QUOTES;
//@}
/** @name Constructors and Destructors
*/
//@{
/// Default Constructor
String();
/// STL string copy constructor
String(const string& string);
/// Copy constructor
String(const String& s);
/// Move constructor
String(String&& s);
/// Move constructor for STL string
String(string&& s);
/// Move assigment operator
String& operator=(String&& s);
/// Move assignment operator for STL string
String& operator=(string&& s);
/// QString copy constructor
explicit String(const QString& string);
/// QByteArray copy constructor
explicit String(const QByteArray& string);
/** Creates a new string from a given range of another string.
@see String:Indices
@exception Exception::IndexUnderflow if <tt>from < 0</tt>
@exception Exception::IndexOverflow if <tt>from >= size()</tt>
*/
String(const String& s, Index from, Size len = EndPos);
/** Creates a new string from a C type string.
The new string contains the contents of <b>s</b> until
it has reached a length of <b>len</b> or contains a zero character
(whichever comes first). Default value for <b>len</b> is <b>EndPos</b>,
meaning as long as possible.
@exception Exception::IndexUnderflow if <tt>from < 0</tt>
@exception Exception::IndexOverflow if <tt>from >= size()</tt>
@exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
String(const char* char_ptr, Index from = 0, Size len = EndPos);
/** Creates a string using <b>sprintf</b>.
This constructor creates a new string and sets its content
to the result of a call to <b>sprintf</b> using <b>format</b> as a
format string and all additional parameters as arguments. \par
The result of the sprintf call is intermediately written to a buffer
of a maximum size of <b>buffer_size</b> characters, so choose an
appropriate size for this variables. \par
@exception IndexUnderflow, if the buffer size specified is not larger than 0
@exception NullPointer, if <tt>format == 0</tt>
*/
String(Size buffer_size, const char* format, ... );
/** Create a new string from the contents of a <b>stringstream</b>.
The contents of the <tt>stringstream</tt> are not modified, i.e.
successive construction of multiple strings from the same <tt>stringstream</tt>
object leads to identical copies.
*/
String(std::stringstream& s);
/** Creates a new string from len copies of c.
*/
String(const char c, Size len = 1);
/// Creates a string just containing an unsigned character
String(const unsigned char uc);
/// Construct a String from a short
String(short s);
/// Construct a String from an unsigned short
String(unsigned short us);
/// Construct a String from an int
String(int i);
/// Construct a String from an unsigned int
String(unsigned int ui);
/// Construct a String from a long
String(long l);
/// Construct a String from an unsigned long
String(unsigned long);
#ifdef BALL_ALLOW_LONG64_TYPE_OVERLOADS
/// Construct a String from a signed 64 bit integer
String(LongIndex l);
/// Construct a String from an unsigned 64 bit integer
String(LongSize);
#endif
/// Construct a String from a float value
String(float f);
/// Construct a String from a double value
String(double d);
/// Cast the String to a std::string reference
operator string&();
/// Cast the String to a std::string reference, const version
operator string const&() const;
/// Destructor
virtual ~String();
/// Clear the string (reset to the empty string)
void destroy();
//@}
/** @name Assignment methods
*/
//@{
/** Assign a string */
void set(const String& s);
/** Assign a String from a range of another string
@exception Exception::IndexOverflow if <tt>from < 0</tt>
@exception Exception::IndexUnderflow if <tt>from >= size()</tt>
*/
void set(const String& string, Index from, Size len = EndPos);
/** Assign a String from a C type string
@exception Exception::IndexUnderflow if <tt>from < 0</tt>
@exception Exception::IndexOverflow if <tt>from >= size()</tt>
@exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
void set(const char* char_ptr, Index from = 0, Size len = EndPos);
/** Assign a string to the result of a <b>sprintf</b> call
@exception Exception::IndexUnderflow, if the buffer size is zero
@exception Exception::NullPointer, <tt>format</tt> is a NULL pointer
*/
void set(Size buffer_size, const char *format, ...);
/** Assign a String from a <b>stringstream</b>.
The contents of the <tt>stringstream</tt> object are not modified.
*/
void set(std::stringstream& s);
/// Assign a String from the result of repeating <b>c</b> <b>len</b> times
void set(char c, Size len = 1);
/// Assign a String from an unsigned char
void set(unsigned char uc);
/// Assign a String from a short
void set(short s);
/// Assign a String from an unsigned short
void set(unsigned short us);
/// Assign a String from an int
void set(int i);
/// Assign a String from an unsigned int
void set(unsigned int ui);
/// Assign a String from a long
void set(long l);
/// Assign a String from an unsigned long
void set(unsigned long ul);
#ifdef BALL_ALLOW_LONG64_TYPE_OVERLOADS
/// Assign a String from a 64 bit integer
void set(LongIndex l);
/// Assign a String from an unsigned 64 bit integer
void set(LongSize ul);
#endif
/// Assign a String from a float value
void set(float f);
/// Assign a String from a double value
void set(double d);
/** Assign to a C type string
The resulting string contains the contents of <b>this</b> until
it has reached a length of <b>len</b> or contains a zero character
(whichever comes first). Default value for <b>len</b> is <b>EndPos</b>,
meaning as long as possible.
@exception Exception::IndexUnderflow if <tt>from < 0</tt>
@exception Exception::IndexOverflow if <tt>from >= size()</tt>
@exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
void get(char* char_ptr, Index from = 0, Size len = EndPos) const;
/// Assign a String from another String
const String& operator = (const String& s);
/** Assign a String from a C type string
@exception Exception::NullPointer if <tt>pc == NULL</tt>
*/
const String& operator = (const char* pc);
/** Assign a string from a <b>stringstream</b>.
The contents of the <tt>stringstream</tt> object are not modified.
*/
const String& operator = (std::stringstream& s);
/// Assign a String from a single char
const String& operator = (char c);
/// Assign a String from an unsigned char
const String& operator = (unsigned char uc);
/// Assign a String from a short
const String& operator = (short s);
/// Assign a String from an unsigned short
const String& operator = (unsigned short us);
/// Assign a String from an int
const String& operator = (int i);
/// Assign a String from an unsigned int
const String& operator = (unsigned int ui);
/// Assign a String from a long
const String& operator = (long l);
/// Assign a String from an unsigned long
const String& operator = (unsigned long ul);
#ifdef BALL_ALLOW_LONG64_TYPE_OVERLOADS
/// Assign a String from a 64 bit integer
const String& operator = (LongIndex l);
/// Assign a String from an unsigned 64 bit integer
const String& operator = (LongSize ul);
#endif
/// Assign a String from a float
const String& operator = (float f);
/// Assign a String from a double
const String& operator = (double d);
//@}
/** @name Compare mode-related methods.
All string comparisons can be made case-sensitive or
case insensitive. The behavior can be toggled globally
for all strings.
*/
//@{
/// Set the compareison mode for all string comparisons
static void setCompareMode(CompareMode compare_mode);
/// Return the current comparison mode
static CompareMode getCompareMode();
//@}
/** @name Converters
*/
//@{
/** Converts the string to a bool value.
This method returns <b>false</b>, if the string contains the string <tt>false</tt>
(may be surrounded by whitespaces), or <b>true</b> otherwise.
*/
bool toBool() const;
/// Return the first character of the string
char toChar() const;
/// Return the first character of the string converted to an unsigned char
unsigned char toUnsignedChar() const;
/** Convert the string to a short
* @exception Exception::InvalidFormat
*/
short toShort() const;
/** Convert the string to an unsigned short
* @exception Exception::InvalidFormat
*/
unsigned short toUnsignedShort() const;
/** Convert the string to an int
* @exception Exception::InvalidFormat
*/
int toInt() const;
/** Convert the string to an unsigned int
* @exception Exception::InvalidFormat
*/
unsigned int toUnsignedInt() const;
/** Convert the string to a long
* @exception Exception::InvalidFormat
*/
long toLong() const;
/** Convert the string to an unsigned long
* @exception Exception::InvalidFormat
*/
unsigned long toUnsignedLong() const;
/** Convert the string to a float
* @exception Exception::InvalidFormat
*/
float toFloat() const;
/** Convert the string to a double
* @exception Exception::InvalidFormat
*/
double toDouble() const;
//@}
/** @name Case Conversion
*/
//@{
/** Convert all characters in the given range to lower case
* @exception Exception::IndexUnderflow if <tt>from < 0</tt>
* @exception Exception::IndexOverflow if <tt>from || len >= size()</tt>
*/
void toLower(Index from = 0, Size len = EndPos);
/** Convert all characters in the given range to upper case
* @exception Exception::IndexUnderflow if <tt>from < 0</tt>
* @exception Exception::IndexOverflow if <tt>from || len >= size()</tt>
*/
void toUpper(Index from = 0, Size len = EndPos);
//@}
/** @name Substring Definition
*/
//@{
/** Return a substring
* @exception Exception::IndexUnderflow if <tt>from < 0</tt>
* @exception Exception::IndexOverflow if <tt>from >= size()</tt>
*/
Substring getSubstring(Index from = 0, Size len = EndPos) const;
/** Return a substring
* @exception Exception::IndexUnderflow if <tt>from < 0</tt>
* @exception Exception::IndexOverflow if <tt>from >= size()</tt>
*/
Substring operator () (Index from, Size len = EndPos) const;
/** Return a substring containing the string before the first occurence of <b>s</b>
*/
Substring before(const String& s, Index from = 0) const;
/** Return a substring containing the beginning of the string including the first occurence of <b>s</b>
*/
Substring through(const String& s, Index from = 0) const;
/** Return a substring containing the string from the first occurence of <b>s</b> on
*/
Substring from(const String& s, Index from = 0) const;
/** Return a substring containing the string after the first occurence of <b>s</b>.
*/
Substring after(const String& s, Index from = 0) const;
//@}
/** @name AWK style field operations
*/
//@{
/** Count the fields that are separated by a defined set of delimiters
* @exception Exception::NullPointer if <tt>delimiters == NULL</tt>
*/
Size countFields(const char* delimiters = CHARACTER_CLASS__WHITESPACE) const;
/** Count the fields and respect quote characters.
* @exception Exception::NullPointer if <tt>delimiters == NULL</tt> or <tt>quotes == NULL</tt>
*/
Size countFieldsQuoted(const char* delimiters = CHARACTER_CLASS__WHITESPACE,
const char* quotes = CHARACTER_CLASS__QUOTES) const;
/** Return a given field as a substring
* @exception Exception::IndexUnderflow if <tt>from < 0</tt>
* @exception Exception::NullPointer if <tt>delimiters == NULL</tt>
*/
String getField(Index index, const char* delimiters = CHARACTER_CLASS__WHITESPACE, Index* from = 0) const;
/** Return a given field and respect quote characters.
* @exception Exception::IndexUnderflow if <tt>from < 0</tt>
* @exception Exception::NullPointer if <tt>delimiters == NULL</tt>
*/
String getFieldQuoted(Index index, const char* delimiters = CHARACTER_CLASS__WHITESPACE,
const char* quotes = CHARACTER_CLASS__QUOTES, Index* from = 0) const;
/** Split the string into fields and assign these field to an array of strings
* @exception Exception::IndexUnderflow if <tt>from < 0</tt>
* @exception Exception::NullPointer if <tt>delimiters == NULL</tt>
*/
Size split(String string_array[], Size array_size, const char* delimiters = CHARACTER_CLASS__WHITESPACE, Index from = 0) const;
/** Split the string into fields and assign these field to a vector of strings.
The vector of strings is cleared in any case. Its final size is returned.
@exception IndexOverflow if <tt>from < 0</tt>
@exception NullPointer if <tt>delimiters == 0</tt>
*/
Size split(std::vector<String>& strings, const char* delimiters = CHARACTER_CLASS__WHITESPACE, Index from = 0) const;
/** Split the string into fields and respect quote characters.
Similar to \link split split \endlink , but delimiters that are inside quote characters (default is \link CHARACTER_CLASS__QUOTES CHARACTER_CLASS__QUOTES \endlink )
are not considered to split the string.
The vector of strings is cleared in any case. Its final size is returned.
@exception IndexOverflow if <tt>from < 0</tt>
@exception NullPointer if <tt>delimiters == 0</tt>
*/
Size splitQuoted(std::vector<String>& strings, const char* delimiters = CHARACTER_CLASS__WHITESPACE,
const char* quotes = CHARACTER_CLASS__QUOTES, Index from = 0) const;
//@}
/** @name BASIC style string operations
*/
//@{
/** Strips all characters in <b>trimmed</b> from the left of the string.
trimLeft stops at the first character encountered that is not in <b>trimmed</b>.
Using its default parameter CHARACTER_CLASS__WHITESPACE, it is usually handy to
remove blanks from the beginning of a string.
Strings consisting of character from <tt>trimmed</tt> only yield an empty string.
*/
String& trimLeft(const char* trimmed = CHARACTER_CLASS__WHITESPACE);
/** Strips all characters in <b>trimmed</b> from the right of the string.
trimRight stops at the first character encountered that is not in <b>trimmed</b>.
Using its default parameter CHARACTER_CLASS__WHITESPACE, it is usually handy to
remove blanks from the end of a string.
Strings consisting of character from <tt>trimmed</tt> only yield an empty string.
*/
String& trimRight(const char* trimmed = CHARACTER_CLASS__WHITESPACE);
/** Strips all characters in <b>trimmed</b> from both sides of the string.
trim calls <tt>trimRight(trimmed).trimLeft(trimmed)</tt>.
*/
String& trim(const char* trimmed = CHARACTER_CLASS__WHITESPACE);
// ?????
/** Strips all characters in <b>trimmed</b> from both sides of the string.
trim calls <tt>trimRight(trimmed).trimLeft(trimmed)</tt>.
*/
String trim(const char* trimmed = CHARACTER_CLASS__WHITESPACE) const;
/// Truncate the string to length <b>size</b>
String& truncate(Size size);
/// Return a substring containing the <b>len</b> leftmost characters of the string
Substring left(Size len) const;
/// Return a substring containing the <b>len</b> rightmost characters of the string
Substring right(Size len) const;
/** Return a substring containing the first occurence of <b>pattern</b> in the string.
If the pattern is not contained in the string, an empty Substring is returned.
The search for the pattern may also start from an index different from zero, allowing
incremental search.
@return Substring containing the search pattern, empty if not found
@param pattern the search pattern
@param from the index in the string to start the search from
*/
Substring instr(const String& pattern, Index from = 0) const;
//@}
/** @name String Operations
*/
//@{
// NOTE: please, please, pretty please, only try to optimize away operator+ definitions
// if you *really* know what you are doing. We didn't, and we definitely don't want
// to touch this stinking heap of C++ garbage ever again!
// (dstoeckel & anhi)
/// Concatenates two strings
BALL_EXPORT
friend String operator + (const String& s1, const string& s2);
/// Concatenates two strings
BALL_EXPORT
friend String operator + (const string& s1, const String& s2);
/// Concatenates two strings
BALL_EXPORT
friend String operator + (const String& s1, const String& s2);
/// Concatenates a string and a C type string
BALL_EXPORT
friend String operator + (const String& s1, const char* char_ptr);
/// Concatenates a C type string and a string
BALL_EXPORT
friend String operator + (const char* char_ptr, const String& s);
/// Concatenates a string and a character
BALL_EXPORT
friend String operator + (const String& s, char c);
/// Concatenates a character and a string
BALL_EXPORT
friend String operator + (char c, const String& s);
/// Concatenates two strings
BALL_EXPORT
friend String operator + (String&& s1, const string& s2);
/// Concatenates two strings
BALL_EXPORT
friend String operator + (String&& s1, const String& s2);
/// Concatenates two strings
BALL_EXPORT
friend String operator + (String&& s1, String&& s2);
BALL_EXPORT
friend String operator + (const String& s1, string&& s2);
BALL_EXPORT
friend String operator + (string&& s1, const String& s2);
BALL_EXPORT
friend String operator + (const string& s1, String&& s2);
/// Concatenates two strings
BALL_EXPORT
friend String operator + (const String& s1, String&& s2);
/// Concatenates a string and a C type string
BALL_EXPORT
friend String operator + (String&& s1, const char* char_ptr);
/// Concatenates a C type string and a string
BALL_EXPORT
friend String operator + (const char* char_ptr, String&& s);
/// Concatenates a string and a character
BALL_EXPORT
friend String operator + (String&& s, char c);
/// Concatenates a character and a string
BALL_EXPORT
friend String operator + (char c, String&& s);
/// Swaps the contents with another String
void swap(String& s);
/** Reverses the string.
If called without arguments, this method simply reverses the character sequence of the string.
By giving arguments for the indices, only a subsequence of the string may be reversed.
@param from first index of the sequence to be reversed
@param to last index of the sequence to be reversed
@see String:Indices
@exception Exception::IndexUnderflow if <tt>from < 0</tt>
@exception Exception::IndexOverflow if <tt>from >= size()</tt>
*/
String& reverse(Index from = 0, Size len = EndPos);
/** Substitute the first occurence of <b>to_replace</b> by the content of <b>replacing</b>.
@return the first position of the substitution or \link EndPos EndPos \endlink if <b>to_replace</b> is not found
*/
Size substitute(const String& to_replace, const String& replacing);
/** Substitute *all* occurences of <b>to_replace</b> by the content of <b>replacing</b>. */
void substituteAll(const String& to_replace, const String& replacing);
//@}
/** @name Predicates
*/
//@{
/// True, if the string contains character <b>c</b>
bool has(char c) const;
/// True, if the string contains the substring <b>s</b> after index <b>from</b>
bool hasSubstring(const String& s, Index from = 0) const;
/// True, if the string starts with <b>s</b>
bool hasPrefix(const String& s) const;
/// True, if the string ends with <b>s</b>
bool hasSuffix(const String& s) const;
/// True, if the string has size 0
bool isEmpty() const;
/** True, if the string only contains letters (any case).
It returns also <b>true</b>, if called for an empty string.
*/
bool isAlpha() const;
/** True, if the string only contains letters and digits.
It returns also <b>true</b>, if called for an empty string.
*/
bool isAlnum() const;
/** True, if the string only contains digits.
It returns also <b>true</b>, if called for an empty string.
*/
bool isDigit() const;
/** True, if the string is a floating number.
(It contains only numbers and maybe a dot).
It returns also <b>true</b>, if called for an empty string.
*/
bool isFloat() const;
/** True, if the string only contains spaces.
It returns also <b>true</b>, if called for an empty string.
*/
bool isSpace() const;
/** True, if the string only contains whitespace characters.
Whitespaces are defined in CHARACTER_CLASS__WHITESPACE.
It returns also <b>true</b>, if called for an empty string.
*/
bool isWhitespace() const;
/// True, if the character is a letter (any case)
static bool isAlpha(char c);
/// True, if the character is a letter or a digit
static bool isAlnum(char c);
/// True, if the character is a digit
static bool isDigit(char c);
/// True, if the character is a space
static bool isSpace(char c);
/** True, if the character is any whitespace character.
Whitespaces are defined in CHARACTER_CLASS__WHITESPACE
*/
static bool isWhitespace(char c);
//@}
/** @name Base64 String methods
*/
//@{
/// Convert a string to a base 64 string
String encodeBase64();
/** Decode a base 64 string.
Return an empty string, if base64 string is not right encoded.
*/
String decodeBase64();
//@}
/** @name Comparators
*/
//@{
/** compare to a string.
* @exception Exception::IndexUnderflow if <tt>from < 0</tt>
* @exception Exception::IndexOverflow if <tt>from >= size()</tt>
*/
int compare(const String& string, Index from = 0) const;
/** compare to a string.
* @exception Exception::IndexUnderflow if <tt>from < 0</tt>
* @exception Exception::IndexOverflow if <tt>from >= size()</tt>
*/
int compare(const String& string, Index from, Size len) const;
/** compare to c-style string.
* @exception Exception::IndexUnderflow if <tt>from < 0</tt>
* @exception Exception::IndexOverflow if <tt>from >= size()</tt>
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
int compare(const char* char_ptr, Index from = 0) const;
/** compare to c-style string.
* @exception Exception::IndexUnderflow if <tt>from < 0</tt>
* @exception Exception::IndexOverflow if <tt>from >= size()</tt>
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
int compare(const char* char_ptr, Index from, Size len) const;
/** compare to character
* @exception Exception::IndexUnderflow if <tt>from < 0</tt>
* @exception Exception::IndexOverflow if <tt>from >= size()</tt>
*/
int compare(char c, Index from = 0) const;
/** Equality operator.
*/
BALL_EXPORT
friend bool operator == (const String& s1, const String& s2);
/** Inequality operator.
*/
BALL_EXPORT
friend bool operator != (const String& s1, const String& s2);
/** Less than comparison
*/
BALL_EXPORT
friend bool operator < (const String& s1, const String& s2);
/** Less than or equal comparison
*/
BALL_EXPORT
friend bool operator <= (const String& s1, const String& s2);
/** Greater than comparison
*/
BALL_EXPORT
friend bool operator > (const String& s1, const String& s2);
/** Greater than or equal comparison
*/
BALL_EXPORT
friend bool operator >= (const String& s1, const String& s2);
/** Equality operator.
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
BALL_EXPORT
friend bool operator == (const char* char_ptr, const String& string);
/** Inequality operator.
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
BALL_EXPORT
friend bool operator != (const char* char_ptr, const String& string);
/** Less than comparison
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
BALL_EXPORT
friend bool operator < (const char* char_ptr, const String& string);
/** Less than or equal comparison
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
BALL_EXPORT
friend bool operator <= (const char* char_ptr, const String& string);
/** Greater than comparison
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
BALL_EXPORT
friend bool operator > (const char* char_ptr, const String& string);
/** Greater than or equal comparison
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
BALL_EXPORT
friend bool operator >= (const char* char_ptr, const String& string);
/** Equality operator.
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
bool operator == (const char* char_ptr) const;
/** Inequality operator.
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
bool operator != (const char* char_ptr) const;
/** Less than comparison
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
bool operator < (const char* char_ptr) const;
/** Less than or equal comparison
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
bool operator <= (const char* char_ptr) const;
/** Greater than comparison
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
bool operator > (const char* char_ptr) const;
/** Greater than or equal comparison
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
bool operator >= (const char* char_ptr) const;
///
BALL_EXPORT
friend bool operator == (char c, const String& string);
///
BALL_EXPORT
friend bool operator != (char c, const String& string);
///
BALL_EXPORT
friend bool operator < (char c, const String& string);
///
BALL_EXPORT
friend bool operator <= (char c, const String& string);
///
BALL_EXPORT
friend bool operator > (char c, const String& string);
///
friend bool operator >= (char c, const String& string);
///
bool operator == (char c) const;
///
bool operator != (char c) const;
///
bool operator < (char c) const;
///
bool operator <= (char c) const;
///
bool operator > (char c) const;
///
bool operator >= (char c) const;
//@}
/** @name Debugging and Diagnostics
*/
//@{
///
bool isValid() const;
///
void dump(std::ostream& s = std::cout, Size depth = 0) const;
//@}
/** @name Stream Operations
*/
//@{
///
std::istream& getline(std::istream& s = std::cin, char delimiter = '\n');
///
// BALL_EXPORT
// friend std::istream& getline(std::istream& s, String& string, char delimiter);
//@}
/// Constant empty string.
static const String EMPTY;
/**@ std::string compatibility layer
*/
//@{
/** name typedefs
*/
//@{
typedef string::value_type valuetype;
typedef string::traits_type traits_type;
typedef string::allocator_type allocator_type;
typedef string::reference reference;
typedef string::const_reference const_reference;
typedef string::pointer pointer;
typedef string::const_pointer const_pointer;
typedef string::iterator iterator;
typedef string::const_iterator const_iterator;
typedef string::reverse_iterator reverse_iterator;
typedef string::const_reverse_iterator const_reverse_iterator;
typedef string::difference_type difference_type;
typedef string::size_type size_type;
//@}
/** @name Iterators
*/
//@{
///
iterator begin() BALL_NOEXCEPT;
///
const_iterator begin() const BALL_NOEXCEPT;
///
iterator end() BALL_NOEXCEPT;
///
const_iterator end() const BALL_NOEXCEPT;
///
reverse_iterator rbegin() BALL_NOEXCEPT;
///
const_reverse_iterator rbegin() const BALL_NOEXCEPT;
///
reverse_iterator rend() BALL_NOEXCEPT;
///
const_reverse_iterator rend() const BALL_NOEXCEPT;
///
const_iterator cbegin() const BALL_NOEXCEPT;
///
const_iterator cend() const BALL_NOEXCEPT;
///
const_reverse_iterator crbegin() const BALL_NOEXCEPT;
///
const_reverse_iterator crend() const BALL_NOEXCEPT;
//@}
/** @name Capacity
*/
//@{
///
size_t size() const BALL_NOEXCEPT;
///
size_t length() const BALL_NOEXCEPT;
///
size_t max_size() const BALL_NOEXCEPT;
///
void resize(size_t n);
///
void resize(size_t n, char c);
///
size_t capacity() const BALL_NOEXCEPT;
///
void reserve(size_t n = 0);
///
void clear() BALL_NOEXCEPT;
///
bool empty() const BALL_NOEXCEPT;
///
void shrink_to_fit();
//@}
/** @name Element Access
*/
//@{
///
char& operator[] (size_t pos);
///
const char& operator[] (size_t pos) const;
///
char& at(size_t pos);
///
const char& at(size_t pos) const;
///
char& front();
///
const char& front() const;
///
char& back();
///
const char& back() const;
//@}
/** @name Modifiers
*/
//@{
///
String& operator += (const String& str);
///
String& operator += (const string& str);
///
String& operator += (const char* s);
///
String& operator += (char c);
///
String& operator += (std::initializer_list<char> il);
///
String& append(const String& str);
///
String& append(const string& str);
///
String& append(const string& str, size_t subpos, size_t sublen);
///
String& append(const char* s);
///
String& append(const char* s, size_t n);
///
String& append(size_t n, char c);
///
template <class InputIterator>
String& append(InputIterator first, InputIterator last);
///
String& append(std::initializer_list<char> li);
///
void push_back(char c);
///
String& assign(const String& str);
///
String& assign(const string& str);
///
String& assign(const string& str, size_t subpos, size_t sublen);
///
String& assign(const char* s);
///
String& assign(const char* s, size_t n);
///
String& assign(size_t n, char c);
///
template <class InputIterator>
String& assign(InputIterator first, InputIterator last);
///
String& assign(std::initializer_list<char> li);
///
String& assign(string&& str) BALL_NOEXCEPT;
///
String& insert(size_t pos, const string& str);
///
String& insert(size_t pos, const string& str, size_t subpos, size_t sublen);
///
String& insert(size_t pos, const char* s);
///
String& insert(size_t pos, const char* s, size_t n);
///
String& insert(size_t pos, size_t n, char c);
#ifdef BALL_HAS_STD_STRING_CONST_ITERATOR_FUNCTIONS
///
iterator insert(const_iterator p, size_t n, char c);
///
iterator insert(const_iterator p, char c);
#else
///
void insert(iterator p, size_t n, char c);
///
iterator insert(iterator p, char c);
#endif
///
template <class InputIterator>
iterator insert(iterator p, InputIterator first, InputIterator last);
#ifdef BALL_HAS_STD_STRING_CONST_ITERATOR_INITLIST_INSERT
///
String& insert(const_iterator p, std::initializer_list<char> li);
#endif
///
String& erase(size_t pos = 0, size_t len = npos);
#ifdef BALL_HAS_STD_STRING_CONST_ITERATOR_FUNCTIONS
///
iterator erase(const_iterator p);
///
iterator erase(const_iterator first, const_iterator last);
#else
///
iterator erase(iterator p);
///
iterator erase(iterator first, iterator last);
#endif
///
String& replace(size_t pos, size_t len, const string& str);
///
String& replace(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);
///
String& replace(size_t pos, size_t len, const char* s);
///
String& replace(size_t pos, size_t len, const char* s, size_t n);
///
String& replace(size_t pos, size_t len, size_t n, char c);
#ifdef BALL_HAS_STD_STRING_CONST_ITERATOR_FUNCTIONS
///
String& replace(const_iterator i1, const_iterator i2, const string& str);
///
String& replace(const_iterator i1, const_iterator i2, const char* s);
///
String& replace(const_iterator i1, const_iterator i2, const char* s, size_t n);
///
String& replace(const_iterator i1, const_iterator i2, size_t n, char c);
///
template <class InputIterator>
String& replace(const_iterator i1, const_iterator i2, InputIterator first, InputIterator last);
#else
///
String& replace(iterator i1, iterator i2, const string& str);
///
String& replace(iterator i1, iterator i2, const char* s);
///
String& replace(iterator i1, iterator i2, const char* s, size_t n);
///
String& replace(iterator i1, iterator i2, size_t n, char c);
///
template <class InputIterator>
String& replace(iterator i1, iterator i2, InputIterator first, InputIterator last);
#endif
#ifdef BALL_HAS_STD_STRING_CONST_ITERATOR_FUNCTIONS
///
String& replace(const_iterator i1, const_iterator i2, std::initializer_list<char> li);
#endif
///
void swap(string& str);
///
void pop_back();
//@}
/** @name String Operations
*/
//@{
///
const char* c_str() const BALL_NOEXCEPT;
///
const char* data() const BALL_NOEXCEPT;
///
allocator_type get_allocator() const BALL_NOEXCEPT;
///
size_t copy(char* s, size_t len, size_t pos = 0) const;
///
size_t find(const string& str, size_t pos = 0) const BALL_NOEXCEPT;
///
size_t find(const char* s, size_t pos = 0) const;
///
size_t find(const char* s, size_t pos, size_t n) const;
///
size_t find(char c, size_t pos = 0) const BALL_NOEXCEPT;
///
size_t rfind(const string& str, size_t pos = npos) const BALL_NOEXCEPT;
///
size_t rfind(const char* s, size_t pos = npos) const;
///
size_t rfind(const char* s, size_t pos, size_t n) const;
///
size_t rfind(char c, size_t pos = npos) const BALL_NOEXCEPT;
///
size_t find_first_of(const string& str, size_t pos = 0) const BALL_NOEXCEPT;
///
size_t find_first_of(const char* s, size_t pos = 0) const;
///
size_t find_first_of(const char* s, size_t pos, size_t n) const;
///
size_t find_first_of(char c, size_t pos = 0) const BALL_NOEXCEPT;
///
size_t find_last_of(const string& str, size_t pos = npos) const BALL_NOEXCEPT;
///
size_t find_last_of(const char* s, size_t pos = npos) const;
///
size_t find_last_of(const char* s, size_t pos, size_t n) const;
///
size_t find_last_of(char c, size_t pos = npos) const BALL_NOEXCEPT;
///
size_t find_first_not_of(const string& str, size_t pos = 0) const BALL_NOEXCEPT;
///
size_t find_first_not_of(const char* s, size_t pos = 0) const;
///
size_t find_first_not_of(const char* s, size_t pos, size_t n) const;
///
size_t find_first_not_of(char c, size_t pos = 0) const BALL_NOEXCEPT;
///
size_t find_last_not_of(const string& str, size_t pos = npos) const BALL_NOEXCEPT;
///
size_t find_last_not_of(const char* s, size_t pos = npos) const;
///
size_t find_last_not_of(const char* s, size_t pos, size_t n) const;
///
size_t find_last_not_of(char c, size_t pos = npos) const BALL_NOEXCEPT;
///
string substr(size_t pos = 0, size_t len = npos) const;
///
int compare(const string& str) const BALL_NOEXCEPT;
///
int compare(size_t pos, size_t len, const string& str) const;
///
int compare(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const;
///
//int compare(const char* s) const;
///
int compare(size_t pos, size_t len, const char* s) const;
///
int compare(size_t pos, size_t len, const char* s, size_t n) const;
//@}
//@}
protected:
// the validate... methods check perform a thorough
// index checking and an index translation
// Indices below zero are interpreted as indices
// relative to the end of the string
// All methods throw IndexUnder|Overflow exceptions
//
void validateIndex_(Index& index) const;
void validateRange_(Index& from, Size& len) const;
static void validateCharPtrRange_(Index& from, Size& len, const char* char_ptr);
static void valudateCharPtrIndex_(Index& index);
private:
/// The encapsulated std::string
string str_;
static int compareAscendingly_(const char* a, const char* b);
static int compareDescendingly_(const char* a, const char* b);
static CompareMode compare_mode_;
static char B64Chars_[64];
static int Index_64_[128];
};
/** A substring class.
The Substring class represents an efficient way to deal with substrings
of \link String String \endlink . Each Substring is bound to an instance of String and
is defined by a start and end index. It can be used like a String (with several
restrictions) but only affects the given range of the string it is bound to. \par
\ingroup String
*/
class BALL_EXPORT Substring
{
friend class String;
public:
BALL_CREATE_DEEP(Substring)
/** @name Exceptions
*/
//@{
/** Exception thrown if an unbound substring is accessed.
This exception is thrown by most accessors and predicates of
Substring if the substring is not bound to a string.
*/
class BALL_EXPORT UnboundSubstring
: public Exception::GeneralException
{
public:
UnboundSubstring(const char* file, int line);
};
/** Exception thrown if an invalid substring is accessed.
This exception is thrown if an invalid substring
is to be used.
@see isValid
*/
class BALL_EXPORT InvalidSubstring
: public Exception::GeneralException
{
public:
InvalidSubstring(const char* file, int line);
};
//@}
/** @name Constructors and Destructors
*/
//@{
/** Default constructor.
Create an empty string.
*/
Substring();
/** Copy constructor.
Create a substring from another substring.
@param substring the substring to be copied
@param deep ignored
*/
Substring(const Substring& substring, bool deep = true);
/** Create a substring from a string and two indices.
@param string the string the substring is bound to.
@param from the start index of the substring
@param len the length of the substring (default <tt>EndPos</tt>: to the end of the string)
@exception Exception::IndexUnderflow if <tt>from < 0</tt>
@exception Exception::IndexOverflow if <tt>from || len >= size()</tt>
*/
Substring(const String& string, Index from = 0, Size len = String::EndPos);
/** Destructor.
Destruct the substring.
*/
virtual ~Substring();
/** Clear the substrings contents.
Unbind the substring from its string and set the
start and the end position to 0.
*/
void destroy();
/** Clear the substrings contents.
Unbind the substring from its string and set the
start and the end position to 0.
*/
virtual void clear();
//@}
/** @name Converters
*/
//@{
/** Convert a substring to a String.
* Return a copy of the substring's contents.
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
operator String() const;
/** Convert a substring to a std::string.
* Return a copy of the substring's contents.
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
//explicit operator string() const;
/** Convert a substring to a string.
* Return a copy of the substring's contents.
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
String toString() const;
//@}
/** @name Binding and Unbinding Substrings
*/
//@{
/** Bind the substring to a string.
@param string the string to bind to
@param from the start position in the string (default is the beginning of the string)
@param len the substring's length (default is to the end of the string)
@exception Exception::IndexUnderflow if <tt>index < 0</tt>
@exception Exception::IndexOverflow if <tt>index >= size()</tt>
*/
Substring& bind(const String& string, Index from = 0, Size len = String::EndPos);
/** Bind the substring to the same string another substring is bound to.
* @param substring the substring that is bound to a string
* @exception Exception::IndexUnderflow if <tt>index < 0</tt>
* @exception Exception::IndexOverflow if <tt>index >= size()</tt>
*/
Substring& bind(const Substring& substring, Index from = 0, Size len = String::EndPos);
/// unbinds the substring from the string it is bound to
void unbind();
/// Return a pointer to the bound String
String* getBoundString();
/// Retunrs a const pointer to the bound String
const String* getBoundString() const
;
//@}
/** @name Assignment
*/
//@{
/** Sets the substring to a certain string
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
void set(const String& string);
/** Copies a substring from another substring
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
void set(const Substring& s);
/** Assigns a substring from a char pointer
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
* @exception Exception::IndexUnderflow
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
*/
void set(const char* char_ptr, Size size = String::EndPos);
/** String assignment operator
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
const Substring& operator = (const String& string);
/** Substring assignment operator
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
const Substring& operator = (const Substring& substring);
/** char pointer assignment operator
* @exception Exception::NullPointer if <tt>char_ptr == NULL</tt>
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
const Substring& operator = (const char* char_ptr);
//@}
/** @name Accessors and Mutators
*/
//@{
/** Return a pointer to the substring's contents
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
char* c_str();
/** Return a const pointer to the substring's contents
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
const char* c_str() const;
/** Return the first index of the substring.
* This means the starting point in the bound string.
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
Index getFirstIndex() const;
/** Return the last index of the substring
* This means the end point in the bound string.
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
Index getLastIndex() const;
/// Return the substring size
Size size() const;
/** Mutable random access to a character of the substring
* @exception Exception::IndexUnderflow if <tt>index < 0</tt>
* @exception Exception::IndexOverflow if <tt>index >= size()</tt>
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
char& operator [] (Index index);
/** Random access to a character of the substring (const method).
* @exception Exception::IndexUnderflow if <tt>index < 0</tt>
* @exception Exception::IndexOverflow if <tt>index >= size()</tt>
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
char operator [] (Index index) const;
/** Converts the substring to lower case characters
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
Substring& toLower();
/** Converts the substring to lower case characters
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
Substring& toUpper();
//@}
/** @name Predicates
*/
//@{
/// Return true, if the substring is bound to a String
bool isBound() const;
/// Return true, if the substring is empty or unbound
bool isEmpty() const;
//@}
/** @name Comparison Operators
*/
//@{
/** returns true, if the contents of the two substrings are equal
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
bool operator == (const Substring& substring) const;
/** Return true, if the contents of the two substrings are not equal
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
bool operator != (const Substring& substring) const;
/** Return true, if the contents of the substring and the string are equal
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
bool operator == (const String& string) const;
/** Return true, if the contents of the substring and the string are not equal
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
bool operator != (const String& string) const;
/** Return true, if the contents of the substring and the string are equal
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
BALL_EXPORT
friend bool operator == (const String& string, const Substring& substring);
/** Return true, if the contents of the substring and the string are not equal
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
BALL_EXPORT
friend bool operator != (const String& string, const Substring& substring);
/** Return true, if the contents of the substring are equal to the contents of the C-string
* @exception Exception::NullPointer if <tt>delimiters == NULL</tt>
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
bool operator == (const char* char_ptr) const;
/** Return true, if the contents of the substring are not equal to the contents of the C-string
* @exception Exception::NullPointer if <tt>delimiters == NULL</tt>
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
bool operator != (const char* char_ptr) const;
/** Return true, if the substring has length 1 and contains the given char
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
bool operator == (char c) const;
/** Return true, if the substring is differnet from the given char
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
bool operator != (char c) const;
//@}
/** @name Stream I/O
*/
//@{
/// Writes the substring to a stream
BALL_EXPORT
friend std::ostream& operator << (std::ostream& s, const Substring& substring);
//@}
/** @name Debugging and Diagnostics
*/
//@{
/** Return true, if the string is bound to a string and its indices are valid.
Valid indices means that the first index is not greater than the last index,
both indices are non-negative and lesser than the size of the bound string.
*/
bool isValid() const;
/** Dumps the substring object (including the values of its private members)
* @exception Substring::UnboundSubstring if this Substring is not correctly bound
*/
void dump(std::ostream& s = std::cout, Size depth = 0) const;
//@}
protected:
// throws IndexUnderflow|IndexOverflow
void validateRange_(Index& from, Size& len) const;
private:
/*_ @name Attributes
*/
//_@{
//_ pointer to the bound String
String* bound_;
//_ start index in the bound String
Index from_;
//_ end index in the bound String
Index to_;
//_@}
};
// non-member functions of String
/** Equality operator.
*/
BALL_EXPORT
bool operator == (const String& s1, const String& s2);
/** Inequality operator.
*/
BALL_EXPORT
bool operator != (const String& s1, const String& s2);
/** Less than comparison
*/
BALL_EXPORT
bool operator < (const String& s1, const String& s2);
/** Less than or equal comparison
*/
BALL_EXPORT
bool operator <= (const String& s1, const String& s2);
/** Greater than comparison
*/
BALL_EXPORT
bool operator > (const String& s1, const String& s2);
/** Greater than or equal comparison
*/
BALL_EXPORT
bool operator >= (const String& s1, const String& s2);
/// Concatenates two strings
BALL_EXPORT
String operator + (const String& s1, const string& s2);
/// Concatenates two strings
BALL_EXPORT
String operator + (const string& s1, const String& s2);
/// Concatenates two strings
BALL_EXPORT
String operator + (const String& s1, const String& s2);
/// Concatenates a string and a C type string
BALL_EXPORT
String operator + (const String& s1, const char* char_ptr);
/// Concatenates a C type string and a string
BALL_EXPORT
String operator + (const char* char_ptr, const String& s);
/// Concatenates a string and a character
BALL_EXPORT
String operator + (const String& s, char c);
/// Concatenates a character and a string
BALL_EXPORT
String operator + (char c, const String& s);
/// Concatenates two strings
BALL_EXPORT
String operator + (String&& s1, const string& s2);
/// Concatenates two strings
BALL_EXPORT
String operator + (String&& s1, const String& s2);
/// Concatenates two strings
BALL_EXPORT
String operator + (String&& s1, String&& s2);
/// Concatenates two strings
BALL_EXPORT
String operator + (const String& s1, string&& s2);
/// Concatenates two strings
BALL_EXPORT
String operator + (string&& s1, const String& s2);
/// Concatenates two strings
BALL_EXPORT
String operator + (const string& s1, String&& s2);
/// Concatenates two strings
BALL_EXPORT
String operator + (const String& s1, String&& s2);
/// Concatenates a string and a C type string
BALL_EXPORT
String operator + (String&& s1, const char* char_ptr);
/// Concatenates a C type string and a string
BALL_EXPORT
String operator + (const char* char_ptr, String&& s);
/// Concatenates a string and a character
BALL_EXPORT
String operator + (String&& s, char c);
/// Concatenates a character and a string
BALL_EXPORT
String operator + (char c, String&& s);
} // namespace BALL
namespace std
{
// Non-member functions for string
///
istream& operator>> (istream& is, BALL::String& str);
///
ostream& operator<< (ostream& os, BALL::String const& str);
///
istream& getline(istream& is, BALL::String& str, char delim);
///
istream& getline(istream& is, BALL::String& str);
///
istream& getline(istream& is, BALL::String&& str, char delim);
///
istream& getline(istream& is, BALL::String&& str);
}
//@}
# ifndef BALL_NO_INLINE_FUNCTIONS
# include <BALL/DATATYPE/string.iC>
# endif
#endif // BALL_DATATYPE_STRING_H
|