- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Lang/String.h

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 #if !defined(AFX_STRING_H__D0A9C1DF_44E8_490C_88C1_9377D66A0DFA__INCLUDED_)
    00015 #define AFX_STRING_H__D0A9C1DF_44E8_490C_88C1_9377D66A0DFA__INCLUDED_
    00016
    00017 #if _MSC_VER > 1000
    00018 #pragma once
    00019 #endif // _MSC_VER > 1000
    00020
    00021 #include "BaseModelObject.h"
    00022
    00023 #ifndef MERS_STRING_ONLY
    00024 #include <string>
    00025 // This is needed to convert everything to a string that isn't already
    00026 // something we can easily understand (such as char, char *, or
    00027 // String).
    00028 #include <sstream>
    00029 #endif
    00030
    00031 // MJP
    00032 // To help make cleaner exception/abort messages.
    00033 #define ERR_STR(x) String::format("[ERROR]: %s (%s, %d)", x, __FILE__, __LINE__)
    00034
    00035 namespace Mers {
    00036         namespace Utils {
    00037                 namespace Lang {
    00038
    00044                         class String : public BaseModelObject
    00045                         {
    00046                         public:
    00047                                 typedef int size_type;
    00048                                 typedef char & reference;
    00049                                 typedef char const & const_reference;
    00050                                 static const size_type npos;
    00051
    00052                                 explicit String();
    00053                                 String(const char * s);
    00054                                 String(const char * s, size_type n);
    00055                                 String(const String& rhs);
    00056                                 String(const String& rhs, size_type pos, size_type n);
    00057                                 String(size_type n, char c);
    00058                                 //String(const_iterator first, const_iterator last);
    00059
    00060 #ifndef MERS_STRING_ONLY
    00061
    00062                                 String(const std::string& rhs);
    00063
    00064 #endif
    00065
    00066                                 virtual ~String();
    00067
    00073                                 static String format(const char*formatStr, ...);
    00074
    00075                                 String& operator=(const String& rhs);
    00076                                 String& operator=(const char * s);
    00077                                 String& operator=(char c);
    00078                                 virtual BaseModelObject * duplicate() const;
    00079
    00080                                 bool operator == (const String & rhs) const;
    00081                                 bool operator == (const char * rhs) const;
    00082
    00083                                 bool operator != (const String & rhs) const;
    00084                                 bool operator != (const char * rhs) const;
    00085
    00086                                 inline bool operator < (const String & rhs) const {
    00087                                         return compare( rhs ) < 0;
    00088                                 }
    00089
    00090                                 inline bool operator < (const char * rhs) const {
    00091                                         return compare( rhs ) < 0;
    00092                                 }
    00093
    00101                                 inline operator char *() {
    00102                                         return array;
    00103                                 }
    00104
    00112                                 inline operator const char *() const {
    00113                                         return array;
    00114                                 }
    00115
    00116                                 //iterator begin();
    00117                                 //const_iterator begin() const;
    00118                                 //iterator end();
    00119                                 //const_iterator end() const;
    00120
    00121                                 //reverse_iterator rbegin();
    00122                                 //const_reverse_iterator rbegin() const;
    00123                                 //reverse_iterator rend();
    00124                                 //const_reverse_iterator rend() const;
    00125
    00126                                 reference at(size_type pos);
    00127                                 const_reference at(size_type pos) const;
    00128                                 reference operator[](size_type pos);
    00129                                 const_reference operator[](size_type pos) const;
    00130
    00131                                 const char * c_str() const;
    00132                                 const char * data() const;
    00133
    00134                                 size_type length() const;
    00135                                 size_type size() const;
    00136                                 size_type max_size() const;
    00137                                 void resize(size_type n, char c = char());
    00138                                 size_type capacity() const;
    00139                                 void reserve(size_type n = 0);
    00140
    00141                                 bool empty() const;
    00142
    00143                                 String& append(const String& str);
    00144                                 String& append(const String& str, size_type pos, size_type n);
    00145                                 String& append(const char * s, size_type n);
    00146                                 String& append(const char * s);
    00147                                 String& append(size_type n, char c);
    00148                                 //String& append(const_iterator first, const_iterator last);
    00149
    00150                                 friend String operator+(const char * lhs, const String& rhs) {
    00151                                         return String( lhs ) += rhs;
    00152                                 }
    00153
    00154 #ifndef MERS_STRING_ONLY
    00155
    00158                                 template< typename Type >
    00159                                 String& operator+=(Type rhs) {
    00160                                         std::ostringstream oss;
    00161                                         oss << rhs;
    00162                                         return append( oss.str().c_str() );
    00163                                 }
    00164 #else
    00165
    00169                                 template< typename Type >
    00170                                 String& operator+=(Type rhs) {
    00171                                         return append( ":: String += " ).append( typeid(Type).name() )
    00172                                                 .append( " not supported::" );
    00173                                 }
    00174 #endif
    00175                                 inline String& operator+=(const String& rhs) {
    00176                                         return append( rhs );
    00177                                 }
    00178                                 inline String& operator+=(String& rhs) {
    00179                                         return append( rhs );
    00180                                 }
    00181                                 inline String& operator+=(const char * s) {
    00182                                         return append( s );
    00183                                 }
    00184                                 inline String& operator+=(char * s) {
    00185                                         return append( s );
    00186                                 }
    00187                                 inline String& operator+=(char c) {
    00188                                         return append( 1, c );
    00189                                 }
    00190
    00197                                 template< typename Type >
    00198                                 String operator+(Type rhs) const {
    00199                                         return (String( *this ) += rhs);
    00200                                 }
    00201
    00205                                 template< typename Type >
    00206                                 friend String operator+( Type lhs, const String& rhs ) {
    00207                                         String str();
    00208                                         str += lhs;
    00209                                         str += rhs;
    00210                                         return str;
    00211                                 }
    00212
    00213                                 String operator+(const String& rhs) {
    00214                                         return (String( *this) += rhs);
    00215                                 }
    00216                                 String operator+(const char * rhs) {
    00217                                         return (String( *this) += rhs);
    00218                                 }
    00219
    00220 #ifndef MERS_STRING_ONLY
    00221
    00225                                 String operator+(const std::string& rhs) const {
    00226                                         return operator+( rhs.c_str() );
    00227                                 }
    00228                                 String& operator=(const std::string& rhs) {
    00229                                         return operator=( rhs.c_str() );
    00230                                 }
    00231                                 String& operator+=(const std::string& rhs) {
    00232                                         return operator+=( rhs.c_str() );
    00233                                 }
    00234
    00235 #endif
    00236
    00237                                 String& assign(const String& str);
    00238                                 String& assign(const String& str, size_type pos, size_type n);
    00239                                 String& assign(const char * s, size_type n);
    00240                                 String& assign(const char * s);
    00241                                 String& assign(size_type n, char c);
    00242                                 //String& assign(const_iterator first, const_iterator last);
    00243
    00250                                 String& insert(size_type p0, const String& str);
    00251                                 String& insert(size_type p0, const String& str, size_type pos, size_type n);
    00252                                 String& insert(size_type p0, const char * s, size_type n);
    00253                                 String& insert(size_type p0, const char * s);
    00254                                 String& insert(size_type p0, size_type n, char c);
    00255                                 //iterator insert(iterator it, char c);
    00256                                 //void insert(iterator it, size_type n, char c);
    00257                                 //void insert(iterator it, const_iterator first, const_iterator last);
    00258
    00259                                 String& erase(size_type p0 = 0, size_type n = npos);
    00260                                 //iterator erase(iterator it);
    00261                                 //iterator erase(iterator first, iterator last);
    00262
    00263                                 String& replace(size_type p0, size_type n0, const String& str);
    00264                                 String& replace(size_type p0, size_type n0, const String& str, size_type pos, size_type n);
    00265                                 String& replace(size_type p0, size_type n0, const char * s, size_type n);
    00266                                 String& replace(size_type p0, size_type n0, const char * s);
    00267                                 String& replace(size_type p0, size_type n0, size_type n, char c);
    00268                                 //String& replace(iterator first0, iterator last0, const String& str);
    00269                                 //String& replace(iterator first0, iterator last0, const char * s, size_type n);
    00270                                 //String& replace(iterator first0, iterator last0, const char * s);
    00271                                 //String& replace(iterator first0, iterator last0, size_type n, char c);
    00272                                 //String& replace(iterator first0, iterator last0, const_iterator first, const_iterator last);
    00273
    00274                                 size_type copy(char * s, size_type n, size_type pos = 0) const;
    00275
    00276                                 void swap(String& str);
    00277
    00278                                 size_type find(const String& str, size_type pos = 0) const;
    00279                                 size_type find(const char * s, size_type pos, size_type n) const;
    00280                                 size_type find(const char * s, size_type pos = 0) const;
    00281                                 size_type find(char c, size_type pos = 0) const;
    00282
    00283                                 size_type rfind(const String& str, size_type pos = npos) const;
    00284                                 size_type rfind(const char * s, size_type pos, size_type n = npos) const;
    00285                                 size_type rfind(const char * s, size_type pos = npos) const;
    00286                                 size_type rfind(char c, size_type pos = npos) const;
    00287
    00288                                 size_type find_first_of(const String& str, size_type pos = 0) const;
    00289                                 size_type find_first_of(const char * s, size_type pos, size_type n) const;
    00290                                 size_type find_first_of(const char * s, size_type pos = 0) const;
    00291                                 size_type find_first_of(char c, size_type pos = 0) const;
    00292
    00293                                 size_type find_last_of(const String& str, size_type pos = npos) const;
    00294                                 size_type find_last_of(const char * s, size_type pos, size_type n = npos) const;
    00295                                 size_type find_last_of(const char * s, size_type pos = npos) const;
    00296                                 size_type find_last_of(char c, size_type pos = npos) const;
    00297
    00298                                 size_type find_first_not_of(const String& str, size_type pos = 0) const;
    00299                                 size_type find_first_not_of(const char * s, size_type pos, size_type n) const;
    00300                                 size_type find_first_not_of(const char * s, size_type pos = 0) const;
    00301                                 size_type find_first_not_of(char c, size_type pos = 0) const;
    00302
    00303                                 size_type find_last_not_of(const String& str, size_type pos = npos) const;
    00304                                 size_type find_last_not_of(const char * s, size_type pos, size_type n) const;
    00305                                 size_type find_last_not_of(const char * s, size_type pos = npos) const;
    00306                                 size_type find_last_not_of(char c, size_type pos = npos) const;
    00307
    00308                                 String substr(size_type pos = 0, size_type n = npos) const;
    00309
    00310                                 int compare(const String& str) const;
    00311                                 int compare(size_type p0, size_type n0, const String& str);
    00312                                 int compare(size_type p0, size_type n0, const String& str, size_type pos, size_type n);
    00313                                 int compare(const char * s) const;
    00314                                 int compare(size_type p0, size_type n0, const char * s) const;
    00315                                 int compare(size_type p0, size_type n0, const char * s, size_type pos) const;
    00316
    00317                                 //A get_allocator() const;
    00318
    00319 #ifndef EMBEDDED
    00320
    00324                                 void print(std::ostream &out) const;
    00325 #endif
    00326
    00330                                 size_type trimLeft( void );
    00335                                 size_type trimRight( void );
    00340                                 size_type trim( void );
    00341
    00342                         protected:
    00349                                 virtual void destroy();
    00350
    00356                                 int grow(size_type newMaxSize);
    00364                                 int shrink();
    00365
    00366                         protected:
    00368                                 char * array;
    00370                                 size_type curSize;
    00372                                 size_type maxSize;
    00373                         };
    00374                 }
    00375         }
    00376 }
    00377
    00378 #endif // !defined(AFX_STRING_H__D0A9C1DF_44E8_490C_88C1_9377D66A0DFA__INCLUDED_)