Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

/home/tommi/cxxtools/include/cxxtools/string.h

00001 /*
00002  * Copyright (C) 2004-2007 Marc Boris Duerner
00003  * 
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  * 
00009  * As a special exception, you may use this file as part of a free
00010  * software library without restriction. Specifically, if other files
00011  * instantiate templates or use macros or inline functions from this
00012  * file, or you compile this file and link it with other files to
00013  * produce an executable, this file does not by itself cause the
00014  * resulting executable to be covered by the GNU General Public
00015  * License. This exception does not however invalidate any other
00016  * reasons why the executable file might be covered by the GNU Library
00017  * General Public License.
00018  * 
00019  * This library is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022  * Lesser General Public License for more details.
00023  * 
00024  * You should have received a copy of the GNU Lesser General Public
00025  * License along with this library; if not, write to the Free Software
00026  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00027  */
00028 
00029 #ifndef CXXTOOLS_STRING_H
00030 #define CXXTOOLS_STRING_H
00031 
00032 #include <cxxtools/api.h>
00033 #include <cxxtools/char.h>
00034 #include <cxxtools/stringdata.h>
00035 
00036 #include <string>
00037 #include <iterator>
00038 #include <cassert>
00039 #include <stdexcept>
00040 
00041 
00042 namespace std {
00043 
00047 template <>
00048 class basic_string< cxxtools::Char > {
00049     public:
00050         typedef cxxtools::Char value_type;
00051         typedef size_t size_type;
00052         typedef char_traits< cxxtools::Char > traits_type;
00053         typedef std::allocator<cxxtools::Char> allocator_type;
00054         typedef allocator_type::difference_type difference_type;
00055         typedef allocator_type::reference reference;
00056         typedef allocator_type::const_reference const_reference;
00057         typedef allocator_type::pointer pointer;
00058         typedef allocator_type::const_pointer const_pointer;
00059         typedef value_type* iterator;
00060         typedef const value_type* const_iterator;
00061 
00062         typedef std::reverse_iterator<iterator> reverse_iterator;
00063         typedef const std::reverse_iterator<const_iterator> const_reverse_iterator;
00064 
00065         static const size_type npos = static_cast<size_type>(-1);
00066 
00067     public:
00068         basic_string();
00069 
00070         explicit basic_string( const allocator_type& a );
00071 
00072         basic_string(const cxxtools::Char* str, const allocator_type& a = allocator_type());
00073 
00074         basic_string(const wchar_t* str, const allocator_type& a = allocator_type());
00075 
00076         basic_string(const wchar_t* str, size_type n, const allocator_type& a = allocator_type());
00077 
00078         basic_string(const cxxtools::Char* str, size_type n, const allocator_type& a = allocator_type());
00079 
00080         basic_string(size_type n, cxxtools::Char c);
00081 
00082         basic_string(const basic_string& str);
00083 
00084         basic_string(const basic_string& str, size_type pos);
00085 
00086         basic_string(const basic_string& str, size_type pos, size_type n);
00087 
00088         basic_string(const basic_string& str, size_type pos, size_type n, const allocator_type& a);
00089 
00090         basic_string(const cxxtools::Char* begin, const cxxtools::Char* end);
00091 
00092         ~basic_string();
00093 
00094     public:
00095         iterator begin();
00096 
00097         iterator end();
00098 
00099         const_iterator begin() const;
00100 
00101         const_iterator end() const;
00102 
00103         reverse_iterator rbegin()
00104         { return reverse_iterator( this->end() ); }
00105 
00106         reverse_iterator rend()
00107         { return reverse_iterator( this->begin() ); }
00108 
00109         const_reverse_iterator rbegin() const
00110         { return const_reverse_iterator( this->end() ); }
00111 
00112         const_reverse_iterator rend()   const
00113         { return const_reverse_iterator( this->begin() ); }
00114 
00115         reference operator[](size_type n)
00116         {
00117             this->detach( _data->length() );
00118             _data->setBusy();
00119             return *(_data->str() + n);
00120         }
00121 
00122         const_reference operator[](size_type n) const
00123         { return *(_data->str() + n); }
00124 
00125         reference at(size_type n)
00126         {
00127             if( n > this->size() ) {
00128                 throw std::out_of_range("The given at-value is out of range");
00129             }
00130             this->detach( _data->length() );
00131             _data->setBusy();
00132             return *(_data->str() + n);
00133         }
00134 
00135         const_reference at(size_type n) const
00136         {
00137             if( n > this->size() ) {
00138                 throw std::out_of_range("The given at-value is out of range");
00139             }
00140             return *(_data->str() + n);
00141         }
00142 
00143     public:
00144         void push_back(cxxtools::Char ch)
00145         { this->append(1, ch); }
00146 
00147         // untested
00148         void resize( size_t n, cxxtools::Char ch = value_type() );
00149 
00150         // untested
00151         void reserve(size_t n = 0);
00152 
00153         void swap(basic_string& str);
00154 
00155         allocator_type get_allocator() const
00156         { return _data->get_allocator(); }
00157 
00158         size_type copy(cxxtools::Char* a, size_type n, size_type pos = 0) const;
00159 
00160         basic_string substr(size_type pos, size_type n) const
00161         { return basic_string(*this, pos, n); }
00162 
00163         basic_string substr(size_type pos = 0) const
00164         { return basic_string(*this, pos); }
00165 
00166     public:
00167         size_type length() const;
00168 
00169         size_type size() const;
00170 
00171         bool empty() const;
00172 
00173         size_type max_size() const;
00174 
00175         size_type capacity() const;
00176 
00177         const cxxtools::Char* data() const
00178         { return this->c_str(); }
00179 
00180         const cxxtools::Char* c_str() const;
00181 
00182         basic_string& assign(const basic_string& str);
00183 
00184         basic_string& assign(const basic_string& str, size_type pos, size_type n);
00185 
00186         basic_string& assign(const cxxtools::Char* str);
00187 
00188         basic_string& assign(const cxxtools::Char* str, size_type length);
00189 
00190         basic_string& assign(size_type n, cxxtools::Char c);
00191 
00192         basic_string& append(const cxxtools::Char* str);
00193 
00194         basic_string& append(const cxxtools::Char* str, size_type n);
00195 
00196         basic_string& append(size_type n, cxxtools::Char ch);
00197 
00198         basic_string& append(const basic_string& str);
00199 
00200         basic_string& append(const basic_string& str, size_type pos, size_type n);
00201 
00202         basic_string& append(const cxxtools::Char* begin, const cxxtools::Char* end);
00203 
00204         basic_string& insert(size_type pos, const cxxtools::Char* str);
00205 
00206         basic_string& insert(size_type pos, const cxxtools::Char* str, size_type n);
00207 
00208         basic_string& insert(size_type pos, size_type n, cxxtools::Char ch);
00209 
00210         basic_string& insert(size_type pos, const basic_string& str);
00211 
00212         basic_string& insert(size_type pos, const basic_string& str, size_type pos2, size_type n);
00213 
00214         basic_string& insert(iterator p, cxxtools::Char ch);
00215 
00216         basic_string& insert(iterator p, size_type n, cxxtools::Char ch);
00217 
00218         // unimplemented
00219         //template <typename InputIterator>
00220         //basic_string& insert(iterator p, InputIterator first, InputIterator last);
00221 
00222         void clear();
00223 
00224         basic_string& erase(size_type pos = 0, size_type n = npos);
00225 
00226         iterator erase(iterator pos);
00227 
00228         iterator erase(iterator first, iterator last);
00229 
00230         basic_string& replace(size_type pos, size_type n, const cxxtools::Char* str);
00231 
00232         basic_string& replace(size_type pos, size_type n, const cxxtools::Char* str, size_type n2);
00233 
00234         basic_string& replace(size_type pos, size_type n, size_type n2, cxxtools::Char ch);
00235 
00236         basic_string& replace(size_type pos, size_type n, const basic_string& str);
00237 
00238         basic_string& replace(size_type pos, size_type n, const basic_string& str, size_type pos2, size_type n2);
00239 
00240         basic_string& replace(iterator i1, iterator i2, const cxxtools::Char* str);
00241 
00242         basic_string& replace(iterator i1, iterator i2, const cxxtools::Char* str, size_type n);
00243 
00244         basic_string& replace(iterator i1, iterator i2, size_type n, cxxtools::Char ch);
00245 
00246         basic_string& replace(iterator i1, iterator i2, const basic_string& str);
00247 
00248         //template<InputIterator>
00249         //basic_string& replace(iterator i1, iterator i2, InputIterator j1, InputIterator j2);
00250 
00251         int compare(const basic_string& str) const;
00252 
00253         int compare(const cxxtools::Char* str) const;
00254 
00255         int compare(const wchar_t* str) const;
00256 
00257         int compare(size_type pos, size_type n, const basic_string& str) const;
00258 
00259         int compare(size_type pos, size_type n, const basic_string& str, size_type pos2, size_type n2) const;
00260 
00261         int compare(size_type pos, size_type n, const cxxtools::Char* str) const;
00262 
00263         int compare(size_type pos, size_type n, const cxxtools::Char* str, size_type n2) const;
00264 
00265         size_type find(const basic_string& str, size_type pos = 0) const;
00266 
00267         size_type find(const cxxtools::Char* str, size_type pos, size_type n) const;
00268 
00269         size_type find(const cxxtools::Char* str, size_type pos = 0) const;
00270 //
00271         size_type find(cxxtools::Char ch, size_type pos = 0) const;
00272 
00273         size_type rfind(const basic_string& str, size_type pos = npos) const;
00274 
00275         size_type rfind(const cxxtools::Char* str, size_type pos, size_type n) const;
00276 
00277         size_type rfind(const cxxtools::Char* str, size_type pos = npos) const;
00278 
00279         size_type rfind(cxxtools::Char ch, size_type pos = npos) const;
00280 
00281         size_type find_first_of(const basic_string& str, size_type pos = 0) const
00282         { return this->find_first_of( str.data(), pos, str.size() ); }
00283 
00284         size_type find_first_of(const cxxtools::Char* s, size_type pos, size_type n) const;
00285 
00286         size_type find_first_of(const cxxtools::Char* str, size_type pos = 0) const
00287         { return this->find_first_of( str, pos, traits_type::length(str) ); }
00288 
00289         size_type find_first_of(const cxxtools::Char ch, size_type pos = 0) const
00290         { return this->find(ch, pos); }
00291 
00292         size_type find_last_of(const basic_string& str, size_type pos = npos) const
00293         { return this->find_last_of( str.data(), pos, str.size() ); }
00294 
00295         size_type find_last_of(const cxxtools::Char* s, size_type pos, size_type n) const;
00296 
00297         size_type find_last_of(const cxxtools::Char* str, size_type pos = npos) const
00298         { return this->find_last_of( str, pos, traits_type::length(str) ); }
00299 
00300         size_type find_last_of(const cxxtools::Char ch, size_type pos = npos) const
00301         { return this->rfind(ch, pos); }
00302 
00303         size_type find_first_not_of(const basic_string& str, size_type pos = 0) const
00304         { return this->find_first_not_of( str.data(), pos, str.size() ); }
00305 
00306         size_type find_first_not_of(const cxxtools::Char* s, size_type pos, size_type n) const;
00307 
00308         size_type find_first_not_of(const cxxtools::Char* str, size_type pos = 0) const
00309         {
00310             // requires_string(str);
00311             return this->find_first_not_of( str, pos, traits_type::length(str) );
00312         }
00313 
00314         size_type find_first_not_of(const cxxtools::Char ch, size_type pos = 0) const;
00315 
00316         size_type find_last_not_of(const basic_string& str, size_type pos = npos) const
00317         { return this->find_last_not_of( str.data(), pos, str.size() ); }
00318 
00319         size_type find_last_not_of(const cxxtools::Char* tok, size_type pos, size_type n) const;
00320 
00321         size_type find_last_not_of(const cxxtools::Char* str, size_type pos = npos) const
00322         {
00323             //requires_string(s);
00324             return this->find_last_not_of( str, pos, traits_type::length(str) );
00325         }
00326 
00327         // untested
00328         size_type find_last_not_of(cxxtools::Char ch, size_type pos = npos) const;
00329 
00330     public:
00331         void detach(size_type reserveSize);
00332 
00333         std::string narrow() const;
00334 
00335         static basic_string widen(const std::string& str);
00336 
00337         template <typename OutIterT>
00338         OutIterT toUtf16(OutIterT to) const;
00339 
00340         template <typename InIterT>
00341         static basic_string fromUtf16(InIterT from, InIterT fromEnd);
00342 
00343     public:
00344         basic_string& operator=(const basic_string& str)
00345         { return this->assign(str); }
00346 
00347         basic_string& operator=(const cxxtools::Char* str)
00348         { return this->assign(str); }
00349 
00350         basic_string& operator=(cxxtools::Char c)
00351         { return this->assign(1, c); }
00352 
00353         basic_string& operator+=(const basic_string& str)
00354         { return this->append(str); }
00355 
00356         basic_string& operator+=(const cxxtools::Char* str)
00357         { return this->append(str); }
00358 
00359         basic_string& operator+=(cxxtools::Char c)
00360         { return this->append(1, c); }
00361 
00362         const cxxtools::StringData& sdata() const
00363         {
00364             return *_data;
00365         }
00366 
00367     private:
00368         cxxtools::StringData* _data;
00369     };
00370 
00371     inline basic_string<cxxtools::Char> operator+(const basic_string<cxxtools::Char>& a, const basic_string<cxxtools::Char>& b)
00372     { basic_string<cxxtools::Char> temp; temp += a; temp += b; return temp; }
00373 
00374     inline basic_string<cxxtools::Char> operator+(const basic_string<cxxtools::Char>& a, const cxxtools::Char* b)
00375     { basic_string<cxxtools::Char> temp; temp += a; temp += b; return temp; }
00376 
00377     inline basic_string<cxxtools::Char> operator+(const cxxtools::Char* a, const basic_string<cxxtools::Char>& b)
00378     { basic_string<cxxtools::Char> temp; temp += a; temp += b; return temp; }
00379 
00380     inline basic_string<cxxtools::Char> operator+(const basic_string<cxxtools::Char>& a, cxxtools::Char b)
00381     { basic_string<cxxtools::Char> temp; temp += a; temp += b; return temp; }
00382 
00383     inline basic_string<cxxtools::Char> operator+(cxxtools::Char a, const basic_string<cxxtools::Char>& b)
00384     { basic_string<cxxtools::Char> temp; temp += a; temp += b; return temp; }
00385 
00386     // operator ==
00387     inline bool operator==(const basic_string<cxxtools::Char>& a, const basic_string<cxxtools::Char>& b)
00388     { return a.compare(b) == 0; }
00389 
00390     inline bool operator==(const cxxtools::Char* a, const basic_string<cxxtools::Char>& b)
00391     { return b.compare(a) == 0; }
00392 
00393     inline bool operator==(const basic_string<cxxtools::Char>& a, const cxxtools::Char* b)
00394     { return a.compare(b) == 0; }
00395 
00396     inline bool operator==(const basic_string<cxxtools::Char>& a, const wchar_t* b)
00397     { return a.compare(b) == 0; }
00398 
00399     // operator !=
00400     inline bool operator!=(const basic_string<cxxtools::Char>& a, const basic_string<cxxtools::Char>& b)
00401     { return a.compare(b) != 0; }
00402 
00403     inline bool operator!=(const cxxtools::Char* a, const basic_string<cxxtools::Char>& b)
00404     { return b.compare(a) != 0; }
00405 
00406     inline bool operator!=(const basic_string<cxxtools::Char>& a, const cxxtools::Char* b)
00407     { return a.compare(b) != 0; }
00408 
00409     inline bool operator!=(const basic_string<cxxtools::Char>& a, const wchar_t* b)
00410     { return a.compare(b) != 0; }
00411 
00412     // operator <
00413     inline bool operator<(const basic_string<cxxtools::Char>& a, const basic_string<cxxtools::Char>& b)
00414     { return a.compare(b) < 0; }
00415 
00416     inline bool operator<(const cxxtools::Char* a, const basic_string<cxxtools::Char>& b)
00417     { return b.compare(a) > 0; }
00418 
00419     inline bool operator<(const basic_string<cxxtools::Char>& a, const cxxtools::Char* b)
00420     { return a.compare(b) < 0; }
00421 
00422     inline bool operator<(const basic_string<cxxtools::Char>& a, const wchar_t* b)
00423     { return a.compare(b) < 0; }
00424 
00425     // operator >
00426     inline bool operator>(const basic_string<cxxtools::Char>& a, const basic_string<cxxtools::Char>& b)
00427     { return a.compare(b) > 0; }
00428 
00429     inline bool operator>(const cxxtools::Char* a, const basic_string<cxxtools::Char>& b)
00430     { return b.compare(a) < 0; }
00431 
00432     inline bool operator>(const basic_string<cxxtools::Char>& a, const cxxtools::Char* b)
00433     { return a.compare(b) > 0; }
00434 
00435     inline bool operator>(const basic_string<cxxtools::Char>& a, const wchar_t* b)
00436     { return a.compare(b) > 0; }
00437 
00438 } // namespace std
00439 
00440 
00441 namespace cxxtools {
00442 
00446     typedef std::basic_string<cxxtools::Char> String;
00447 
00448 }
00449 
00450 // Include the implementation header
00451 #include <cxxtools/string.tpp>
00452 
00453 #endif
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6