00001 #ifndef _T_STRING_HH
00002 #define _T_STRING_HH
00003
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include "TMapping.hh"
00007 #include "TSingletonDestroyerT.hh"
00008
00017 class TString
00018 {
00019 public:
00020
00024 TString ();
00025
00029 TString (const char *cs);
00030
00034 TString (const char *cs, int n);
00035
00039 TString (const TString& str);
00040
00044 TString (char c);
00045
00049 TString (char c, int n);
00050
00054 ~TString ();
00055
00061 inline operator const char*() const { return myMappingEntry->key; }
00062
00066 inline operator int() const { return Integer (); }
00067
00071 inline operator unsigned int() const { return UnsignedInteger (); }
00072
00076 TString& operator=(const char* cs);
00077
00081 TString& operator=(const TString& str);
00082
00087 inline bool operator==(const TString& str) const
00088 {
00089 return myMappingEntry == str.myMappingEntry;
00090 }
00091
00095 inline bool operator==(const char *cs) const
00096 {
00097 return ! strcmp (myMappingEntry->key, cs);
00098 }
00099
00104 inline bool operator!=(const TString& str)
00105 {
00106 return myMappingEntry != str.myMappingEntry;
00107 }
00108
00112 inline TString& operator+=(const char* cs)
00113 {
00114 return Append (cs, Length (cs));
00115 }
00116
00120 inline TString& operator+=(const TString& str)
00121 {
00122 return Append (str.myMappingEntry->key, str.myMappingEntry->bytes_used);
00123 }
00124
00128 inline TString& operator+=(char c)
00129 {
00130 return Append (&c, 1);
00131 }
00132
00136 inline TString operator+(const char *cs)
00137 {
00138 TString t (*this);
00139 t.Append (cs, Length (cs));
00140 return t;
00141 }
00142
00146 inline TString operator+(const TString& str)
00147 {
00148 TString t (*this);
00149 t.Append (str);
00150 return t;
00151 }
00152
00157 inline char operator[](int i) const
00158 {
00159 return ((char *)myMappingEntry->key)[i];
00160 }
00161
00165 inline TString& Append (char c)
00166 {
00167 return Append (&c, 1);
00168 }
00169
00173 TString& Append (char c, int n);
00174
00178 inline TString& Append (const char* cs)
00179 {
00180 return Append (cs, Length (cs));
00181 }
00182
00186 TString& Append (const char* cs, int n);
00187
00191 inline TString& Append (const TString& str)
00192 {
00193 return Append (str.myMappingEntry->key, str.myMappingEntry->bytes_used);
00194 }
00195
00196 #if ! (defined (_WIN32) || defined (__CYGWIN__))
00197
00201 TString BaseName () const;
00202
00207 TString DirName () const;
00208 #endif
00209
00215 inline const char *Data () const { return myMappingEntry->key; }
00216
00220 TString& Clear ();
00221
00227 inline bool Empty () const { return !myMappingEntry->bytes_used; }
00228
00237 TString& Format (const char *fmt, ...);
00238
00242 inline TString& Insert (int pos, char c)
00243 {
00244 return Insert (pos, c, 1);
00245 }
00246
00250 TString& Insert (int pos, char c, int n);
00251
00255 inline TString& Insert (int pos, const char *cs)
00256 {
00257 return Insert (pos, cs, TString::Length (cs));
00258 }
00259
00263 TString& Insert (int pos, const char *cs, int n);
00264
00268 inline TString& Insert (int pos, const TString& str)
00269 {
00270 return Insert (pos, str.myMappingEntry->key,
00271 str.myMappingEntry->bytes_used);
00272 }
00273
00279 inline unsigned int Hash () const { return myMappingEntry->hash; }
00280
00287 bool IsAscii () const;
00288
00294 bool IsDigits () const;
00295
00301 bool IsXDigits () const;
00302
00306 inline int Length () const { return myMappingEntry->bytes_used; }
00307
00315 static inline int Length (const char *cs)
00316 {
00317 int n = 0;
00318 while (*cs++)
00319 n++;
00320
00321 return n;
00322 }
00323
00333 int Integer () const;
00334
00338 TString& Lower ();
00339
00343 inline TString& Prepend (char c)
00344 {
00345 return Insert (0, c, 1);
00346 }
00347
00351 inline TString& Prepend (char c, int n)
00352 {
00353 return Insert (0, c, n);
00354 }
00355
00359 inline TString& Prepend (const char *cs)
00360 {
00361 return Insert (0, cs, TString::Length (cs));
00362 }
00363
00367 inline TString& Prepend (const char *cs, int n)
00368 {
00369 return Insert (0, cs, n);
00370 }
00371
00375 inline TString& Prepend (const TString& str)
00376 {
00377 return Insert (0, str.myMappingEntry->key,
00378 str.myMappingEntry->bytes_used);
00379 }
00380
00385 void Print (bool newline = true) const;
00386
00391 static void PrintStatus ()
00392 {
00393 ConstructMapping ();
00394 myMapping->PrintStatus ();
00395 }
00396
00400 TString& Trim ();
00401
00405 TString& TrimLeft ();
00406
00410 TString& TrimRight ();
00411
00415 TString& Truncate (int pos);
00416
00426 unsigned int UnsignedInteger () const;
00427
00431 TString& Upper ();
00432
00433
00434 private:
00435
00440 inline void GetEntry (const char *cs, int n)
00441 {
00442 unsigned int hash = TUtil::Hash (cs, n);
00443
00444 myMappingEntry = myMapping->Find (cs, n, hash);
00445 if (myMappingEntry)
00446 myMappingEntry->ref_count++;
00447 else
00448 myMappingEntry = myMapping->Insert (cs, n, hash);
00449 }
00450
00454 static void ConstructMapping ();
00455
00456
00457
00458
00459 static TMapping* myMapping;
00460
00461
00462 static TMappingEntry_t* myNullString;
00463
00464
00465
00466 static TSingletonDestroyerT<TMapping> myMappingDestroyer;
00467
00468
00469 TMappingEntry_t* myMappingEntry;
00470 };
00471
00472 #endif // _T_STRING_HH