00001
00026 #include <strstream>
00027 #include <iostream>
00028
00029 using namespace std;
00030
00032 #include <string.h>
00033
00034 #include "String.h"
00035
00036
00041 void String::init( const char *initCstr )
00042 {
00043 if (initCstr != 0) {
00044 char ch;
00045 for (ch = *initCstr; ch != '\0'; ch = *initCstr) {
00046 value->data.append( ch );
00047 initCstr++;
00048 }
00049 value->data.append( ch );
00050 }
00051 }
00052
00053
00054
00067 void String::appendCString( const char *CStr )
00068 {
00069 if (CStr != 0) {
00070 size_t len = length();
00071 if (len > 0) {
00072
00073
00074 if (value->data[len-1] == '\0') {
00075 value->data[len-1] = *CStr++;
00076 }
00077 }
00078 while (*CStr) {
00079 value->data.append( *CStr++ );
00080 }
00081 value->data.append('\0');
00082 }
00083 }
00084
00085
00097 void String::appendString( String &s )
00098 {
00099 size_t i = 0;
00100 size_t len = s.length();
00101 size_t thisLen = length();
00102
00103 if (thisLen > 0 && len > 0) {
00104
00105 if (value->data[thisLen-1] == '\0') {
00106 value->data[thisLen-1] = s.value->data[i];
00107 i++;
00108 }
00109 }
00110 while (i < len) {
00111 char ch = s.value->data[i];
00112 value->data.append( ch );
00113 i++;
00114 }
00115 }
00116
00117
00118
00122 int String::compareTo( String &s )
00123 {
00124 if (length() == 0 && s.length() == 0) {
00125
00126
00127 return 0;
00128 }
00129
00130 if (value == s.value) {
00131
00132 return 0;
00133 }
00134
00135 const char *pThisCStr = *this;
00136 const char *Cstr = s;
00137
00138 return Cstrcmp( pThisCStr, Cstr );
00139 }
00140
00141
00158 String &String::resize( const size_t newSize )
00159 {
00160 if (newSize == 0) {
00161 newEmptyString();
00162 }
00163 else {
00164 size_t new_len = newSize + 1;
00165 size_t old_len = length();
00166
00167 if (value->isShared()) {
00168 if (new_len >= old_len) {
00169
00170 newSharedData();
00171 value->data.set_size( new_len );
00172 }
00173 else {
00174 RCPtr<SharedData> new_value = new SharedData();
00175 for (size_t i = 0; i < newSize; i++) {
00176 new_value->data.append( value->data[i] );
00177 }
00178 new_value->data.append('\0');
00179
00180 value = new_value;
00181 }
00182 }
00183 else {
00184 value->data.set_size( new_len );
00185 }
00186
00187 for (size_t i = old_len-1; i < new_len-1; i++) {
00188 value->data[i] = ' ';
00189 }
00190 value->data[ new_len-1 ] = '\0';
00191 }
00192 return *this;
00193 }
00194
00195
00196
00204 int String::Cstrcmp(const char *pThisCStr, const char *Cstr )
00205 {
00206 int rslt;
00207
00208 if (pThisCStr == 0) {
00209 if (Cstr == 0)
00210 rslt = 0;
00211 else {
00212 if (::strlen( Cstr ) == 0)
00213 rslt = 0;
00214 else
00215 rslt = -1;
00216 }
00217 }
00218 else {
00219 if (Cstr == 0)
00220 rslt = 1;
00221 else
00222 rslt = strcmp(pThisCStr, Cstr );
00223 }
00224
00225 return rslt;
00226 }
00227
00228
00229
00256 String::operator const char *() const
00257 {
00258 const char *pCstr = "";
00259 size_t len = value->data.length();
00260
00261 if (len > 0) {
00262 if (value->data[len-1] != '\0') {
00263
00264 value->data.append('\0');
00265 }
00266 pCstr = value->data.getData();
00267 }
00268
00269 return pCstr;
00270 }
00271
00272
00278 void String::newEmptyString()
00279 {
00280 if (value->isShared()) {
00281 value = new SharedData();
00282 }
00283 else {
00284 value->data.set_size(0);
00285 }
00286 }
00287
00288
00289
00293 String &String::operator =(const char ch)
00294 {
00295 newEmptyString();
00296 if (ch != '\0') {
00297 value->data.append( ch );
00298 value->data.append('\0');
00299 }
00300 return *this;
00301 }
00302
00303
00304
00323 String &String::operator =(const char *Cstr)
00324 {
00325 newEmptyString();
00326 if (Cstr != 0) {
00327 char ch;
00328 for (ch = *Cstr; ch != '\0'; ch = *Cstr) {
00329 value->data.append(ch);
00330 Cstr++;
00331 }
00332 value->data.append(ch);
00333 }
00334 return *this;
00335 }
00336
00337
00338
00352 String &String::operator +=( const char *Cstr )
00353 {
00354 if (value->isShared()) {
00355 newSharedData();
00356 }
00357
00358
00359
00360 size_t len = value->data.length();
00361 if (len > 0 && value->data[len-1] == '\0')
00362 value->data.remove();
00363
00364 appendCString( Cstr );
00365
00366 return *this;
00367 }
00368
00369
00370
00375 String &String::operator +=(String &s )
00376 {
00377 if (s.value->data.length() > 0) {
00378 if (value->isShared()) {
00379 newSharedData();
00380 }
00381
00382
00383
00384 size_t len = value->data.length();
00385 if (len > 0 && value->data[len-1] == '\0') {
00386 value->data.remove();
00387 }
00388
00389 appendString( s );
00390 }
00391 return *this;
00392 }
00393
00394
00395
00396
00397
00398
00399
00409 String operator +(const char *Cstr, String &s)
00410 {
00411 String tmp;
00412 if (Cstr != 0) {
00413 tmp = Cstr;
00414 }
00415
00416 tmp += s;
00417 return tmp;
00418 }
00419
00420
00434 String operator +(const char ch, String &s)
00435 {
00436 String tmp;
00437
00438 tmp = ch;
00439 tmp += s;
00440
00441 return tmp;
00442 }
00443
00444
00449 bool operator ==(const char *Cstr, String &s)
00450 {
00451 return (s == Cstr);
00452 }
00453
00454
00459 bool operator !=(const char *Cstr, String &s)
00460 {
00461 return (s != Cstr);
00462 }
00463
00464
00471 bool operator <=(const char *Cstr, String &s)
00472 {
00473 return (s >= Cstr);
00474 }
00475
00476
00483 bool operator >=(const char *Cstr, String &s)
00484 {
00485 return (s <= Cstr);
00486 }
00487
00488
00495 bool operator <(const char *Cstr, String &s)
00496 {
00497 return (s > Cstr);
00498 }
00499
00500
00507 bool operator >(const char *Cstr, String &s)
00508 {
00509 return (s < Cstr);
00510 }
00511
00512