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 #include <string>
00047
00048 namespace BLOCXX_NAMESPACE
00049 {
00050
00051 BLOCXX_DECLARE_APIEXCEPTION(StringConversion, BLOCXX_COMMON_API);
00052
00065 class BLOCXX_COMMON_API String
00066 {
00067 public:
00068 class ByteBuf;
00072 String();
00079 explicit String(Int32 val);
00086 explicit String(UInt32 val);
00087
00088 #if defined(BLOCXX_INT32_IS_INT) && defined(BLOCXX_INT64_IS_LONG_LONG)
00089
00095 explicit String(long val);
00102 explicit String(unsigned long val);
00103 #endif
00104
00110 explicit String(Int64 val);
00117 explicit String(UInt64 val);
00124 explicit String(Real32 val);
00131 explicit String(Real64 val);
00137 String(const char* str);
00138
00144 String(const std::string &str);
00145
00146 enum ETakeOwnershipFlag
00147 {
00148 E_TAKE_OWNERSHIP
00149 };
00161 explicit String(ETakeOwnershipFlag, char* allocatedMemory, size_t len);
00168 explicit String(const char* str, size_t len);
00176 String(const String& arg);
00181 explicit String(char c);
00185 ~String();
00189 void swap(String& x);
00196 char* allocateCString() const;
00200 size_t length() const;
00206 size_t UTF8Length() const;
00210 bool empty() const { return length() == 0; }
00219 int format(const char* fmt, ...);
00220 enum EReturnDelimitersFlag
00221 {
00222 E_DISCARD_DELIMITERS,
00223 E_RETURN_DELIMITERS
00224 };
00225 enum EEmptyTokenReturnFlag
00226 {
00227 E_SKIP_EMPTY_TOKENS,
00228 E_RETURN_EMPTY_TOKENS
00229 };
00243 StringArray tokenize(const char* delims = " \n\r\t\v",
00244 EReturnDelimitersFlag returnDelimitersAsTokens = E_DISCARD_DELIMITERS,
00245 EEmptyTokenReturnFlag returnEmptyTokens = E_SKIP_EMPTY_TOKENS ) const;
00250 const char* c_str() const;
00257 char charAt(size_t ndx) const;
00265 int compareTo(const String& arg) const;
00273 int compareTo(const char* arg) const;
00281 int compareToIgnoreCase(const String& arg) const;
00289 int compareToIgnoreCase(const char* arg) const;
00295 String& concat(const char* arg);
00296
00302 String& concat(const String& arg)
00303 {
00304 return concat(arg.c_str());
00305 }
00306
00312 String& concat(char arg);
00313 enum EIgnoreCaseFlag
00314 {
00315 E_CASE_SENSITIVE,
00316 E_CASE_INSENSITIVE
00317 };
00326 bool endsWith(const char* arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const;
00327
00337 bool endsWith(const String& arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const
00338 {
00339 return endsWith(arg.c_str(), ignoreCase);
00340 }
00341
00348 bool endsWith(char arg) const;
00349
00357 bool equals(const String& arg) const;
00365 bool equals(const char* arg) const;
00374 bool equalsIgnoreCase(const String& arg) const;
00383 bool equalsIgnoreCase(const char* arg) const;
00387 UInt32 hashCode() const;
00396 size_t indexOf(char ch, size_t fromIndex=0) const;
00404 size_t indexOf(const char* arg, size_t fromIndex=0) const;
00412 size_t indexOf(const String& arg, size_t fromIndex=0) const
00413 {
00414 return indexOf(arg.c_str(), fromIndex);
00415 }
00416
00425 size_t lastIndexOf(char ch, size_t fromIndex=npos) const;
00434 size_t lastIndexOf(const char* arg, size_t fromIndex=npos) const;
00443 size_t lastIndexOf(const String& arg, size_t fromIndex=npos) const
00444 {
00445 return lastIndexOf(arg.c_str(), fromIndex);
00446 }
00447
00456 bool startsWith(const char* arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const;
00465 bool startsWith(const String& arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const
00466 {
00467 return startsWith(arg.c_str(), ignoreCase);
00468 }
00475 bool startsWith(char arg) const;
00476
00485 String substring(size_t beginIndex,
00486 size_t length=npos) const;
00491 bool isSpaces() const;
00497 String& toLowerCase();
00503 String& toUpperCase();
00509 String& ltrim();
00515 String& rtrim();
00522 String& trim();
00528 String& erase();
00529
00535 String& erase( size_t idx, size_t len = npos );
00542 String& operator= (const String & arg);
00550 const char& operator[] (size_t ndx) const;
00551 char& operator[] (size_t ndx);
00558 String& operator+= (const String& arg) { return concat(arg); }
00565 String& operator+= (const char* arg) { return concat(arg); }
00572 String& operator+= (char arg) { return concat(arg); }
00579 void readObject(std::istream& istrm);
00585 void writeObject(std::ostream& ostrm) const;
00589 String toString() const;
00594 Real32 toReal32() const;
00599 Real64 toReal64() const;
00605 bool toBool() const;
00610 UInt8 toUInt8(int base=10) const;
00615 Int8 toInt8(int base=10) const;
00620 UInt16 toUInt16(int base=10) const;
00625 Int16 toInt16(int base=10) const;
00630 UInt32 toUInt32(int base=10) const;
00635 Int32 toInt32(int base=10) const;
00640 UInt64 toUInt64(int base=10) const;
00645 Int64 toInt64(int base=10) const;
00650 unsigned int toUnsignedInt(int base=10) const;
00655 int toInt(int base=10) const;
00666 static unsigned long long int strtoull(const char* nptr, char** endptr,
00667 int base);
00678 static long long int strtoll(const char* nptr, char** endptr, int base);
00686 static const char* strchr(const char* theStr, int c);
00695 static String getLine(std::istream& istr);
00696
00697 #if defined(BLOCXX_AIX)
00698 static const size_t npos;
00699 #else
00700 static const size_t npos = size_t(~0);
00701 #endif // BLOCXX_AIX
00702
00703 #ifdef BLOCXX_WIN32
00704 #pragma warning (push)
00705 #pragma warning (disable: 4251)
00706 #endif
00707
00708 typedef COWIntrusiveReference<ByteBuf> buf_t;
00709 private:
00710 buf_t m_buf;
00711
00712 #ifdef BLOCXX_WIN32
00713 #pragma warning (pop)
00714 #endif
00715
00716 };
00717 BLOCXX_EXPORT_TEMPLATE(BLOCXX_COMMON_API, Array, String);
00718 BLOCXX_EXPORT_TEMPLATE(BLOCXX_COMMON_API, Enumeration, String);
00719
00720 BLOCXX_COMMON_API std::ostream& operator<< (std::ostream& ostr, const String& arg);
00721 BLOCXX_COMMON_API String operator + (const String& s1, const String& s2);
00722 BLOCXX_COMMON_API String operator + (const char* p, const String& s);
00723 BLOCXX_COMMON_API String operator + (const String& s, const char* p);
00724 BLOCXX_COMMON_API String operator + (char c, const String& s);
00725 BLOCXX_COMMON_API String operator + (const String& s, char c);
00726 inline bool
00727 operator == (const String& s1, const String& s2)
00728 {
00729 return (s1.compareTo(s2) == 0);
00730 }
00731 inline bool
00732 operator == (const String& s, const char* p)
00733 {
00734 return (s.compareTo(p) == 0);
00735 }
00736 inline bool
00737 operator == (const char* p, const String& s)
00738 {
00739 return (s.compareTo(p) == 0);
00740 }
00741 inline bool
00742 operator != (const String& s1, const String& s2)
00743 {
00744 return (s1.compareTo(s2) != 0);
00745 }
00746 inline bool
00747 operator != (const String& s, const char* p)
00748 {
00749 return (s.compareTo(p) != 0);
00750 }
00751 inline bool
00752 operator != (const char* p, const String& s)
00753 {
00754 return (s.compareTo(p) != 0);
00755 }
00756 inline bool
00757 operator < (const String& s1, const String& s2)
00758 {
00759 return (s1.compareTo(s2) < 0);
00760 }
00761 inline bool
00762 operator < (const String& s, const char* p)
00763 {
00764 return (s.compareTo(p) < 0);
00765 }
00766 inline bool
00767 operator < (const char* p, const String& s)
00768 {
00769 return (String(p).compareTo(s) < 0);
00770 }
00771 inline bool
00772 operator <= (const String& s1, const String& s2)
00773 {
00774 return (s1.compareTo(s2) <= 0);
00775 }
00776 inline bool
00777 operator <= (const String& s, const char* p)
00778 {
00779 return (s.compareTo(p) <= 0);
00780 }
00781 inline bool
00782 operator <= (const char* p, const String& s)
00783 {
00784 return (String(p).compareTo(s) <= 0);
00785 }
00786 inline bool
00787 operator > (const String& s1, const String& s2)
00788 {
00789 return (s1.compareTo(s2) > 0);
00790 }
00791 inline bool
00792 operator > (const String& s, const char* p)
00793 {
00794 return (s.compareTo(p) > 0);
00795 }
00796 inline bool
00797 operator > (const char* p, const String& s)
00798 {
00799 return (String(p).compareTo(s) > 0);
00800 }
00801 inline bool
00802 operator >= (const String& s1, const String& s2)
00803 {
00804 return (s1.compareTo(s2) >= 0);
00805 }
00806 inline bool
00807 operator >= (const String& s, const char* p)
00808 {
00809 return (s.compareTo(p) >= 0);
00810 }
00811 inline bool
00812 operator >= (const char* p, const String& s)
00813 {
00814 return (String(p).compareTo(s) >= 0);
00815 }
00816
00817 }
00818
00819 #endif