00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __ccxx_BasicString_hxx
00024 #define __ccxx_BasicString_hxx
00025
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/AtomicCounter.h++>
00028 #include <commonc++/Blob.h++>
00029 #include <commonc++/Char.h++>
00030 #include <commonc++/CharsetConverter.h++>
00031 #include <commonc++/CharTraits.h++>
00032 #include <commonc++/OutOfBoundsException.h++>
00033
00034 #include <string>
00035 #include <vector>
00036 #include <sstream>
00037 #include <cerrno>
00038
00039 #ifdef _MSC_VER
00040 #pragma warning(push)
00041 #pragma warning(disable: 4251)
00042 #endif // _MSC_VER
00043
00044 namespace ccxx {
00045
00046 template<typename T> class BasicStringVec;
00047
00054 template<typename T> class BasicString
00055 {
00056 public:
00057
00059 BasicString();
00060
00071 BasicString(T *str, size_t offset = 0, size_t length = 0,
00072 bool copy = true);
00073
00075 BasicString(const BasicString<T>& other);
00076
00085 BasicString(const T *other, size_t offset = 0, size_t length = 0);
00086
00093 BasicString(const byte_t *data, size_t length, const char *encoding);
00094
00096 virtual ~BasicString() throw();
00097
00106 bool endsWith(const BasicString<T> &str, bool ignoreCase = false)
00107 const throw();
00108
00117 bool endsWith(T c, bool ignoreCase = false) const throw();
00118
00128 bool endsWithAnyOf(const T *chars, bool ignoreCase = false) const throw();
00129
00138 bool startsWith(const BasicString<T> &str, bool ignoreCase = false)
00139 const throw();
00140
00149 bool startsWith(T c, bool ignoreCase = false) const throw();
00150
00160 bool startsWithAnyOf(const T *chars, bool ignoreCase = false) const throw();
00161
00167 size_t getLength() const throw();
00168
00173 inline size_t length() const throw()
00174 { return(getLength()); }
00175
00182 void setLength(uint_t length);
00183
00190 void reserve(uint_t capacity);
00191
00195 size_t getCapacity() const throw();
00196
00200 void compact();
00201
00206 inline BasicString<T>& clear()
00207 {
00208 setLength(0);
00209 return(*this);
00210 }
00211
00217 bool isEmpty() const throw();
00218
00224 bool isNull() const throw();
00225
00232 bool isAlphaNumeric() const throw();
00233
00240 bool isInteger() const throw();
00241
00251 int compareTo(const T *other, bool ignoreCase = false) const throw();
00252
00262 int compareTo(const BasicString<T> &other, bool ignoreCase = false) const
00263 throw();
00264
00269 BasicString<T>& trim(const T *chars = whitespaceChars, bool begin = true,
00270 bool end = true);
00271
00277 inline BasicString<T>& trimBegin(const T *chars = whitespaceChars)
00278 { return(trim(chars, true, false)); }
00279
00285 inline BasicString<T>& trimEnd(const T *chars = whitespaceChars)
00286 { return(trim(chars, false, true)); }
00287
00293 BasicString<T>& chop(T c);
00294
00300 BasicString<T>& reverseChop(T c);
00301
00308 BasicString<T>& replace(T a, T b);
00309
00316 inline BasicString<T>& replace(const BasicString<T> &a,
00317 const BasicString<T> &b)
00318 { return(replace(a.c_str(), b.c_str())); }
00319
00326 BasicString<T>& replace(const T *a, const T *b);
00327
00333 BasicString<T>& printf(const T *format, ...);
00334
00341 BasicString<T>& insert(T c, uint_t index = 0);
00342
00351 BasicString<T>& insert(const T *str, uint_t index = 0, size_t length = 0);
00352
00361 BasicString<T>& insert(const BasicString<T>& str, uint_t index = 0,
00362 size_t length = 0);
00363
00372 inline BasicString<T>& insert(const std::basic_string<T> &str,
00373 uint_t index = 0, size_t length = 0)
00374 { return(insert(str.c_str(), index, length)); }
00375
00381 BasicString<T>& append(bool v);
00382
00388 BasicString<T>& append(int val);
00389
00397 BasicString<T>& append(int val, uint_t base);
00398
00404 BasicString<T>& append(uint_t val);
00405
00413 BasicString<T>& append(uint_t val, uint_t base);
00414
00420 BasicString<T>& append(int64_t val);
00421
00429 BasicString<T>& append(double val, uint_t precision = 6);
00430
00436 BasicString<T>& append(uint64_t val);
00437
00442 inline BasicString<T>& append(T c)
00443 { return(insert(c, END)); }
00444
00451 inline BasicString<T>& append(const T *str, size_t length = 0)
00452 { return(insert(str, END, length)); }
00453
00460 inline BasicString<T>& append(const BasicString<T> &str,
00461 size_t length = 0)
00462 { return(insert(str, END, length)); }
00463
00470 inline BasicString<T>& append(const std::basic_string<T> &str,
00471 size_t length = 0)
00472 { return(insert(str.c_str(), END, length)); }
00473
00478 inline BasicString<T>& prepend(T c)
00479 { return(insert(c, 0)); }
00480
00487 inline BasicString<T>& prepend(const T *str, size_t length = 0)
00488 { return(insert(str, 0, length)); }
00489
00496 inline BasicString<T>& prepend(const BasicString<T>& str, size_t length = 0)
00497 { return(insert(str, 0, length)); }
00498
00505 inline BasicString<T>& prepend(const std::basic_string<T> &str,
00506 size_t length = 0)
00507 { return(insert(str.c_str(), 0, length)); }
00508
00515 BasicString<T>& remove(uint_t fromIndex, uint_t toIndex = END);
00516
00522 uint_t hash(uint_t modulo = 256) const throw();
00523
00532 BasicString<T> substring(uint_t fromIndex, uint_t toIndex = END) const;
00533
00541 int indexOf(T c, uint_t fromIndex = 0) const throw();
00542
00550 int indexOf(const T *str, uint_t fromIndex = 0) const throw();
00551
00560 int indexOfAnyOf(const T *chars, uint_t fromIndex = 0) const throw();
00561
00569 inline int indexOf(const BasicString<T>& str, uint_t fromIndex = 0) const
00570 throw()
00571 { return(indexOf(str.c_str(), fromIndex)); }
00572
00580 inline int indexOf(const std::basic_string<T> &str, uint_t fromIndex = 0)
00581 const throw()
00582 { return(indexOf(str.c_str(), fromIndex)); }
00583
00592 int lastIndexOf(T c, uint_t fromIndex = END) const throw();
00593
00603 int lastIndexOfAnyOf(const T *chars, uint_t fromIndex = END) const throw();
00604
00613 int lastIndexOf(const T *str, uint_t fromIndex = END) const throw();
00614
00623 inline int lastIndexOf(const BasicString<T> &str, uint_t fromIndex = END)
00624 const throw()
00625 { return(lastIndexOf(str.c_str(), fromIndex)); }
00626
00635 inline int lastIndexOf(const std::basic_string<T> &str,
00636 uint_t fromIndex = END) const throw()
00637 { return(lastIndexOf(str.c_str(), fromIndex)); }
00638
00645 inline bool contains(T c) const throw()
00646 { return(indexOf(c) >= 0); }
00647
00655 inline bool containsAnyOf(const T *chars) const throw()
00656 { return(indexOfAnyOf(chars) >= 0); }
00657
00664 inline bool contains(const T *str) const throw()
00665 { return(indexOf(str) >= 0); }
00666
00673 inline bool contains(const BasicString<T> &str) const throw()
00674 { return(indexOf(str) >= 0); }
00675
00682 inline bool contains(const std::basic_string<T> &str) const throw()
00683 { return(indexOf(str) >= 0); }
00684
00695 BasicString<T> nextToken(uint_t &fromIndex,
00696 const T *delim = whitespaceChars) const;
00697
00710 uint_t split(BasicStringVec<T> &tokens, const T *delim = whitespaceChars,
00711 uint_t limit = 0) const;
00712
00714 BasicString<T>& toLowerCase();
00715
00717 BasicString<T>& toUpperCase();
00718
00724 BasicString<T>& fill(T c, size_t length);
00725
00731 BasicString<T>& pad(T c, size_t length);
00732
00738 bool toBool(uint_t index = 0) const throw();
00739
00746 int toInt(uint_t index = 0, uint_t base = 10) const throw();
00747
00754 uint_t toUInt(uint_t index = 0, uint_t base = 10) const throw();
00755
00762 int64_t toInt64(uint_t index = 0, uint_t base = 10) const throw();
00763
00770 uint64_t toUInt64(uint_t index = 0, uint_t base = 10) const throw();
00771
00777 float toFloat(uint_t index = 0) const throw();
00778
00784 double toDouble(uint_t index = 0) const throw();
00785
00787 operator const void *() const throw();
00788
00790 const T * c_str() const throw();
00791
00804 Blob getBytes(const char *encoding, size_t length = 0,
00805 size_t maxEncodedLength = 0) const;
00806
00808 BasicString<T>& operator=(const T *other);
00809
00811 BasicString<T>& operator=(const BasicString<T> &other);
00812
00814 inline BasicString<T>& operator=(const std::basic_string<T> &other)
00815 { return(operator=(other.c_str())); }
00816
00824 T& operator[](int index) throw(OutOfBoundsException);
00825
00833 BasicChar<T> operator[](int index) const throw(OutOfBoundsException);
00834
00843 inline BasicString<T> operator()(uint_t fromIndex, uint_t toIndex = END)
00844 const
00845 { return(substring(fromIndex, toIndex)); }
00846
00852 inline bool operator!() const throw()
00853 { return(isNull()); }
00854
00859 inline BasicString<T>& operator+=(T c)
00860 { return(append(c)); }
00861
00866 inline BasicString<T>& operator+=(const T *str)
00867 { return(append(str)); }
00868
00873 inline BasicString<T>& operator+=(const BasicString<T> &str)
00874 { return(append(str)); }
00875
00880 inline BasicString<T>& operator+=(const std::basic_string<T> &str)
00881 { return(append(str.c_str())); }
00882
00888 inline BasicString<T>& operator+=(bool val)
00889 { return(append(val)); }
00890
00896 inline BasicString<T>& operator+=(int val)
00897 { return(append(val)); }
00898
00904 inline BasicString<T>& operator+=(uint_t val)
00905 { return(append(val)); }
00906
00912 inline BasicString<T>& operator+=(double val)
00913 { return(append(val)); }
00914
00920 inline BasicString<T>& operator+=(int64_t val)
00921 { return(append(val)); }
00922
00928 inline BasicString<T>& operator+=(uint64_t val)
00929 { return(append(val)); }
00930
00932 inline BasicString<T>& operator<<(const T *str)
00933 { return(append(str)); }
00934
00936 inline BasicString<T>& operator<<(const BasicString<T> &str)
00937 { return(append(str)); }
00938
00940 inline BasicString<T>& operator<<(const std::basic_string<T> &str)
00941 { return(append(str)); }
00942
00944 inline BasicString<T>& operator<<(bool val)
00945 { return(append(val)); }
00946
00948 inline BasicString<T>& operator<<(T c)
00949 { return(append(c)); }
00950
00952 inline BasicString<T>& operator<<(int val)
00953 { return(append(val)); }
00954
00956 inline BasicString<T>& operator<<(uint_t val)
00957 { return(append(val)); }
00958
00960 inline BasicString<T>& operator<<(int64_t val)
00961 { return(append(val)); }
00962
00964 inline BasicString<T>& operator<<(uint64_t val)
00965 { return(append(val)); }
00966
00968 inline BasicString<T>& operator<<(double val)
00969 { return(append(val)); }
00970
00972 inline BasicString<T>& operator^=(T c)
00973 { return(prepend(c)); }
00974
00976 inline BasicString<T>& operator^=(const T *str)
00977 { return(prepend(str)); }
00978
00980 inline BasicString<T>& operator^=(const BasicString<T> &str)
00981 { return(prepend(str)); }
00982
00984 inline BasicString<T>& operator^=(const std::basic_string<T> &str)
00985 { return(prepend(str)); }
00986
00987 BasicString<T> operator+(const BasicString<T> &str);
00988 BasicString<T> operator+(const T *str);
00989 BasicString<T> operator+(T c);
00990
00992 static BasicString<T> fromCharArray(const char *str, size_t length = 0);
00993
00995 static BasicString<T> fromUTF8(const char *str);
00996
00998 static BasicString<T> fromWCharArray(const wchar_t *str, size_t length = 0);
00999
01001 static const uint_t END;
01002
01004 static const BasicString<T> null;
01005
01007 static const BasicString<T> empty;
01008
01010 static const T *whitespaceChars;
01011
01012 private:
01013
01014 class StringBuf
01015 {
01016 friend class BasicString<T>;
01017
01018 protected:
01019
01020 T *_data;
01021 size_t _capacity;
01022 size_t _length;
01023 AtomicCounter _refs;
01024
01025 StringBuf(const StringBuf& other, size_t length);
01026 StringBuf(T *str = NULL, size_t offset = 0, size_t length = 0,
01027 bool copy = true);
01028 StringBuf(const T *str, size_t offset = 0, size_t length = 0);
01029 ~StringBuf() throw();
01030
01031 void reserve(size_t size);
01032 void compact();
01033 void makeNull();
01034 void makeCopy(const T *str, size_t offset, size_t length);
01035
01036 private:
01037
01038 CCXX_COPY_DECLS(StringBuf);
01039 };
01040
01041 StringBuf *_buf;
01042
01043 void _release();
01044 void _makeCopy(size_t size, bool unshareable = false);
01045 T *_makeGap(uint_t index, size_t gaplen);
01046 void _encode(const byte_t *data, size_t length, const char *encoding);
01047 };
01048
01055 template<typename T> class BasicStringVec
01056 : public std::vector<BasicString<T> >
01057 {
01058 public:
01059
01060 BasicStringVec()
01061 { }
01062
01064 inline BasicStringVec<T>& operator<<(BasicString<T> s)
01065 { push_back(s); return(*this); }
01066
01068 static const BasicStringVec<T> emptyVec;
01069 };
01070
01071 template<typename T>
01072 inline bool operator==(const BasicString<T> &s1, const BasicString<T> &s2)
01073 throw()
01074 { return(s1.compareTo(s2) == 0); }
01075
01076 template<typename T>
01077 inline bool operator==(const BasicString<T> &s1, const T *s2) throw()
01078 { return(s1.compareTo(s2) == 0); }
01079
01080 template<typename T>
01081 inline bool operator==(const T *s1, const BasicString<T> &s2) throw()
01082 { return(s2.compareTo(s1) == 0); }
01083
01084 template<typename T>
01085 inline bool operator!=(const BasicString<T> &s1, const BasicString<T> &s2)
01086 throw()
01087 { return(s1.compareTo(s2) != 0); }
01088
01089 template<typename T>
01090 inline bool operator!=(const BasicString<T> &s1, const T *s2) throw()
01091 { return(s1.compareTo(s2) != 0); }
01092
01093 template<typename T>
01094 inline bool operator!=(const T *s1, const BasicString<T> &s2) throw()
01095 { return(s2.compareTo(s1) != 0); }
01096
01097 template<typename T>
01098 inline bool operator<(const BasicString<T> &s1, const BasicString<T> &s2)
01099 throw()
01100 { return(s1.compareTo(s2) < 0); }
01101
01102 template<typename T>
01103 inline bool operator<(const BasicString<T> &s1, const T *s2) throw()
01104 { return(s1.compareTo(s2) < 0); }
01105
01106 template<typename T>
01107 inline bool operator<(const T *s1, const BasicString<T> &s2) throw()
01108 { return(s2.compareTo(s1) > 0); }
01109
01110 template<typename T>
01111 inline bool operator>(const BasicString<T> &s1, const BasicString<T> &s2)
01112 throw()
01113 { return(s1.compareTo(s2) > 0); }
01114
01115 template<typename T>
01116 inline bool operator>(const BasicString<T> &s1, const T *s2) throw()
01117 { return(s1.compareTo(s2) > 0); }
01118
01119 template<typename T>
01120 inline bool operator>(const T *s1, const BasicString<T> &s2) throw()
01121 { return(s2.compareTo(s1) < 0); }
01122
01123 template<typename T>
01124 inline bool operator<=(const BasicString<T> &s1, const BasicString<T> &s2)
01125 throw()
01126 { return(s1.compareTo(s2) <= 0); }
01127
01128 template<typename T>
01129 inline bool operator<=(const BasicString<T> &s1, const T *s2) throw()
01130 { return(s1.compareTo(s2) <= 0); }
01131
01132 template<typename T>
01133 inline bool operator<=(const T *s1, const BasicString<T> &s2) throw()
01134 { return(s2.compareTo(s1) > 0); }
01135
01136 template<typename T>
01137 inline bool operator>=(const BasicString<T> &s1, const BasicString<T> &s2)
01138 throw()
01139 { return(s1.compareTo(s2) >= 0); }
01140
01141 template<typename T>
01142 inline bool operator>=(const BasicString<T> &s1, const T *s2) throw()
01143 { return(s1.compareTo(s2) >= 0); }
01144
01145 template<typename T>
01146 inline bool operator>=(const T *s1, const BasicString<T> &s2) throw()
01147 { return(s2.compareTo(s1) < 0); }
01148
01149 template<typename T>
01150 inline BasicString<T> operator+(const T *s1, const BasicString<T> &s2)
01151 { return(BasicString<T>(s1) + s2); }
01152
01153 template<typename T>
01154 inline BasicString<T> operator+(T c, const BasicString<T> &s)
01155 { BasicString<T> t; t += c; t += s; return(t); }
01156
01157 template<typename T>
01158 inline std::ostream& operator<<(std::ostream& stream,
01159 const BasicString<T> &s)
01160 { return(stream << (s ? s.c_str() : BasicCharTraits<T>::nullRep())); }
01161
01162 #include <commonc++/BasicStringImpl.h++>
01163
01164 };
01165
01166 #ifdef _MSC_VER
01167 #pragma warning(pop)
01168 #endif // _MSC_VER
01169
01170 #endif // __ccxx_BasicString_hxx
01171
01172