StringBasics.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __BASICSTRING_H__
00019 #define __BASICSTRING_H__
00020
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <stdarg.h>
00024 #include <vector>
00025 #include <iostream>
00026
00027 using std::vector;
00028
00029 #define READBUF 128
00030 #define READBUFSTR "128"
00031
00032 #ifdef __PREFIX_STRING__
00033 #define String BasicString
00034 #endif
00035
00036 #include "InputFile.h"
00037
00038 class String
00039 {
00040 private:
00041 void NewString(int startsize);
00042
00043 protected:
00044 char * buffer;
00045 int len, size;
00046
00047 public:
00048 static int alloc;
00049 static bool caseSensitive;
00050
00051 explicit String(int startsize = 0)
00052 {
00053 NewString(startsize);
00054 }
00055 String(const char * s);
00056 String(const String & s);
00057 explicit String(char ch, int count = 1);
00058
00059 ~String()
00060 {
00061 delete [] buffer;
00062 }
00063
00064 String & Clear()
00065 {
00066 len = buffer[0] = 0;
00067 return *this;
00068 }
00069
00070 String & Copy(const String & s);
00071 String & Copy(const String & s, int start, int count);
00072 String & Copy(const char * s);
00073
00074 bool IsEmpty() const
00075 {
00076 return len == 0;
00077 }
00078 String & ToUpper();
00079 String & ToLower();
00080 String AsUpper();
00081 String AsLower();
00082 String Capitalize();
00083 String & Reverse();
00084
00085 String & LeftClip(int clipAmount);
00086 String & RightClip(int clipAmount);
00087
00088 String & operator = (char ch);
00089 String operator + (char ch) const;
00090 String & operator += (char ch);
00091
00092 String & operator = (const String & rhs);
00093 String operator + (const String & rhs) const;
00094 String & operator += (const String & rhs);
00095
00096 String & operator = (const char * rhs);
00097 String operator + (const char * rhs) const;
00098 String & operator += (const char * rhs);
00099
00100 String & operator = (int rhs);
00101 String operator + (int rhs) const;
00102 String & operator += (int rhs);
00103
00104 String & operator = (double rhs);
00105 String operator + (double rhs) const;
00106 String & operator += (double rhs);
00107
00108 String & operator = (unsigned int rhs);
00109 String operator + (unsigned int rhs) const;
00110 String & operator += (unsigned int rhs);
00111 String operator *(unsigned int rhs) const;
00112 String & operator *= (unsigned int rhs);
00113
00114 int Compare(const String & rhs) const;
00115 int FastCompare(const String & rhs) const;
00116 int SlowCompare(const String & rhs) const;
00117
00118 int Compare(const char * rhs) const;
00119 int FastCompare(const char * rhs) const;
00120 int SlowCompare(const char * rhs) const;
00121
00122 int CompareToStem(const String & stem) const;
00123 int FastCompareToStem(const String & stem) const;
00124 int SlowCompareToStem(const String & stem) const;
00125
00126 int CompareToStem(const char * stem) const;
00127 int FastCompareToStem(const char * stem) const;
00128 int SlowCompareToStem(const char * stem) const;
00129
00130 int MatchesBeginningOf(const String & stem) const;
00131 int FastMatchesBeginningOf(const String & stem) const;
00132 int SlowMatchesBeginningOf(const String & stem) const;
00133
00134 int MatchesBeginningOf(const char * stem) const;
00135 int FastMatchesBeginningOf(const char * stem) const;
00136 int SlowMatchesBeginningOf(const char * stem) const;
00137
00138 int operator == (const String & rhs) const
00139 {
00140 return Compare(rhs) == 0;
00141 }
00142 int operator != (const String & rhs) const
00143 {
00144 return Compare(rhs) != 0;
00145 }
00146 int operator < (const String & rhs) const
00147 {
00148 return Compare(rhs) < 0;
00149 }
00150 int operator > (const String & rhs) const
00151 {
00152 return Compare(rhs) > 0;
00153 }
00154 int operator >= (const String & rhs) const
00155 {
00156 return Compare(rhs) >= 0;
00157 }
00158 int operator <= (const String & rhs) const
00159 {
00160 return Compare(rhs) <= 0;
00161 }
00162
00163 int operator == (const char * rhs) const
00164 {
00165 return Compare(rhs) == 0;
00166 }
00167 int operator != (const char * rhs) const
00168 {
00169 return Compare(rhs) != 0;
00170 }
00171 int operator < (const char * rhs) const
00172 {
00173 return Compare(rhs) < 0;
00174 }
00175 int operator > (const char * rhs) const
00176 {
00177 return Compare(rhs) > 0;
00178 }
00179 int operator <= (const char * rhs) const
00180 {
00181 return Compare(rhs) <= 0;
00182 }
00183 int operator >= (const char * rhs) const
00184 {
00185 return Compare(rhs) >= 0;
00186 }
00187
00188 operator const char *() const
00189 {
00190 return buffer;
00191 }
00192 const char *c_str() const
00193 {
00194 return (const char *) buffer;
00195 }
00196 operator char *()
00197 {
00198 return buffer;
00199 }
00200
00201 operator int () const
00202 {
00203 return atoi(buffer);
00204 }
00205 operator double() const
00206 {
00207 return atof(buffer);
00208 }
00209 operator long double() const;
00210
00211 char operator [](int i) const
00212 {
00213 return buffer[i];
00214 }
00215 char & operator [](int i)
00216 {
00217 return buffer[i];
00218 }
00219
00220 char & Last()
00221 {
00222 return buffer[len - 1];
00223 }
00224 char & First()
00225 {
00226 return buffer[0];
00227 }
00228
00229 void Grow(int newSize);
00230 void Swap(String & s);
00231
00232 char * LockBuffer(int size = -1);
00233 String & UnlockBuffer();
00234
00235 String & Read();
00236
00237 int ReadLine();
00238 void WriteLine();
00239 void Write();
00240
00241 String & Read(FILE * f);
00242
00243 int ReadLine(FILE * f);
00244 void WriteLine(FILE * f);
00245 void Write(FILE * f);
00246
00247 String & Read(IFILE & f);
00248
00249
00250
00251 int ReadLine(IFILE & f);
00252
00253 String Left(int count) const;
00254 String Right(int count) const;
00255 String Mid(int start, int end) const;
00256 String SubStr(int start, int count) const;
00257 String SubStr(int start) const;
00258
00259 int FindChar(char ch, int start = 0) const;
00260 int FastFindChar(char ch, int start = 0) const;
00261 int SlowFindChar(char ch, int start = 0) const;
00262
00263 int FindLastChar(char ch) const;
00264 int FastFindLastChar(char ch) const;
00265 int SlowFindLastChar(char ch) const;
00266
00267
00268
00269
00270 int Find(char ch, int start = 0) const
00271 {
00272 return(FindChar(ch, start));
00273 }
00274 int Find(const String & str, int start = 0) const;
00275 int FastFind(const String & str, int start = 0) const;
00276 int SlowFind(const String & str, int start = 0) const;
00277
00278 String & Filter(const String & s);
00279 String & Filter(const char * s);
00280
00281 String & ExcludeCharacters(const String & s);
00282 String & ExcludeCharacters(const char * s);
00283
00284 int Length() const
00285 {
00286 return len;
00287 }
00288 int BufferSize() const
00289 {
00290 return size;
00291 }
00292
00293 int SetLength(int newlen);
00294 int Dimension(int newlen)
00295 {
00296 return SetLength(newlen);
00297 }
00298
00299 String & Add(const String & s)
00300 {
00301 return *this += s;
00302 }
00303 String & Add(char ch)
00304 {
00305 return *this += ch;
00306 }
00307
00308 String RightToLeft();
00309 String & Invert();
00310 String & Invert(const String & s);
00311
00312 String & Trim();
00313 String & Trim(char character);
00314 vector<String> *Split(char splitChar);
00315
00316 long AsInteger() const;
00317 bool AsInteger(long& intValue) const;
00318 bool AsInteger(int& intValue) const;
00319 double AsDouble() const
00320 {
00321 return (double) *this;
00322 }
00323 long double AsLongDouble() const
00324 {
00325 return (long double) *this;
00326 }
00327
00328 int printf(const char * format, ...);
00329 int vprintf(const char * format, va_list arglist);
00330
00331 int catprintf(const char * format, ...);
00332 int vcatprintf(const char * format, va_list arglist);
00333
00334
00335
00336
00337 static int my_snprintf(char * buffer, int bufsize, const char * format, ...);
00338 static int my_vsnprintf(char * buffer, int bufsize, const char * format, va_list args);
00339 static void my_vsnprintf_close_file();
00340 static void check_vsnprintf();
00341
00342
00343 bool IsNumber();
00344
00345
00346 const unsigned char * uchar() const
00347 {
00348 return (unsigned char *) buffer;
00349 }
00350 const signed char * schar() const
00351 {
00352 return (signed char *) buffer;
00353 }
00354
00355 static FILE * my_vsnprintf_file;
00356
00357
00358 void Fill(char ch, int length = -1);
00359
00360 private:
00361
00362 static int vsnprintfChecked;
00363 };
00364
00365 inline int Compare(const String & s1, const String & s2)
00366 {
00367 return s1.Compare(s2);
00368 }
00369
00370 inline int Compare(const String & s1, const char * s2)
00371 {
00372 return s1.Compare(s2);
00373 }
00374
00375 inline int Compare(const char * s1, const String & s2)
00376 {
00377 return -s2.Compare(s1);
00378 }
00379
00380 inline int FastCompare(const String & s1, const String & s2)
00381 {
00382 return s1.FastCompare(s2);
00383 }
00384
00385 inline int FastCompare(const String & s1, const char * s2)
00386 {
00387 return s1.FastCompare(s2);
00388 }
00389
00390 inline int FastCompare(const char * s1, const String & s2)
00391 {
00392 return -s2.FastCompare(s1);
00393 }
00394
00395 inline int SlowCompare(const String & s1, const String & s2)
00396 {
00397 return s1.SlowCompare(s2);
00398 }
00399
00400 inline int SlowCompare(const String & s1, const char * s2)
00401 {
00402 return s1.SlowCompare(s2);
00403 }
00404
00405 inline int SlowCompare(const char * s1, const String & s2)
00406 {
00407 return -s2.SlowCompare(s1);
00408 }
00409
00410 String operator + (char lhs, const String & rhs);
00411 String operator + (const char * lhs, const String & rhs);
00412 String operator + (int lhs, const String & rhs);
00413 String operator + (unsigned int lhs, const String & rhs);
00414
00415 std::ostream& operator << (std::ostream& os, const String& s);
00416
00417
00418
00419
00420 inline InputFile& operator << (InputFile& stream, const String& str)
00421 {
00422 unsigned int numExpected = str.Length();
00423 unsigned int numWritten =
00424 stream.ifwrite(str.c_str(), numExpected);
00425 if(numExpected != numWritten)
00426 {
00427 std::cerr << "Failed to stream to IFILE, expected "
00428 << numExpected << " but only wrote "
00429 << numWritten << std::endl;
00430 }
00431 return(stream);
00432 }
00433
00434
00435 #endif
00436
00437
00438