blocxx
|
00001 /******************************************************************************* 00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved. 00003 * Copyright (C) 2006, Novell, Inc. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of 00014 * Vintela, Inc., 00015 * nor Novell, Inc., 00016 * nor the names of its contributors or employees may be used to 00017 * endorse or promote products derived from this software without 00018 * specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00030 * POSSIBILITY OF SUCH DAMAGE. 00031 *******************************************************************************/ 00032 00033 00039 #ifndef BLOCXX_STRING_HPP_INCLUDE_GUARD_ 00040 #define BLOCXX_STRING_HPP_INCLUDE_GUARD_ 00041 #include "blocxx/BLOCXX_config.h" 00042 #include "blocxx/Types.hpp" 00043 #include "blocxx/COWIntrusiveReference.hpp" 00044 #include "blocxx/CommonFwd.hpp" 00045 #include "blocxx/Exception.hpp" 00046 #include <iosfwd> 00047 #include <string> 00048 00049 namespace BLOCXX_NAMESPACE 00050 { 00051 00052 BLOCXX_DECLARE_APIEXCEPTION(StringConversion, BLOCXX_COMMON_API); 00053 00066 class BLOCXX_COMMON_API String 00067 { 00068 public: 00069 class ByteBuf; 00073 String(); 00080 explicit String(Int32 val); 00087 explicit String(UInt32 val); 00088 // do this check so we fill in the gaps and have int, long & long long constructors if necessary 00089 #if defined(BLOCXX_INT32_IS_INT) && defined(BLOCXX_INT64_IS_LONG_LONG) 00090 00096 explicit String(long val); 00103 explicit String(unsigned long val); 00104 #endif 00105 00111 explicit String(Int64 val); 00118 explicit String(UInt64 val); 00125 explicit String(Real32 val); 00132 explicit String(Real64 val); 00138 String(const char* str); 00145 explicit String(const Char16Array& ra) BLOCXX_DEPRECATED; 00152 explicit String(Bool parm) BLOCXX_DEPRECATED; 00159 explicit String(const Char16& parm) BLOCXX_DEPRECATED; 00165 explicit String(const std::string& str); 00166 00167 enum ETakeOwnershipFlag 00168 { 00169 E_TAKE_OWNERSHIP 00170 }; 00182 explicit String(ETakeOwnershipFlag, char* allocatedMemory, size_t len); 00191 explicit String(const char* str, size_t len); 00199 String(const String& arg); 00204 explicit String(char c); 00208 ~String(); 00212 void swap(String& x); 00219 char* allocateCString() const; 00223 size_t length() const; 00227 size_t size() const { return length(); } 00233 size_t UTF8Length() const; 00237 bool empty() const { return length() == 0; } 00246 int format(const char* fmt, ...); 00247 enum EReturnDelimitersFlag 00248 { 00249 E_DISCARD_TOKENS, // deprecated in 3.1.0 00250 E_RETURN_TOKENS, // deprecated in 3.1.0 00251 E_DISCARD_DELIMITERS, 00252 E_RETURN_DELIMITERS 00253 }; 00254 enum EEmptyTokenReturnFlag 00255 { 00256 E_SKIP_EMPTY_TOKENS, 00257 E_RETURN_EMPTY_TOKENS 00258 }; 00272 StringArray tokenize(const char* delims = " \n\r\t\v", 00273 EReturnDelimitersFlag returnDelimitersAsTokens = E_DISCARD_DELIMITERS, 00274 EEmptyTokenReturnFlag returnEmptyTokens = E_SKIP_EMPTY_TOKENS ) const; 00279 const char* c_str() const; 00284 BLOCXX_DEPRECATED const char* getBytes() const { return c_str(); } 00291 char charAt(size_t ndx) const; 00299 int compareTo(const String& arg) const; 00307 int compareTo(const char* arg) const; 00315 int compareToIgnoreCase(const String& arg) const; 00323 int compareToIgnoreCase(const char* arg) const; 00329 String& concat(const char* arg); 00330 00336 String& concat(const String& arg) 00337 { 00338 return concat(arg.c_str()); 00339 } 00340 00346 String& concat(char arg); 00347 enum EIgnoreCaseFlag 00348 { 00349 E_CASE_SENSITIVE, 00350 E_CASE_INSENSITIVE 00351 }; 00360 bool endsWith(const char* arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const; 00361 00371 bool endsWith(const String& arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const 00372 { 00373 return endsWith(arg.c_str(), ignoreCase); 00374 } 00375 00382 bool endsWith(char arg) const; 00383 00391 bool equals(const String& arg) const; 00399 bool equals(const char* arg) const; 00408 bool equalsIgnoreCase(const String& arg) const; 00417 bool equalsIgnoreCase(const char* arg) const; 00421 UInt32 hashCode() const; 00430 size_t indexOf(char ch, size_t fromIndex=0) const; 00438 size_t indexOf(const char* arg, size_t fromIndex=0) const; 00446 size_t indexOf(const String& arg, size_t fromIndex=0) const 00447 { 00448 return indexOf(arg.c_str(), fromIndex); 00449 } 00450 00459 size_t lastIndexOf(char ch, size_t fromIndex=npos) const; 00468 size_t lastIndexOf(const char* arg, size_t fromIndex=npos) const; 00477 size_t lastIndexOf(const String& arg, size_t fromIndex=npos) const 00478 { 00479 return lastIndexOf(arg.c_str(), fromIndex); 00480 } 00481 00490 bool startsWith(const char* arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const; 00499 bool startsWith(const String& arg, EIgnoreCaseFlag ignoreCase = E_CASE_SENSITIVE) const 00500 { 00501 return startsWith(arg.c_str(), ignoreCase); 00502 } 00509 bool startsWith(char arg) const; 00510 00519 String substring(size_t beginIndex, 00520 size_t length=npos) const; 00525 bool isSpaces() const; 00531 String& toLowerCase(); 00537 String& toUpperCase(); 00544 String& ltrim(); 00551 String& rtrim(); 00558 String& trim(); 00564 String& erase(); 00565 00571 String& erase( size_t idx, size_t len = npos ); 00578 String& operator= (const String & arg); 00586 const char& operator[] (size_t ndx) const; 00587 char& operator[] (size_t ndx); 00594 String& operator+= (const String& arg) { return concat(arg); } 00601 String& operator+= (const char* arg) { return concat(arg); } 00608 String& operator+= (char arg) { return concat(arg); } 00615 void readObject(std::streambuf & istrm); 00621 void writeObject(std::streambuf & ostrm) const; 00625 String toString() const; 00631 Char16 toChar16() const BLOCXX_DEPRECATED; 00636 Real32 toReal32() const; 00641 Real64 toReal64() const; 00647 bool toBool() const; 00652 UInt8 toUInt8(int base=10) const; 00657 Int8 toInt8(int base=10) const; 00662 UInt16 toUInt16(int base=10) const; 00667 Int16 toInt16(int base=10) const; 00672 UInt32 toUInt32(int base=10) const; 00677 Int32 toInt32(int base=10) const; 00682 UInt64 toUInt64(int base=10) const; 00687 Int64 toInt64(int base=10) const; 00692 unsigned int toUnsignedInt(int base=10) const; 00697 int toInt(int base=10) const; 00708 static unsigned long long int strtoull(const char* nptr, char** endptr, 00709 int base); 00720 static long long int strtoll(const char* nptr, char** endptr, int base); 00728 static const char* strchr(const char* theStr, int c); 00737 static String getLine(std::istream& istr); 00738 00739 #if defined(BLOCXX_AIX) 00740 static const size_t npos; 00741 #else 00742 static const size_t npos = size_t(~0); 00743 #endif // BLOCXX_AIX 00744 00745 #ifdef BLOCXX_WIN32 00746 #pragma warning (push) 00747 #pragma warning (disable: 4251) 00748 #endif 00749 00750 typedef COWIntrusiveReference<ByteBuf> buf_t; 00751 private: 00752 buf_t m_buf; 00753 00754 #ifdef BLOCXX_WIN32 00755 #pragma warning (pop) 00756 #endif 00757 00758 }; 00759 BLOCXX_EXPORT_TEMPLATE(BLOCXX_COMMON_API, Array, String); 00760 BLOCXX_EXPORT_TEMPLATE(BLOCXX_COMMON_API, Enumeration, String); 00761 00762 BLOCXX_COMMON_API std::ostream& operator<< (std::ostream& ostr, const String& arg); 00763 BLOCXX_COMMON_API String operator + (const String& s1, const String& s2); 00764 BLOCXX_COMMON_API String operator + (const char* p, const String& s); 00765 BLOCXX_COMMON_API String operator + (const String& s, const char* p); 00766 BLOCXX_COMMON_API String operator + (char c, const String& s); 00767 BLOCXX_COMMON_API String operator + (const String& s, char c); 00768 inline bool 00769 operator == (const String& s1, const String& s2) 00770 { 00771 return (s1.compareTo(s2) == 0); 00772 } 00773 inline bool 00774 operator == (const String& s, const char* p) 00775 { 00776 return (s.compareTo(p) == 0); 00777 } 00778 inline bool 00779 operator == (const char* p, const String& s) 00780 { 00781 return (s.compareTo(p) == 0); 00782 } 00783 inline bool 00784 operator != (const String& s1, const String& s2) 00785 { 00786 return (s1.compareTo(s2) != 0); 00787 } 00788 inline bool 00789 operator != (const String& s, const char* p) 00790 { 00791 return (s.compareTo(p) != 0); 00792 } 00793 inline bool 00794 operator != (const char* p, const String& s) 00795 { 00796 return (s.compareTo(p) != 0); 00797 } 00798 inline bool 00799 operator < (const String& s1, const String& s2) 00800 { 00801 return (s1.compareTo(s2) < 0); 00802 } 00803 inline bool 00804 operator < (const String& s, const char* p) 00805 { 00806 return (s.compareTo(p) < 0); 00807 } 00808 inline bool 00809 operator < (const char* p, const String& s) 00810 { 00811 return (String(p).compareTo(s) < 0); 00812 } 00813 inline bool 00814 operator <= (const String& s1, const String& s2) 00815 { 00816 return (s1.compareTo(s2) <= 0); 00817 } 00818 inline bool 00819 operator <= (const String& s, const char* p) 00820 { 00821 return (s.compareTo(p) <= 0); 00822 } 00823 inline bool 00824 operator <= (const char* p, const String& s) 00825 { 00826 return (String(p).compareTo(s) <= 0); 00827 } 00828 inline bool 00829 operator > (const String& s1, const String& s2) 00830 { 00831 return (s1.compareTo(s2) > 0); 00832 } 00833 inline bool 00834 operator > (const String& s, const char* p) 00835 { 00836 return (s.compareTo(p) > 0); 00837 } 00838 inline bool 00839 operator > (const char* p, const String& s) 00840 { 00841 return (String(p).compareTo(s) > 0); 00842 } 00843 inline bool 00844 operator >= (const String& s1, const String& s2) 00845 { 00846 return (s1.compareTo(s2) >= 0); 00847 } 00848 inline bool 00849 operator >= (const String& s, const char* p) 00850 { 00851 return (s.compareTo(p) >= 0); 00852 } 00853 inline bool 00854 operator >= (const char* p, const String& s) 00855 { 00856 return (String(p).compareTo(s) >= 0); 00857 } 00858 00859 } // end namespace BLOCXX_NAMESPACE 00860 00861 #endif