2 // ********************************************************************
3 // * License and Disclaimer *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
27 // $Id: G4String.icc 95426 2016-02-10 14:54:59Z gcosmo $
30 //---------------------------------------------------------------
31 // GEANT 4 class implementation file
34 //---------------------------------------------------------------
36 // **************************************************************
38 // **************************************************************
40 inline G4SubString::G4SubString(const G4SubString& str)
41 : mystring(str.mystring), mystart(str.mystart), extent(str.extent)
45 inline G4SubString::G4SubString(G4String& str, str_size siz, str_size e)
46 : mystring(&str),mystart(siz),extent(e)
50 inline G4SubString& G4SubString::operator=(const G4SubString& str)
52 mystring->replace(mystart,extent,str.mystring->data(),str.length());
57 inline G4SubString& G4SubString::operator=(const char* str)
59 mystring->replace(mystart,extent,str,strlen(str));
64 inline char& G4SubString::operator()(str_size i)
66 return mystring->operator[](mystart+i);
69 inline char G4SubString::operator()(str_size i) const
71 return mystring->operator[](mystart+i);
74 inline char& G4SubString::operator[](str_size i)
76 return mystring->operator[](mystart+i);
79 inline char G4SubString::operator[](str_size i) const
81 return mystring->operator[](mystart+i);
84 inline G4int G4SubString::operator!() const
86 return (extent==0) ? 1 : 0;
89 inline G4bool G4SubString::operator==(const G4String& str) const
91 return (mystring->find(str,mystart,extent) != std::string::npos);
94 inline G4bool G4SubString::operator==(const char* str) const
96 return (mystring->find(str,mystart,extent) != std::string::npos);
99 inline G4bool G4SubString::operator!=(const G4String& str) const
101 return (mystring->find(str,mystart,extent) == std::string::npos);
104 inline G4bool G4SubString::operator!=(const char* str) const
106 return (mystring->find(str,mystart,extent) == std::string::npos);
109 inline str_size G4SubString::length() const
114 inline str_size G4SubString::start() const
119 inline G4bool G4SubString::isNull() const
124 // **************************************************************
126 // **************************************************************
128 inline G4String::G4String () {}
130 inline G4String::G4String ( const char * astring )
131 : std_string ( astring ) {}
133 inline G4String::G4String ( const char * astring, str_size len )
134 : std_string ( astring, len ) {}
136 inline G4String::G4String ( char ch )
141 std_string::operator=(str);
144 inline G4String::G4String ( const G4String& str )
147 inline G4String::G4String ( const G4SubString& str )
148 : std_string(*(str.mystring),str.mystart,str.extent) {}
150 inline G4String::G4String ( const std::string& str )
153 inline G4String& G4String::operator=(const G4String& str)
155 if (&str == this) { return *this; }
156 std_string::operator=(str);
160 inline G4String& G4String::operator=(const std::string& str)
162 std_string::operator=(str);
166 inline G4String& G4String::operator=(const char* str)
168 std_string::operator=(str);
172 // "cmp" optional parameter is NOT implemented !
173 // N.B.: The hash value returned is generally DIFFERENT from the
174 // one returned by the original RW function.
175 // Users should not rely on the specific return value.
177 inline char G4String::operator () (str_size i) const
179 return operator[](i);
182 inline char& G4String::operator () (str_size i)
184 return std_string::operator[](i);
187 inline G4String& G4String::operator+=(const G4SubString& str)
190 std_string::operator+=(tmp);
194 inline G4String& G4String::operator+=(const char* str)
196 std_string::operator+=(str);
200 inline G4String& G4String::operator+=(const std::string& str)
202 std_string::operator+=(str);
206 inline G4String& G4String::operator+=(const char& ch)
208 std_string::operator+=(ch);
212 inline G4bool G4String::operator==(const G4String& str) const
214 if (length() != str.length()) return false;
215 return (std_string::compare(str) == 0);
218 inline G4bool G4String::operator==(const char* str) const
220 return (std_string::compare(str) == 0);
223 inline G4bool G4String::operator!=(const G4String& str) const
225 return !(*this == str);
228 inline G4bool G4String::operator!=(const char* str) const
230 return !(*this == str);
233 inline G4String::operator const char*() const
238 inline G4int G4String::strcasecompare(const char* s1, const char* s2) const
240 char* buf1 = new char[strlen(s1)+1];
241 char* buf2 = new char[strlen(s2)+1];
243 for (str_size i=0; i<=strlen(s1); i++)
244 { buf1[i] = tolower(char(s1[i])); }
245 for (str_size j=0; j<=strlen(s2); j++)
246 { buf2[j] = tolower(char(s2[j])); }
248 G4int res = strcmp(buf1, buf2);
254 inline G4int G4String::compareTo(const char* str, caseCompare mode) const
256 return (mode==exact) ? strcmp(c_str(),str)
257 : strcasecompare(c_str(),str);
260 inline G4int G4String::compareTo(const G4String& str, caseCompare mode) const
262 return compareTo(str.c_str(), mode);
265 inline G4String& G4String::prepend (const char* str)
271 inline G4String& G4String::append(const G4String& str)
273 std_string::operator+=(str);
278 G4String::readLine (std::istream& strm, G4bool skipWhite)
284 strm.getline(tmp,1024);
289 strm.getline(tmp,1024);
295 inline G4String& G4String::replace (unsigned int start, unsigned int nbytes,
296 const char* buff, unsigned int n2 )
298 std_string::replace ( start, nbytes, buff, n2 );
302 inline G4String& G4String::replace(str_size pos, str_size n, const char* str)
304 std_string::replace(pos,n,str);
308 inline G4String& G4String::remove(str_size n)
310 if(n<size()) { erase(n,size()-n); }
314 inline G4String& G4String::remove(str_size pos, str_size N)
320 inline G4int G4String::first(char ch) const
325 inline G4int G4String::last(char ch) const
330 inline G4bool G4String::contains(const std::string& str) const
332 return (std_string::find(str) != std_string::npos);
335 inline G4bool G4String::contains(char ch) const
337 return (std_string::find(ch) != std_string::npos);
340 inline G4String G4String::strip (G4int strip_Type, char ch)
342 G4String retVal = *this;
343 if(length()==0) { return retVal; }
345 switch ( strip_Type ) {
348 for(i=0;i<length();i++)
349 { if (std_string::operator[](i) != ch) { break; } }
350 retVal = substr(i,length()-i);
356 for(j=length()-1;j>=0;j--)
357 { if (std_string::operator[](j) != ch) { break; } }
358 retVal = substr(0,j+1);
363 for(i=0;i<length();i++)
364 { if (std_string::operator[](i) != ch) { break; } }
365 G4String tmp(substr(i,length()-i));
367 for(k=tmp.length()-1;k>=0;k--)
368 { if (tmp.std_string::operator[](k) != ch) { break; } }
369 retVal = tmp.substr(0,k+1);
378 inline void G4String::toLower ()
380 for (str_size i=0; i<size();i++)
382 //GB:HP-UX-aCC,Linux-KCC
383 std_string::operator[](i) = tolower(char(std_string::operator[](i)));
384 //at(i) = tolower(at(i));
388 inline void G4String::toUpper ()
390 for (str_size i=0; i<size();i++)
392 //GB:HP-UX-aCC,Linux-KCC
393 std_string::operator[](i) = toupper(char(std_string::operator[](i)));
394 //at(i) = toupper(at(i));
398 inline G4bool G4String::isNull() const
403 // "caseCompare" optional parameter is NOT implemented !
405 inline str_size G4String::index( const G4String& str, str_size ln,
406 str_size st, G4String::caseCompare ) const
408 return std_string::find( str.c_str(), st, ln );
411 inline str_size G4String::index (const char* str, G4int pos) const
413 return std_string::find(str,pos);
416 inline str_size G4String::index (char ch, G4int pos) const
418 return std_string::find(ch,pos);
421 inline G4SubString G4String::operator()(str_size start, str_size extent)
423 return G4SubString(*this,start,extent);
426 inline const char* G4String::data() const
431 inline unsigned int G4String::hash( caseCompare ) const
433 const char* str=c_str();
441 inline unsigned int G4String::stlhash() const
443 const char* str=c_str();