UString.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    ---------------------------------------------------------------------------
00021 */
00022 
00023 #ifndef __ccxx_UString_hxx
00024 #define __ccxx_UString_hxx
00025 
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/AtomicCounter.h++>
00028 #include <commonc++/UChar.h++>
00029 #include <commonc++/CharRef.h++>
00030 #include <commonc++/CString.h++>
00031 #include <commonc++/Hash.h++>
00032 #include <commonc++/OutOfBoundsException.h++>
00033 
00034 #include <string>
00035 #include <vector>
00036 #include <cerrno>
00037 #include <iosfwd>
00038 
00039 #include <stdio.h> // remove
00040 
00041 namespace ccxx {
00042 
00043 class UStringVec;
00044 
00052 class COMMONCPP_API UString
00053 {
00054   public:
00055 
00057   UString();
00058 
00069   UString(char16_t *str, uint_t offset = 0, uint_t count = 0,
00070           bool copy = true);
00071 
00072   UString(const char16_t *str, uint_t offset = 0, uint_t count = 0);
00073 
00074   UString(const char *str, uint_t offset = 0, uint_t count = 0);
00075 
00077   UString(const UString &other);
00078 
00080   ~UString() throw();
00081 
00090   bool endsWith(const UString &str, bool ignoreCase = false) const throw();
00091 
00100   bool endsWith(const UChar &c, bool ignoreCase = false) const throw();
00101 
00111   bool endsWithAnyOf(const UString &chars, bool ignoreCase = false) const
00112     throw();
00113 
00122   bool startsWith(const UString &str, bool ignoreCase = false) const throw();
00123 
00132   bool startsWith(const UChar &c, bool ignoreCase = false) const throw();
00133 
00143   bool startsWithAnyOf(const UString &chars, bool ignoreCase = false)
00144     const throw();
00145 
00151   uint_t getLength() const throw();
00152 
00157   inline uint_t length() const throw()
00158   { return(getLength()); }
00159 
00166   void setLength(uint_t length);
00167 
00174   void reserve(uint_t capacity);
00175 
00179   uint_t getCapacity() const throw();
00180 
00184   void compact();
00185 
00190   inline UString& clear()
00191   {
00192     setLength(0);
00193     return(*this);
00194   }
00195 
00201   bool isEmpty() const throw();
00202 
00208   bool isNull() const throw();
00209 
00216   bool isInteger() const throw();
00217 
00227   int compareTo(const UString &other, bool ignoreCase = false) const throw();
00228 
00233   UString& trim(const UString &chars = whitespaceChars, bool begin = true,
00234                bool end = true);
00235 
00241   inline UString& trimBegin(const UString &chars = whitespaceChars)
00242   { return(trim(chars, true, false)); }
00243 
00249   inline UString& trimEnd(const UString &chars = whitespaceChars)
00250   { return(trim(chars, false, true)); }
00251 
00257   UString& chop(const UChar &c);
00258 
00264   UString& reverseChop(const UChar &c);
00265 
00272   UString& replace(const UChar &a, const UChar &b);
00273 
00280   UString& replace(const UString &a, const UString &b);
00281 
00288   UString& insert(const UChar &c, uint_t index = 0);
00289 
00298   UString& insert(const UString &str, uint_t index = 0, uint_t length = 0);
00299 
00300   UString& insert(const char16_t *str, uint_t index = 0, uint_t count = 0);
00301 
00302   inline UString& insert(const char *str, uint_t index = 0,
00303                          uint_t count = 0)
00304   { return(insert(UString(str), index, count)); }
00305 
00311   UString& append(bool v);
00312 
00313   inline UString& append(char c)
00314   { return(append(UChar(c))); }
00315 
00321   UString& append(int val);
00322 
00330   UString& append(int val, uint_t base);
00331 
00337   UString& append(uint_t val);
00338 
00346   UString& append(uint_t val, uint_t base);
00347 
00353   UString& append(const int64_t &val);
00354 
00362   UString& append(const double &val, uint_t precision = 6);
00363 
00369   UString& append(const uint64_t &val);
00370 
00375   inline UString& append(const UChar &c)
00376   { return(insert(c, END)); }
00377 
00384   inline UString& append(const UString &str, uint_t count = 0)
00385   { return(insert(str, END, count)); }
00386 
00387   inline UString& append(const char16_t *str, uint_t count = 0)
00388   { return(insert(str, END, count)); }
00389 
00396   inline UString& append(const char *str, uint_t count = 0)
00397   { return(insert(UString(str), END, count)); }
00398 
00403   inline UString& prepend(const UChar &c)
00404   { return(insert(c, 0)); }
00405 
00412   inline UString& prepend(const UString &str, uint_t count = 0)
00413   { return(insert(str, 0, count)); }
00414 
00415   inline UString& prepend(const char16_t *str, uint_t count = 0)
00416   { return(insert(str, 0, count)); }
00417 
00418   inline UString& prepend(const char *str, uint_t count = 0)
00419   { return(insert(UString(str), 0, count)); }
00420 
00427   UString& remove(uint_t fromIndex, uint_t toIndex = END);
00428 
00429   UString& setCharAt(uint_t index, const UChar &c) throw(OutOfBoundsException);
00430 
00436   uint_t hash(uint_t modulo = 256) const throw();
00437 
00446   UString substring(uint_t fromIndex, uint_t toIndex = END) const;
00447 
00455   int indexOf(const UChar& c, uint_t fromIndex = 0) const throw();
00456 
00464   inline int indexOf(const char *str, uint_t fromIndex = 0) const throw()
00465   { return(indexOf(UString(str), fromIndex)); }
00466 
00475   int indexOfAnyOf(const UString &chars, uint_t fromIndex = 0) const throw();
00476 
00484   int indexOf(const UString &str, uint_t fromIndex = 0) const throw();
00485 
00494   int lastIndexOf(const UChar &c, uint_t fromIndex = END) const throw();
00495 
00505   int lastIndexOfAnyOf(const UString &chars, uint_t fromIndex = END) const
00506     throw();
00507 
00516   int lastIndexOf(const UString &str, uint_t fromIndex = END) const throw();
00517 
00524   inline bool contains(const UChar &c) const throw()
00525   { return(indexOf(c) >= 0); }
00526 
00533   inline bool contains(const UString &str) const throw()
00534   { return(indexOf(str) >= 0); }
00535 
00543   inline bool containsAnyOf(const char *chars) const throw()
00544   { return(indexOfAnyOf(chars) >= 0); }
00545 
00556   UString nextToken(uint_t &fromIndex, const UString &delim = whitespaceChars)
00557     const;
00558 
00571   uint_t split(UStringVec &tokens, const UString &delim = whitespaceChars,
00572                uint_t limit = 0) const;
00573 
00575   UString& toLowerCase();
00576 
00578   UString& toUpperCase();
00579 
00585   UString& fill(const UChar &c, uint_t count);
00586 
00592   UString& pad(const UChar &c, uint_t count);
00593 
00599   bool toBool(uint_t index = 0) const throw();
00600 
00607   int toInt(uint_t index = 0, uint_t base = 10) const throw();
00608 
00615   uint_t toUInt(uint_t index = 0, uint_t base = 10) const throw();
00616 
00623   int64_t toInt64(uint_t index = 0, uint_t base = 10) const throw();
00624 
00631   uint64_t toUInt64(uint_t index = 0, uint_t base = 10) const throw();
00632 
00638   float toFloat(uint_t index = 0) const throw();
00639 
00645   double toDouble(uint_t index = 0) const throw();
00646 
00648   operator const void *() const throw();
00649 
00650   const char16_t* data() const throw();
00651 
00653   CString toUTF8() const;
00654 
00656   UString& operator=(const char *other);
00657 
00659   UString& operator=(const UString &other);
00660 
00662   inline UString& operator=(const std::string &other)
00663   { return(operator=(other.c_str())); }
00664 
00672   CharRef operator[](int index) throw(OutOfBoundsException);
00673 
00681   const UChar operator[](int index) const throw(OutOfBoundsException);
00682 
00691   inline UString operator()(uint_t fromIndex, uint_t toIndex = END) const
00692   { return(substring(fromIndex, toIndex)); }
00693 
00699   inline bool operator!() const throw()
00700   { return(isNull()); }
00701 
00706   inline UString& operator+=(const UChar &c)
00707   { return(append(c)); }
00708 
00713   inline UString& operator+=(const char *str)
00714   { return(append(str)); }
00715 
00720   inline UString& operator+=(const UString &str)
00721   { return(append(str)); }
00722 
00728   inline UString& operator+=(bool val)
00729   { return(append(val)); }
00730 
00736   inline UString& operator+=(int val)
00737   { return(append(val)); }
00738 
00744   inline UString& operator+=(uint_t val)
00745   { return(append(val)); }
00746 
00752   inline UString& operator+=(double val)
00753   { return(append(val)); }
00754 
00760   inline UString& operator+=(int64_t val)
00761   { return(append(val)); }
00762 
00768   inline UString& operator+=(uint64_t val)
00769   { return(append(val)); }
00770 
00772   inline UString& operator<<(const char *str)
00773   { return(append(str)); }
00774 
00776   inline UString& operator<<(const UString &str)
00777   { return(append(str)); }
00778 
00780   inline UString& operator<<(bool val)
00781   { return(append(val)); }
00782 
00784   inline UString& operator<<(const UChar &c)
00785   { return(append(c)); }
00786 
00788   inline UString& operator<<(int val)
00789   { return(append(val)); }
00790 
00792   inline UString& operator<<(uint_t val)
00793   { return(append(val)); }
00794 
00796   inline UString& operator<<(const int64_t &val)
00797   { return(append(val)); }
00798 
00800   inline UString& operator<<(const uint64_t &val)
00801   { return(append(val)); }
00802 
00804   inline UString& operator<<(const double &val)
00805   { return(append(val)); }
00806 
00808   inline UString& operator^=(const UChar &c)
00809   { return(prepend(c)); }
00810 
00812   inline UString& operator^=(const char *str)
00813   { return(prepend(str)); }
00814 
00816   inline UString& operator^=(const UString &str)
00817   { return(prepend(str)); }
00818 
00819   UString operator+(const UString &str);
00820   UString operator+(const char *str);
00821   UString operator+(const UChar &c);
00822 
00824   void write(std::ostream& stream) const;
00825 
00827   static const uint_t END;
00828 
00830   static const UString null;
00831 
00833   static const UString empty;
00834 
00836   static const UString whitespaceChars;
00837 
00838   private:
00839 
00840   class StringBuf;
00841 
00842   StringBuf* _buf;
00843 
00844   void _insert(const char16_t *str, uint_t length, uint_t index, uint_t count);
00845   void _release();
00846   void _makeCopy(size_t size, bool unshareable = false);
00847   char16_t* _makeGap(uint_t index, uint_t gaplen);
00848   void _fromUTF8(const char *str, uint_t len, uint_t offset);
00849 };
00850 
00857 class UStringVec : public std::vector<UString>
00858 {
00859   public:
00860 
00861   UStringVec()
00862   { }
00863 
00865   inline UStringVec& operator<<(UString s)
00866   { push_back(s); return(*this); }
00867 
00869   static const UStringVec emptyVec;
00870 };
00871 
00872 inline bool operator==(const UString &s1, const UString &s2) throw()
00873 { return(s1.compareTo(s2) == 0); }
00874 
00875 inline bool operator==(const UString &s1, const char *s2) throw()
00876 { return(s1.compareTo(s2) == 0); }
00877 
00878 inline bool operator==(const char *s1, const UString &s2) throw()
00879 { return(s2.compareTo(s1) == 0); }
00880 
00881 inline bool operator!=(const UString &s1, const UString &s2) throw()
00882 { return(s1.compareTo(s2) != 0); }
00883 
00884 inline bool operator!=(const UString &s1, const char *s2) throw()
00885 { return(s1.compareTo(s2) != 0); }
00886 
00887 inline bool operator!=(const char *s1, const UString &s2) throw()
00888 { return(s2.compareTo(s1) != 0); }
00889 
00890 inline bool operator<(const UString &s1, const UString &s2) throw()
00891 { return(s1.compareTo(s2) < 0); }
00892 
00893 inline bool operator<(const UString &s1, const char *s2) throw()
00894 { return(s1.compareTo(s2) < 0); }
00895 
00896 inline bool operator<(const char *s1, const UString &s2) throw()
00897 { return(s2.compareTo(s1) > 0); }
00898 
00899 inline bool operator>(const UString &s1, const UString &s2) throw()
00900 { return(s1.compareTo(s2) > 0); }
00901 
00902 inline bool operator>(const UString &s1, const char *s2) throw()
00903 { return(s1.compareTo(s2) > 0); }
00904 
00905 inline bool operator>(const char *s1, const UString &s2) throw()
00906 { return(s2.compareTo(s1) < 0); }
00907 
00908 inline bool operator<=(const UString &s1, const UString &s2) throw()
00909 { return(s1.compareTo(s2) <= 0); }
00910 
00911 inline bool operator<=(const UString &s1, const char *s2) throw()
00912 { return(s1.compareTo(s2) <= 0); }
00913 
00914 inline bool operator<=(const char *s1, const UString &s2) throw()
00915 { return(s2.compareTo(s1) > 0); }
00916 
00917 inline bool operator>=(const UString &s1, const UString &s2) throw()
00918 { return(s1.compareTo(s2) >= 0); }
00919 
00920 inline bool operator>=(const UString &s1, const char *s2) throw()
00921 { return(s1.compareTo(s2) >= 0); }
00922 
00923 inline bool operator>=(const char *s1, const UString &s2) throw()
00924 { return(s2.compareTo(s1) < 0); }
00925 
00926 inline UString operator+(const char *s1, const UString &s2)
00927 { return(UString(s1) + s2); }
00928 
00929 inline UString operator+(const UChar &c, const UString &s)
00930 { UString t; t += c; t += s; return(t); }
00931 
00932 inline std::ostream& operator<<(std::ostream &stream, const UString &s)
00933 { s.write(stream); return(stream); }
00934 
00935 } // namespace ccxx
00936 
00937 #endif // __ccxx_String_hxx
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3