AndroMeta  2.0.0
AndroMeta/include/AndroMeta/MStr.h
Go to the documentation of this file.
00001 
00002 /*
00003  * Copyright 2009-2012 AndroMeta LLC. All rights reserved.
00004  *
00005  * AndroMeta LLC retains all intellectual property and proprietary 
00006  * rights to this software and related documentation and any modifications 
00007  * thereto.  Any use, reproduction, disclosure, or distribution of this 
00008  * software and related documentation without an express license agreement 
00009  * from AndroMeta LLC is strictly prohibited.
00010  * 
00011  */
00012 
00020 #ifndef ANDROMETA_M_STR_H
00021 #define ANDROMETA_M_STR_H
00022 
00023 #include <string>
00024 #include <sstream>
00025 
00026 #include <AndroMeta/MVector.h>
00027 
00028 namespace Meta{
00029   
00030   class MVar;
00031   
00040   class MStr{
00041   public:
00042     typedef std::string::iterator iterator;
00043     
00044     typedef std::string::const_iterator const_iterator;
00045     
00046     typedef std::string::reverse_iterator reverse_iterator;
00047     
00048     typedef std::string::const_reverse_iterator const_reverse_iterator;
00049     
00050     MStr(const std::string& str);
00051     
00052     MStr(const char* s);
00053     
00054     MStr();
00055     
00056     ~MStr();
00057     
00058     static const size_t npos = std::string::npos;
00059     
00060     bool operator!=(const MStr& str) const;
00061     
00062     bool operator==(const MStr& str) const;
00063     
00064     MStr& operator=(const MStr& str);
00065     
00066     bool operator<(const MStr& str) const;
00067     
00068     bool operator>(const MStr& str) const;
00069     
00070     bool operator<=(const MStr& str) const;
00071     
00072     bool operator>=(const MStr& str) const;
00073     
00074     MStr operator+(const MStr& str) const;
00075     
00076     MStr& operator+=(const MStr& str);
00077     
00078     MStr& operator+=(char c);
00079     
00080     const char& operator[](size_t pos) const;
00081     
00082     char& operator[](size_t pos);
00083     
00084     const char& at(size_t pos) const;
00085     
00086     char& at(size_t pos);
00087     
00088     MStr& append(const MStr& str);
00089     
00090     MStr& append(const MStr& str, size_t pos, size_t n);
00091     
00092     MStr& append(const char* s, size_t n);
00093     
00094     MStr& append(const char* s);
00095     
00096     MStr& append(size_t n, char c);
00097     
00098     iterator begin();
00099     
00100     const_iterator begin() const;
00101     
00102     iterator end();
00103     
00104     const_iterator end() const;
00105     
00106     reverse_iterator rbegin();
00107     
00108     const_reverse_iterator rbegin() const;
00109     
00110     reverse_iterator rend();
00111     
00112     const_reverse_iterator rend() const;
00113     
00114     MStr& insert(size_t pos1, const MStr& str);
00115     
00116     MStr& insert(size_t pos1, const MStr& str, size_t pos2, size_t n);
00117     
00118     MStr& insert(size_t pos1, const char* s, size_t n);
00119     
00120     MStr& insert(size_t pos1, const char* s);
00121     
00122     MStr& insert(size_t pos1, size_t n, char c);
00123     
00124     iterator insert(iterator p, char c);
00125     
00126     void insert(iterator p, size_t n, char c);
00127     
00128     template<class InputIterator>
00129     void insert(iterator p, InputIterator first, InputIterator last);
00130     
00131     size_t copy(char* s, size_t n, size_t pos=0) const;
00132     
00133     template <class InputIterator>
00134     MStr& append(InputIterator first, InputIterator last);
00135     
00136     MStr substr(size_t pos=0, size_t n=std::string::npos) const;
00137     
00138     const char* c_str() const;
00139     
00140     const char* cstr() const{
00141       return c_str();
00142     }
00143     
00144     size_t find(const MStr& str, size_t pos=0) const;
00145     
00146     size_t find(const char* s, size_t pos, size_t n) const;
00147     
00148     size_t find(const char* s, size_t pos=0) const;
00149     
00150     size_t find(char c, size_t pos=0) const;
00151     
00152     size_t rfind(const MStr& str, size_t pos=npos) const;
00153     
00154     size_t rfind(const char* s, size_t pos, size_t n) const;
00155     
00156     size_t rfind(const char* s, size_t pos=npos) const;
00157     
00158     size_t rfind(char c, size_t pos=npos) const;
00159     
00160     size_t findReplace(const MStr& value,
00161                        const MStr& replacement,
00162                        bool all=true,
00163                        size_t pos=0);
00164     
00168     size_t findCount(const MStr& str) const;
00169     
00170     operator std::string() const;
00171     
00172     std::string& str();
00173     
00174     const std::string& str() const;
00175     
00176     size_t length() const;
00177     
00178     void resize(size_t n, char c);
00179     
00180     void resize(size_t n);
00181     
00182     size_t size() const;
00183     
00184     size_t max_size() const;
00185     
00186     size_t maxSize() const;
00187     
00188     size_t capacity() const;
00189     
00190     void reserve(size_t res_arg=0);
00191     
00192     void clear();
00193     
00194     MStr& erase(size_t pos=0, size_t n=npos);
00195     
00196     MStr& replace(size_t pos1, size_t n1, const MStr& str);
00197     
00198     MStr allCaps() const;
00199     
00200     MStr allLower() const;
00201     
00202     MStr uppercase() const;
00203     
00204     MStr lowercase() const;
00205     
00206     void strip(bool start=true, bool end=true);
00207     
00208     static MStr toStr(const MVar& v, bool concise=true, bool quote=true);
00209     
00210     MStr quote() const;
00211     
00212     bool isIdentifier() const;
00213     
00214     bool isAllCapsIdentifier() const;
00215     
00216     static bool isLower(int c);
00217     
00218     static bool isUpper(int c);
00219     
00220     static bool isDigit(int c);
00221     
00222     static bool isAlpha(int c);
00223     
00224     static int toLower(int c);
00225     
00226     static int toUpper(int c);
00227 
00228     bool isNumeric() const;
00229     
00230     bool empty() const;
00231     
00232     void convertEscapes();
00233     
00234     template<typename T>
00235     static MStr join(const T& v,
00236                      const MStr& delimiter,
00237                      bool concise=true,
00238                      bool quote=false){
00239       std::stringstream ostr;
00240       typename T::const_iterator itr = v.begin();
00241       while(itr != v.end()){
00242         if(itr != v.begin()){
00243           ostr << delimiter.str_; 
00244         }
00245         ostr << MStr::toStr(*itr, concise, quote);
00246         ++itr;
00247       }
00248       return ostr.str();
00249     }
00250     
00251     template<typename T>
00252     void split(T& out, const MStr& delimiter, int maxTokens=-1) const{
00253       size_t i = 0;
00254       size_t count = 0;
00255       for(;;){
00256         if(maxTokens > 0 && count >= maxTokens){
00257           out.push_back(substr(i, length() - i));
00258           break;
00259         }
00260         size_t pos = find(delimiter, i);
00261         if(pos == MStr::npos){
00262           out.push_back(substr(i, length() - i));
00263           break;
00264         }
00265         out.push_back(substr(i, pos-i));
00266         i = pos + delimiter.length();
00267         ++count;
00268       }
00269     }
00270     
00271     void push_back(char c);
00272     
00273     void pushBack(char c);
00274     
00275     MStr& assign(const MStr& str);
00276     
00277     MStr& assign(const MStr& str, size_t pos, size_t n);
00278     
00279     MStr& assign(const char* s, size_t n);
00280     
00281     MStr& assign(const char* s);
00282     
00283     MStr& assign(size_t n, char c);
00284     
00285     template <class InputIterator>
00286     MStr& assign(InputIterator first, InputIterator last);
00287     
00288     void swap(MStr& str);
00289     
00290     const char* data() const;
00291     
00292     std::allocator<char> get_allocator() const;
00293     
00294     size_t find_first_of(const MStr& str, size_t pos=0) const;
00295     
00296     size_t findFirstOf(const MStr& str, size_t pos=0) const{
00297       return find_first_of(str, pos);
00298     }
00299     
00300     size_t find_first_of(const char* s, size_t pos, size_t n) const;
00301     
00302     size_t findFirstOf(const char* s, size_t pos, size_t n) const{
00303       return find_first_of(s, pos, n);
00304     }
00305     
00306     size_t find_first_of(const char* s, size_t pos=0) const;
00307     
00308     size_t findFirstOf(const char* s, size_t pos=0) const{
00309       return find_first_of(s, pos);
00310     }
00311     
00312     size_t find_first_of(char c, size_t pos=0) const;
00313     
00314     size_t findFirstOf(char c, size_t pos=0) const{
00315       return find_first_of(c, pos);
00316     }
00317     
00318     size_t find_last_of(const MStr& str, size_t pos=npos) const;
00319     
00320     size_t findLastOf(const MStr& str, size_t pos=npos) const{
00321       return find_last_of(str, pos);
00322     }
00323     
00324     size_t find_last_of(const char* s, size_t pos, size_t n) const;
00325     
00326     size_t findLastOf(const char* s, size_t pos, size_t n) const{
00327       return find_last_of(s, pos, n);
00328     }
00329     
00330     size_t find_last_of(const char* s, size_t pos=npos) const;
00331     
00332     size_t findLastOf(const char* s, size_t pos=npos) const{
00333       return find_last_of(s, pos);
00334     }
00335     
00336     size_t find_last_of(char c, size_t pos=npos) const;
00337     
00338     size_t findLastOf(char c, size_t pos=npos) const{
00339       return find_last_of(c, pos);
00340     } 
00341     
00342     size_t find_first_not_of(const MStr& str, size_t pos=0) const;
00343     
00344     size_t findFirstNotOf(const MStr& str, size_t pos=0) const{
00345       return find_first_not_of(str, pos);
00346     }
00347     
00348     size_t find_first_not_of(const char* s, size_t pos, size_t n) const;
00349     
00350     size_t findFirstNotOf(const char* s, size_t pos, size_t n) const{
00351       return find_first_not_of(s, pos, n);
00352     }
00353     
00354     size_t find_first_not_of(const char* s, size_t pos=0) const;
00355     
00356     size_t findFirstNotOf(const char* s, size_t pos=0) const{
00357       return find_first_not_of(s, pos);
00358     }
00359     
00360     size_t find_first_not_of(char c, size_t pos=0) const;
00361     
00362     size_t findFirstNotOf(char c, size_t pos=0) const{
00363       return find_first_not_of(c, pos);
00364     }
00365     
00366     size_t find_last_not_of(const MStr& str, size_t pos=npos) const;
00367     
00368     size_t findLastNotOf(const MStr& str, size_t pos=npos) const{
00369       return find_last_not_of(str, pos);
00370     }
00371     
00372     size_t find_last_not_of(const char* s, size_t pos, size_t n) const;
00373     
00374     size_t findLastNotOf(const char* s, size_t pos, size_t n) const{
00375       return find_last_not_of(s, pos, n);
00376     }
00377     
00378     size_t find_last_not_of(const char* s, size_t pos=npos) const;
00379     
00380     size_t findLastNotOf(const char* s, size_t pos=npos) const{
00381       return find_last_not_of(s, pos);
00382     }
00383     
00384     size_t find_last_not_of(char c, size_t pos=npos) const;
00385     
00386     size_t findLastNotOf(char c, size_t pos=npos) const{
00387       return find_last_not_of(c, pos);
00388     } 
00389     
00390   private:
00391     std::string str_;
00392   };
00393   
00394   typedef MStr mstr;
00395   
00396   inline std::ostream& operator<<(std::ostream& ostr, const MStr& v){
00397     ostr << v.str();
00398     return ostr;
00399   }
00400   
00401   inline std::istream& operator>>(std::istream& istr, MStr& str){
00402     istr >> str.str();
00403     return istr;
00404   }
00405   
00406   inline MStr& operator<<(MStr& str, char c){
00407     return str.append(1, c);
00408   }
00409   
00410   inline MStr operator+(const char* s, const MStr& str){
00411     return s + str.str();
00412   }
00413   
00414   inline MStr& operator<<(MStr& str, const MVar& v){
00415     return str += MStr::toStr(v, true, false);
00416   }
00417   
00418   inline MStr operator<<(const MStr& str, const MVar& v){
00419     return str + MStr::toStr(v, true, false);
00420   }
00421   
00422 } // end namespace Meta
00423 
00424 #endif // ANDROMETA_M_STR_H