00001
00002 #ifndef MY_STRING_H
00003 #define MY_STRING_H
00004
00005
00006
00031 #include <stdexcept>
00032
00033 #include <stdio.h>
00034
00035 #include "SubString.h"
00036 #include "RCArray.h"
00037
00042 typedef RCArray<char> CharArray;
00043
00099 class String : public CharArray
00100 {
00101
00102
00103 friend SubString;
00104 protected:
00105 void init(const char *initCstr );
00106 size_t length();
00107 void appendCString( const char *CStr );
00108 void appendString( String &Str );
00109 void newEmptyString();
00110 void newSharedData();
00111 int Cstrcmp( const char *s1, const char *s2 );
00112 int compareTo( String &s );
00113
00114 public:
00115
00116 String();
00117 String( const char ch );
00118 String( const char *initCstr );
00119 String( const String &Str );
00120
00121
00122 SubString operator()(const size_t start, const size_t len);
00123
00125 size_t strlen();
00126
00127 size_t getRefCnt();
00128
00129 int compareTo( const char *Cstr );
00130
00131 String &resize(const size_t new_size );
00132
00133
00134
00135
00136 operator const char*() const;
00137
00138 String &operator =(const char ch);
00139 String &operator =(const char *Cstr);
00140 String &operator =(const SubString &sub);
00141
00142 String operator +(const char *Cstr );
00143 String operator +(String &s );
00144 String operator +(const char ch );
00145
00146 String operator +(const SubString &sub);
00147
00148 String &operator +=(const char *Cstr );
00149 String &operator +=(String &s );
00150 String &operator +=(const char ch);
00151
00152 bool operator ==(const char *Cstr ) { return (compareTo( Cstr ) == 0); }
00153 bool operator ==(String &s ) { return (compareTo( s ) == 0); }
00154 bool operator !=(const char *Cstr ) { return (compareTo( Cstr ) != 0); }
00155 bool operator !=(String &s ) { return (compareTo( s ) != 0); }
00156 bool operator <=(const char *Cstr ) { return (compareTo( Cstr ) <= 0); }
00157 bool operator <=(String &s ) { return (compareTo( s ) <= 0); }
00158 bool operator >=(const char *Cstr ) { return (compareTo( Cstr ) >= 0); }
00159 bool operator >=(String &s ) { return (compareTo( s ) >= 0); }
00160 bool operator <(const char *Cstr ) { return (compareTo( Cstr ) < 0); }
00161 bool operator <(String &s ) { return (compareTo( s ) < 0); }
00162 bool operator >(const char *Cstr ) { return (compareTo( Cstr ) > 0); }
00163 bool operator >(String &s ) { return (compareTo( s ) > 0); }
00164
00165 };
00166
00167
00168
00169
00170
00171 String operator +(const char *Cstr, String &s);
00172 String operator +(const char ch, String &s);
00173
00174 bool operator ==(const char *Cstr, String &s);
00175 bool operator !=(const char *Cstr, String &s);
00176 bool operator <=(const char *Cstr, String &s);
00177 bool operator >=(const char *Cstr, String &s);
00178 bool operator <(const char *Cstr, String &s);
00179 bool operator >(const char *Cstr, String &s);
00180
00181 inline
00182 String::String() : CharArray() {}
00183
00185 inline
00186 String::String( const char ch ) : CharArray()
00187 {
00188 if (ch != '\0') {
00189 value->data.append( ch );
00190 value->data.append('\0');
00191 }
00192 }
00193
00195 inline
00196 String::String( const char *initCstr ) : CharArray()
00197 {
00198 init( initCstr );
00199 }
00200
00204 inline
00205 String::String( const String &Str ) : CharArray()
00206 {
00207 value = Str.value;
00208 }
00209
00210
00216 inline
00217 size_t String::length()
00218 {
00219 return CharArray::length();
00220 }
00221
00222
00228 inline
00229 size_t String::getRefCnt()
00230 {
00231 return CharArray::getRefCnt();
00232 }
00233
00234
00235
00236
00254 inline
00255 SubString String::operator ()(const size_t start, const size_t len)
00256 {
00257 SubString subStr( *this, start, len );
00258
00259 return subStr;
00260 }
00261
00262
00274 inline
00275 String String::operator +(const SubString &sub)
00276 {
00277 String section = sub;
00278 String result = *this + section;
00279 return result;
00280 }
00281
00282
00283
00291 inline
00292 String &String::operator =(const SubString &sub)
00293 {
00294 *this = (String)sub;
00295 return *this;
00296 }
00297
00298
00305 inline
00306 void String::newSharedData()
00307 {
00308 value = new SharedData( value->data );
00309 }
00310
00311
00312
00323 inline
00324 int String::compareTo( const char *Cstr )
00325 {
00326 const char *pThisCStr = *this;
00327
00328 return Cstrcmp( pThisCStr, Cstr );
00329 }
00330
00331
00332
00337 inline
00338 String &String::operator +=(const char ch)
00339 {
00340 char buf[4];
00341
00342 buf[0] = ch;
00343 buf[1] = '\0';
00344
00345 *this += buf;
00346 return *this;
00347 }
00348
00349
00350
00372 inline
00373 String String::operator +( const char *Cstr )
00374 {
00375 String tmp;
00376
00377 tmp.appendString( *this );
00378 tmp.appendCString( Cstr );
00379
00380 return tmp;
00381 }
00382
00383
00388 inline
00389 String String::operator +(String &s)
00390 {
00391 String tmp;
00392
00393 tmp.newSharedData();
00394
00395 tmp.appendString( *this );
00396 tmp.appendString( s );
00397
00398 return tmp;
00399 }
00400
00401
00409 inline
00410 String String::operator +(const char ch)
00411 {
00412 char buf[4];
00413
00414 buf[0] = ch;
00415 buf[1] = '\0';
00416
00417 String tmp;
00418
00419 tmp = *this + buf;
00420
00421 return tmp;
00422 }
00423
00424
00425
00432 inline
00433 size_t String::strlen()
00434 {
00435 int len = value->data.length();
00436 if (len > 0 && value->data[len-1] == '\0')
00437 len--;
00438 return len;
00439 }
00440
00441
00442 #endif