G4String.icc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // 
00030 //---------------------------------------------------------------
00031 //  GEANT 4 class implementation file
00032 //
00033 //  G4String
00034 //---------------------------------------------------------------
00035 
00036 // **************************************************************
00037 // G4SubString
00038 // **************************************************************
00039 
00040 G4SubString::G4SubString(const G4SubString& str)
00041   : mystring(str.mystring), mystart(str.mystart), extent(str.extent)
00042 {
00043 }
00044 
00045 G4SubString::G4SubString(G4String& str, str_size siz, str_size e)
00046   : mystring(&str),mystart(siz),extent(e)
00047 {
00048 } 
00049 
00050 G4SubString& G4SubString::operator=(const G4String& str)
00051 {
00052   return operator=(str);
00053 }
00054 
00055 G4SubString& G4SubString::operator=(const G4SubString& str)
00056 {
00057   mystring->replace(mystart,extent,str.mystring->data(),str.length());
00058   extent=str.length();
00059   return *this;
00060 }
00061 
00062 G4SubString& G4SubString::operator=(const char* str)
00063 {
00064   mystring->replace(mystart,extent,str,strlen(str));
00065   extent=strlen(str);
00066   return *this;
00067 }
00068 
00069 char& G4SubString::operator()(str_size i)
00070 {
00071   return mystring->operator[](mystart+i);
00072 }
00073 
00074 char  G4SubString::operator()(str_size i) const
00075 {
00076   return mystring->operator[](mystart+i);
00077 }
00078 
00079 char&  G4SubString::operator[](str_size i)
00080 {
00081   return mystring->operator[](mystart+i);
00082 }
00083 
00084 char  G4SubString::operator[](str_size i) const
00085 {
00086   return mystring->operator[](mystart+i);
00087 }
00088 
00089 G4int G4SubString::operator!() const
00090 {
00091   return (extent==0) ? 1 : 0;
00092 }
00093 
00094 G4bool G4SubString::operator==(const G4String& str) const
00095 {
00096   return (mystring->find(str,mystart,extent) != std::string::npos);
00097 }
00098 
00099 G4bool G4SubString::operator==(const char* str) const
00100 {
00101   return (mystring->find(str,mystart,extent) != std::string::npos);
00102 }
00103 
00104 G4bool G4SubString::operator!=(const G4String& str) const
00105 {
00106   return (mystring->find(str,mystart,extent) == std::string::npos);
00107 }
00108 
00109 G4bool G4SubString::operator!=(const char* str) const
00110 {
00111   return (mystring->find(str,mystart,extent) == std::string::npos);
00112 }
00113 
00114 str_size G4SubString::length() const
00115 {
00116   return extent;
00117 }
00118 
00119 str_size G4SubString::start() const
00120 {
00121   return mystart;
00122 }
00123 
00124 G4bool G4SubString::isNull() const
00125 {
00126   return (extent==0);
00127 }
00128 
00129 // **************************************************************
00130 // G4String
00131 // **************************************************************
00132 
00133 G4String::G4String () {}
00134 
00135 G4String::G4String ( const char * astring )
00136  : std_string ( astring ) {}
00137 
00138 G4String::G4String ( const char * astring, str_size len )
00139  : std_string ( astring, len ) {} 
00140 
00141 G4String::G4String ( char ch )
00142 {
00143   char str[2];
00144   str[0]=ch;
00145   str[1]='\0';
00146   std_string::operator=(str);
00147 }
00148 
00149 G4String::G4String ( const G4String& str )
00150  : std_string(str) {}
00151 
00152 G4String::G4String ( const G4SubString& str )
00153  : std_string(*(str.mystring),str.mystart,str.extent) {}
00154 
00155 G4String::G4String ( const std::string& str )
00156  : std_string(str) {}
00157 
00158 G4String& G4String::operator=(const G4String& str)
00159 {
00160   if (&str == this) { return *this; }
00161   std_string::operator=(str);
00162   return *this;
00163 }
00164 
00165 G4String& G4String::operator=(const std::string& str)
00166 {
00167   std_string::operator=(str);
00168   return *this;
00169 }
00170 
00171 G4String& G4String::operator=(const char* str)
00172 {
00173   std_string::operator=(str);
00174   return *this;
00175 }
00176 
00177 // "cmp" optional parameter is NOT implemented ! 
00178 // N.B.: The hash value returned is generally DIFFERENT from the
00179 //       one returned by the original RW function.
00180 //       Users should not rely on the specific return value.
00181 //
00182 char G4String::operator () (str_size i) const
00183 {
00184   return operator[](i);
00185 }
00186 
00187 char& G4String::operator () (str_size i)
00188 {
00189   return std_string::operator[](i);
00190 }
00191 
00192 G4String& G4String::operator+=(const G4SubString& str)
00193 {
00194   G4String tmp(str);
00195   std_string::operator+=(tmp);
00196   return *this;
00197 }
00198 
00199 G4String& G4String::operator+=(const char* str)
00200 {
00201   std_string::operator+=(str);
00202   return *this;
00203 }
00204 
00205 G4String& G4String::operator+=(const std::string& str)
00206 {
00207   std_string::operator+=(str);
00208   return *this;
00209 }
00210 
00211 G4String& G4String::operator+=(const char& ch)
00212 {
00213   std_string::operator+=(ch);
00214   return *this;
00215 }
00216 
00217 G4bool G4String::operator==(const G4String& str) const
00218 {
00219   return (std_string::compare(str) == 0);
00220 }
00221 
00222 G4bool G4String::operator==(const char* str) const
00223 {
00224   return (std_string::compare(str) == 0);
00225 }
00226 
00227 G4bool G4String::operator!=(const G4String& str) const
00228 {
00229   return !(*this == str);
00230 }
00231 
00232 G4bool G4String::operator!=(const char* str) const
00233 {
00234   return !(*this == str);
00235 }
00236 
00237 G4String::operator const char*() const
00238 {
00239   return c_str();
00240 }
00241 
00242 G4int G4String::strcasecompare(const char* s1, const char* s2) const
00243 {
00244   char* buf1 = new char[strlen(s1)+1];
00245   char* buf2 = new char[strlen(s2)+1];
00246 
00247   for (str_size i=0; i<=strlen(s1); i++)
00248     { buf1[i] = tolower(char(s1[i])); }
00249   for (str_size j=0; j<=strlen(s2); j++)
00250     { buf2[j] = tolower(char(s2[j])); }
00251 
00252   G4int res = strcmp(buf1, buf2);
00253   delete [] buf1;
00254   delete [] buf2;
00255   return res;
00256 }
00257 
00258 G4int G4String::compareTo(const char* str, caseCompare mode) const
00259 {
00260   return (mode==exact) ? strcmp(c_str(),str)
00261                        : strcasecompare(c_str(),str);
00262 }
00263 
00264 G4int G4String::compareTo(const G4String& str, caseCompare mode) const
00265 {
00266   return compareTo(str.c_str(), mode);
00267 }
00268 
00269 G4String& G4String::prepend (const char* str)
00270 {
00271   insert(0,str);
00272   return *this;
00273 }
00274 
00275 G4String& G4String::append(const G4String& str)
00276 {
00277   std_string::operator+=(str);
00278   return *this;
00279 }
00280 
00281 std::istream&
00282 G4String::readLine (std::istream& strm, G4bool skipWhite)
00283 {
00284   char tmp[1024];
00285   if ( skipWhite )
00286   {
00287     strm >> std::ws;
00288     strm.getline(tmp,1024);
00289     *this=tmp;
00290   }
00291   else
00292   {
00293     strm.getline(tmp,1024);    
00294     *this=tmp;
00295   } 
00296   return strm;
00297 }
00298 
00299 G4String& G4String::replace (unsigned int start, unsigned int nbytes, 
00300                              const char* buff, unsigned int n2 )
00301 {
00302   std_string::replace ( start, nbytes, buff, n2 ); 
00303   return *this;                                                              
00304 }                                                                          
00305 
00306 G4String& G4String::replace(str_size pos, str_size n, const char* str)
00307 {
00308   std_string::replace(pos,n,str);
00309   return *this;
00310 }
00311 
00312 G4String& G4String::remove(str_size n)
00313 {
00314   if(n<size()) { erase(n,size()-n); }
00315   return *this;
00316 }
00317 
00318 G4String& G4String::remove(str_size pos, str_size N)
00319 {
00320   erase(pos,N+pos);
00321   return *this;
00322 }
00323 
00324 G4int G4String::first(char ch) const
00325 {
00326   return find(ch);
00327 }
00328 
00329 G4int G4String::last(char ch) const
00330 {
00331   return rfind(ch);
00332 }
00333 
00334 G4bool G4String::contains(const std::string& str) const
00335 {
00336   return (std_string::find(str) != std_string::npos);
00337 }
00338 
00339 G4bool G4String::contains(char ch) const
00340 {
00341   return (std_string::find(ch) != std_string::npos);
00342 }
00343 
00344 G4String G4String::strip (G4int strip_Type, char ch)
00345 {
00346   G4String retVal = *this;
00347   if(length()==0) { return retVal; }
00348   str_size i=0;
00349   switch ( strip_Type ) {
00350   case leading: 
00351     {
00352       for(i=0;i<length();i++)
00353         { if (std_string::operator[](i) != ch) { break; } }
00354       retVal = substr(i,length()-i);
00355     }
00356     break;
00357   case trailing:
00358     {
00359       G4int j=0;
00360       for(j=length()-1;j>=0;j--)
00361         { if (std_string::operator[](j) != ch) { break; } }
00362       retVal = substr(0,j+1);
00363     }
00364     break;
00365   case both:
00366     { 
00367       for(i=0;i<length();i++)
00368         { if (std_string::operator[](i) != ch) { break; } }
00369       G4String tmp(substr(i,length()-i));
00370       G4int k=0;
00371       for(k=tmp.length()-1;k>=0;k--)
00372         { if (tmp.std_string::operator[](k) != ch) { break; } }
00373       retVal = tmp.substr(0,k+1);
00374     }
00375     break;
00376   default:
00377     break;
00378   }
00379   return retVal;
00380 }
00381 
00382 void G4String::toLower ()
00383 {
00384   for (str_size i=0; i<size();i++)
00385   {
00386     //GB:HP-UX-aCC,Linux-KCC 
00387     std_string::operator[](i) = tolower(char(std_string::operator[](i)));
00388     //at(i) = tolower(at(i)); 
00389   } 
00390 }
00391 
00392 void G4String::toUpper ()
00393 {
00394   for (str_size i=0; i<size();i++)
00395   {
00396     //GB:HP-UX-aCC,Linux-KCC 
00397     std_string::operator[](i) = toupper(char(std_string::operator[](i)));
00398     //at(i) = toupper(at(i)); 
00399   }
00400 }
00401 
00402 G4bool G4String::isNull() const
00403 {
00404   return empty ();
00405 }
00406 
00407 // "caseCompare" optional parameter is NOT implemented !
00408 //
00409 str_size G4String::index( const G4String& str, str_size ln, 
00410                           str_size st, G4String::caseCompare ) const
00411 {
00412   return std_string::find( str.c_str(), st, ln );
00413 }
00414 
00415 str_size G4String::index (const char* str, G4int pos) const
00416 {
00417   return std_string::find(str,pos);
00418 }
00419 
00420 str_size G4String::index (char ch, G4int pos) const
00421 {
00422   return std_string::find(ch,pos);
00423 }
00424 
00425 G4SubString G4String::operator()(str_size start, str_size extent)
00426 {
00427   return G4SubString(*this,start,extent);
00428 }
00429 
00430 const char* G4String::data() const
00431 {
00432   return c_str();
00433 }
00434 
00435 unsigned int G4String::hash( caseCompare ) const
00436 {
00437   const char* str=c_str();
00438   unsigned long h = 0;
00439   for ( ; *str; ++str)
00440     { h = 5*h + *str; }
00441 
00442   return str_size(h);
00443 }
00444 
00445 unsigned int G4String::stlhash() const
00446 {
00447   const char* str=c_str();
00448   unsigned long h = 0;
00449   for ( ; *str; ++str)
00450     { h = 5*h + *str; }
00451 
00452   return str_size(h);
00453 }

Generated on Mon May 27 17:49:55 2013 for Geant4 by  doxygen 1.4.7