00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00038 #ifndef BLOCXX_STRING_HPP_INCLUDE_GUARD_
00039 #define BLOCXX_STRING_HPP_INCLUDE_GUARD_
00040 #include "blocxx/BLOCXX_config.h"
00041 #include "blocxx/Types.hpp"
00042 #include "blocxx/COWIntrusiveReference.hpp"
00043 #include "blocxx/CommonFwd.hpp"
00044 #include "blocxx/Exception.hpp"
00045 #include <iosfwd>
00046
00047 namespace BLOCXX_NAMESPACE
00048 {
00049
00050 BLOCXX_DECLARE_APIEXCEPTION(StringConversion, BLOCXX_COMMON_API);
00051
00064 class BLOCXX_COMMON_API String
00065 {
00066 public:
00067 class ByteBuf;
00071 String();
00078 explicit String(Int32 val);
00085 explicit String(UInt32 val);
00086
00087 #if defined(BLOCXX_INT32_IS_INT) && defined(BLOCXX_INT64_IS_LONG_LONG)
00088
00094 explicit String(long val);
00101 explicit String(unsigned long val);
00102 #endif
00103
00109 explicit String(Int64 val);
00116 explicit String(UInt64 val);
00123 explicit String(Real32 val);
00130 explicit String(Real64 val);
00136 String(const char* str);
00137
00138 enum ETakeOwnershipFlag
00139 {
00140 E_TAKE_OWNERSHIP
00141 };
00153 explicit String(ETakeOwnershipFlag, char* allocatedMemory, size_t len);
00160 explicit String(const char* str, size_t len);
00168 String(const String& arg);
00173 explicit String(char c);
00177 ~String();
00181 void swap(String& x);
00188 char* allocateCString() const;
00192 size_t length() const;
00198 size_t UTF8Length() const;
00202 bool empty() const { return length() == 0; }
00211 int format(const char* fmt, ...);
00212 enum EReturnDelimitersFlag
00213 {
00214 E_DISCARD_DELIMITERS,
00215 E_RETURN_DELIMITERS
00216 };
00217 enum EEmptyTokenReturnFlag
00218 {
00219 E_SKIP_EMPTY_TOKENS,
00220 E_RETURN_EMPTY_TOKENS
00221 };
00235 StringArray tokenize(const char* delims = " \n\r\t\v",
00236 EReturnDelimitersFlag returnDelimitersAsTokens = E_DISCARD_DELIMITERS,
00237 EEmptyTokenReturnFlag returnEmptyTokens = E_SKIP_EMPTY_TOKENS ) const;
00242 const char* c_str() const;
00249 char charAt(size_t ndx) const;
00257 int compareTo(const String& arg) const;
00265 int compareTo(const char* arg) const;
00273 int compareToIgnoreCase(const String& arg) const;
00281 int compareToIgnoreCase(const char* arg) const;
00287 String& concat(const char* arg);
00288
00294 String& concat(const String& arg)
00295 {
00296 return concat(arg.c_str());
00297 }
00298
00304 String& concat(char arg);
00305 enum EIgnoreCaseFlag
00306 {
00307 E_CASE_SENSITIVE,
00308 E_CASE_INSENSITIVE
00309 };
00318 bool endsWith(const char* arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const;
00319
00329 bool endsWith(const String& arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const
00330 {
00331 return endsWith(arg.c_str(), ignoreCase);
00332 }
00333
00340 bool endsWith(char arg) const;
00341
00349 bool equals(const String& arg) const;
00357 bool equals(const char* arg) const;
00366 bool equalsIgnoreCase(const String& arg) const;
00375 bool equalsIgnoreCase(const char* arg) const;
00379 UInt32 hashCode() const;
00388 size_t indexOf(char ch, size_t fromIndex=0) const;
00396 size_t indexOf(const char* arg, size_t fromIndex=0) const;
00404 size_t indexOf(const String& arg, size_t fromIndex=0) const
00405 {
00406 return indexOf(arg.c_str(), fromIndex);
00407 }
00408
00417 size_t lastIndexOf(char ch, size_t fromIndex=npos) const;
00426 size_t lastIndexOf(const char* arg, size_t fromIndex=npos) const;
00435 size_t lastIndexOf(const String& arg, size_t fromIndex=npos) const
00436 {
00437 return lastIndexOf(arg.c_str(), fromIndex);
00438 }
00439
00448 bool startsWith(const char* arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const;
00457 bool startsWith(const String& arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const
00458 {
00459 return startsWith(arg.c_str(), ignoreCase);
00460 }
00467 bool startsWith(char arg) const;
00468
00477 String substring(size_t beginIndex,
00478 size_t length=npos) const;
00483 bool isSpaces() const;
00489 String& toLowerCase();
00495 String& toUpperCase();
00501 String& ltrim();
00507 String& rtrim();
00514 String& trim();
00520 String& erase();
00521
00527 String& erase( size_t idx, size_t len = npos );
00534 String& operator= (const String & arg);
00542 const char& operator[] (size_t ndx) const;
00543 char& operator[] (size_t ndx);
00550 String& operator+= (const String& arg) { return concat(arg); }
00557 String& operator+= (const char* arg) { return concat(arg); }
00564 String& operator+= (char arg) { return concat(arg); }
00571 void readObject(std::istream& istrm);
00577 void writeObject(std::ostream& ostrm) const;
00581 String toString() const;
00586 Real32 toReal32() const;
00591 Real64 toReal64() const;
00597 bool toBool() const;
00602 UInt8 toUInt8(int base=10) const;
00607 Int8 toInt8(int base=10) const;
00612 UInt16 toUInt16(int base=10) const;
00617 Int16 toInt16(int base=10) const;
00622 UInt32 toUInt32(int base=10) const;
00627 Int32 toInt32(int base=10) const;
00632 UInt64 toUInt64(int base=10) const;
00637 Int64 toInt64(int base=10) const;
00642 unsigned int toUnsignedInt(int base=10) const;
00647 int toInt(int base=10) const;
00658 static unsigned long long int strtoull(const char* nptr, char** endptr,
00659 int base);
00670 static long long int strtoll(const char* nptr, char** endptr, int base);
00678 static const char* strchr(const char* theStr, int c);
00687 static String getLine(std::istream& istr);
00688
00689 #if defined(BLOCXX_AIX)
00690 static const size_t npos;
00691 #else
00692 static const size_t npos = ~0;
00693 #endif // BLOCXX_AIX
00694
00695 #ifdef BLOCXX_WIN32
00696 #pragma warning (push)
00697 #pragma warning (disable: 4251)
00698 #endif
00699
00700 typedef COWIntrusiveReference<ByteBuf> buf_t;
00701 private:
00702 buf_t m_buf;
00703
00704 #ifdef BLOCXX_WIN32
00705 #pragma warning (pop)
00706 #endif
00707
00708 };
00709 BLOCXX_EXPORT_TEMPLATE(BLOCXX_COMMON_API, Array, String);
00710 BLOCXX_EXPORT_TEMPLATE(BLOCXX_COMMON_API, Enumeration, String);
00711
00712 BLOCXX_COMMON_API std::ostream& operator<< (std::ostream& ostr, const String& arg);
00713 BLOCXX_COMMON_API String operator + (const String& s1, const String& s2);
00714 BLOCXX_COMMON_API String operator + (const char* p, const String& s);
00715 BLOCXX_COMMON_API String operator + (const String& s, const char* p);
00716 BLOCXX_COMMON_API String operator + (char c, const String& s);
00717 BLOCXX_COMMON_API String operator + (const String& s, char c);
00718 inline bool
00719 operator == (const String& s1, const String& s2)
00720 {
00721 return (s1.compareTo(s2) == 0);
00722 }
00723 inline bool
00724 operator == (const String& s, const char* p)
00725 {
00726 return (s.compareTo(p) == 0);
00727 }
00728 inline bool
00729 operator == (const char* p, const String& s)
00730 {
00731 return (s.compareTo(p) == 0);
00732 }
00733 inline bool
00734 operator != (const String& s1, const String& s2)
00735 {
00736 return (s1.compareTo(s2) != 0);
00737 }
00738 inline bool
00739 operator != (const String& s, const char* p)
00740 {
00741 return (s.compareTo(p) != 0);
00742 }
00743 inline bool
00744 operator != (const char* p, const String& s)
00745 {
00746 return (s.compareTo(p) != 0);
00747 }
00748 inline bool
00749 operator < (const String& s1, const String& s2)
00750 {
00751 return (s1.compareTo(s2) < 0);
00752 }
00753 inline bool
00754 operator < (const String& s, const char* p)
00755 {
00756 return (s.compareTo(p) < 0);
00757 }
00758 inline bool
00759 operator < (const char* p, const String& s)
00760 {
00761 return (String(p).compareTo(s) < 0);
00762 }
00763 inline bool
00764 operator <= (const String& s1, const String& s2)
00765 {
00766 return (s1.compareTo(s2) <= 0);
00767 }
00768 inline bool
00769 operator <= (const String& s, const char* p)
00770 {
00771 return (s.compareTo(p) <= 0);
00772 }
00773 inline bool
00774 operator <= (const char* p, const String& s)
00775 {
00776 return (String(p).compareTo(s) <= 0);
00777 }
00778 inline bool
00779 operator > (const String& s1, const String& s2)
00780 {
00781 return (s1.compareTo(s2) > 0);
00782 }
00783 inline bool
00784 operator > (const String& s, const char* p)
00785 {
00786 return (s.compareTo(p) > 0);
00787 }
00788 inline bool
00789 operator > (const char* p, const String& s)
00790 {
00791 return (String(p).compareTo(s) > 0);
00792 }
00793 inline bool
00794 operator >= (const String& s1, const String& s2)
00795 {
00796 return (s1.compareTo(s2) >= 0);
00797 }
00798 inline bool
00799 operator >= (const String& s, const char* p)
00800 {
00801 return (s.compareTo(p) >= 0);
00802 }
00803 inline bool
00804 operator >= (const char* p, const String& s)
00805 {
00806 return (String(p).compareTo(s) >= 0);
00807 }
00808
00809 }
00810
00811 #endif