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 67970 2013-03-13 10:10:06Z gcosmo $
30 //---------------------------------------------------------------
31 // GEANT 4 class implementation file
34 //---------------------------------------------------------------
36 // **************************************************************
38 // **************************************************************
40 G4SubString::G4SubString(const G4SubString& str)
41 : mystring(str.mystring), mystart(str.mystart), extent(str.extent)
45 G4SubString::G4SubString(G4String& str, str_size siz, str_size e)
46 : mystring(&str),mystart(siz),extent(e)
50 G4SubString& G4SubString::operator=(const G4String& str)
52 return operator=(str);
55 G4SubString& G4SubString::operator=(const G4SubString& str)
57 mystring->replace(mystart,extent,str.mystring->data(),str.length());
62 G4SubString& G4SubString::operator=(const char* str)
64 mystring->replace(mystart,extent,str,strlen(str));
69 char& G4SubString::operator()(str_size i)
71 return mystring->operator[](mystart+i);
74 char G4SubString::operator()(str_size i) const
76 return mystring->operator[](mystart+i);
79 char& G4SubString::operator[](str_size i)
81 return mystring->operator[](mystart+i);
84 char G4SubString::operator[](str_size i) const
86 return mystring->operator[](mystart+i);
89 G4int G4SubString::operator!() const
91 return (extent==0) ? 1 : 0;
94 G4bool G4SubString::operator==(const G4String& str) const
96 return (mystring->find(str,mystart,extent) != std::string::npos);
99 G4bool G4SubString::operator==(const char* str) const
101 return (mystring->find(str,mystart,extent) != std::string::npos);
104 G4bool G4SubString::operator!=(const G4String& str) const
106 return (mystring->find(str,mystart,extent) == std::string::npos);
109 G4bool G4SubString::operator!=(const char* str) const
111 return (mystring->find(str,mystart,extent) == std::string::npos);
114 str_size G4SubString::length() const
119 str_size G4SubString::start() const
124 G4bool G4SubString::isNull() const
129 // **************************************************************
131 // **************************************************************
133 G4String::G4String () {}
135 G4String::G4String ( const char * astring )
136 : std_string ( astring ) {}
138 G4String::G4String ( const char * astring, str_size len )
139 : std_string ( astring, len ) {}
141 G4String::G4String ( char ch )
146 std_string::operator=(str);
149 G4String::G4String ( const G4String& str )
152 G4String::G4String ( const G4SubString& str )
153 : std_string(*(str.mystring),str.mystart,str.extent) {}
155 G4String::G4String ( const std::string& str )
158 G4String& G4String::operator=(const G4String& str)
160 if (&str == this) { return *this; }
161 std_string::operator=(str);
165 G4String& G4String::operator=(const std::string& str)
167 std_string::operator=(str);
171 G4String& G4String::operator=(const char* str)
173 std_string::operator=(str);
177 // "cmp" optional parameter is NOT implemented !
178 // N.B.: The hash value returned is generally DIFFERENT from the
179 // one returned by the original RW function.
180 // Users should not rely on the specific return value.
182 char G4String::operator () (str_size i) const
184 return operator[](i);
187 char& G4String::operator () (str_size i)
189 return std_string::operator[](i);
192 G4String& G4String::operator+=(const G4SubString& str)
195 std_string::operator+=(tmp);
199 G4String& G4String::operator+=(const char* str)
201 std_string::operator+=(str);
205 G4String& G4String::operator+=(const std::string& str)
207 std_string::operator+=(str);
211 G4String& G4String::operator+=(const char& ch)
213 std_string::operator+=(ch);
217 G4bool G4String::operator==(const G4String& str) const
219 return (std_string::compare(str) == 0);
222 G4bool G4String::operator==(const char* str) const
224 return (std_string::compare(str) == 0);
227 G4bool G4String::operator!=(const G4String& str) const
229 return !(*this == str);
232 G4bool G4String::operator!=(const char* str) const
234 return !(*this == str);
237 G4String::operator const char*() const
242 G4int G4String::strcasecompare(const char* s1, const char* s2) const
244 char* buf1 = new char[strlen(s1)+1];
245 char* buf2 = new char[strlen(s2)+1];
247 for (str_size i=0; i<=strlen(s1); i++)
248 { buf1[i] = tolower(char(s1[i])); }
249 for (str_size j=0; j<=strlen(s2); j++)
250 { buf2[j] = tolower(char(s2[j])); }
252 G4int res = strcmp(buf1, buf2);
258 G4int G4String::compareTo(const char* str, caseCompare mode) const
260 return (mode==exact) ? strcmp(c_str(),str)
261 : strcasecompare(c_str(),str);
264 G4int G4String::compareTo(const G4String& str, caseCompare mode) const
266 return compareTo(str.c_str(), mode);
269 G4String& G4String::prepend (const char* str)
275 G4String& G4String::append(const G4String& str)
277 std_string::operator+=(str);
282 G4String::readLine (std::istream& strm, G4bool skipWhite)
288 strm.getline(tmp,1024);
293 strm.getline(tmp,1024);
299 G4String& G4String::replace (unsigned int start, unsigned int nbytes,
300 const char* buff, unsigned int n2 )
302 std_string::replace ( start, nbytes, buff, n2 );
306 G4String& G4String::replace(str_size pos, str_size n, const char* str)
308 std_string::replace(pos,n,str);
312 G4String& G4String::remove(str_size n)
314 if(n<size()) { erase(n,size()-n); }
318 G4String& G4String::remove(str_size pos, str_size N)
324 G4int G4String::first(char ch) const
329 G4int G4String::last(char ch) const
334 G4bool G4String::contains(const std::string& str) const
336 return (std_string::find(str) != std_string::npos);
339 G4bool G4String::contains(char ch) const
341 return (std_string::find(ch) != std_string::npos);
344 G4String G4String::strip (G4int strip_Type, char ch)
346 G4String retVal = *this;
347 if(length()==0) { return retVal; }
349 switch ( strip_Type ) {
352 for(i=0;i<length();i++)
353 { if (std_string::operator[](i) != ch) { break; } }
354 retVal = substr(i,length()-i);
360 for(j=length()-1;j>=0;j--)
361 { if (std_string::operator[](j) != ch) { break; } }
362 retVal = substr(0,j+1);
367 for(i=0;i<length();i++)
368 { if (std_string::operator[](i) != ch) { break; } }
369 G4String tmp(substr(i,length()-i));
371 for(k=tmp.length()-1;k>=0;k--)
372 { if (tmp.std_string::operator[](k) != ch) { break; } }
373 retVal = tmp.substr(0,k+1);
382 void G4String::toLower ()
384 for (str_size i=0; i<size();i++)
386 //GB:HP-UX-aCC,Linux-KCC
387 std_string::operator[](i) = tolower(char(std_string::operator[](i)));
388 //at(i) = tolower(at(i));
392 void G4String::toUpper ()
394 for (str_size i=0; i<size();i++)
396 //GB:HP-UX-aCC,Linux-KCC
397 std_string::operator[](i) = toupper(char(std_string::operator[](i)));
398 //at(i) = toupper(at(i));
402 G4bool G4String::isNull() const
407 // "caseCompare" optional parameter is NOT implemented !
409 str_size G4String::index( const G4String& str, str_size ln,
410 str_size st, G4String::caseCompare ) const
412 return std_string::find( str.c_str(), st, ln );
415 str_size G4String::index (const char* str, G4int pos) const
417 return std_string::find(str,pos);
420 str_size G4String::index (char ch, G4int pos) const
422 return std_string::find(ch,pos);
425 G4SubString G4String::operator()(str_size start, str_size extent)
427 return G4SubString(*this,start,extent);
430 const char* G4String::data() const
435 unsigned int G4String::hash( caseCompare ) const
437 const char* str=c_str();
445 unsigned int G4String::stlhash() const
447 const char* str=c_str();