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 92939 2015-09-22 07:24:30Z 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 G4String& str)
52 return operator=(str);
55 inline G4SubString& G4SubString::operator=(const G4SubString& str)
57 mystring->replace(mystart,extent,str.mystring->data(),str.length());
62 inline G4SubString& G4SubString::operator=(const char* str)
64 mystring->replace(mystart,extent,str,strlen(str));
69 inline char& G4SubString::operator()(str_size i)
71 return mystring->operator[](mystart+i);
74 inline char G4SubString::operator()(str_size i) const
76 return mystring->operator[](mystart+i);
79 inline char& G4SubString::operator[](str_size i)
81 return mystring->operator[](mystart+i);
84 inline char G4SubString::operator[](str_size i) const
86 return mystring->operator[](mystart+i);
89 inline G4int G4SubString::operator!() const
91 return (extent==0) ? 1 : 0;
94 inline G4bool G4SubString::operator==(const G4String& str) const
96 return (mystring->find(str,mystart,extent) != std::string::npos);
99 inline G4bool G4SubString::operator==(const char* str) const
101 return (mystring->find(str,mystart,extent) != std::string::npos);
104 inline G4bool G4SubString::operator!=(const G4String& str) const
106 return (mystring->find(str,mystart,extent) == std::string::npos);
109 inline G4bool G4SubString::operator!=(const char* str) const
111 return (mystring->find(str,mystart,extent) == std::string::npos);
114 inline str_size G4SubString::length() const
119 inline str_size G4SubString::start() const
124 inline G4bool G4SubString::isNull() const
129 // **************************************************************
131 // **************************************************************
133 inline G4String::G4String () {}
135 inline G4String::G4String ( const char * astring )
136 : std_string ( astring ) {}
138 inline G4String::G4String ( const char * astring, str_size len )
139 : std_string ( astring, len ) {}
141 inline G4String::G4String ( char ch )
146 std_string::operator=(str);
149 inline G4String::G4String ( const G4String& str )
152 inline G4String::G4String ( const G4SubString& str )
153 : std_string(*(str.mystring),str.mystart,str.extent) {}
155 inline G4String::G4String ( const std::string& str )
158 inline G4String& G4String::operator=(const G4String& str)
160 if (&str == this) { return *this; }
161 std_string::operator=(str);
165 inline G4String& G4String::operator=(const std::string& str)
167 std_string::operator=(str);
171 inline 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 inline char G4String::operator () (str_size i) const
184 return operator[](i);
187 inline char& G4String::operator () (str_size i)
189 return std_string::operator[](i);
192 inline G4String& G4String::operator+=(const G4SubString& str)
195 std_string::operator+=(tmp);
199 inline G4String& G4String::operator+=(const char* str)
201 std_string::operator+=(str);
205 inline G4String& G4String::operator+=(const std::string& str)
207 std_string::operator+=(str);
211 inline G4String& G4String::operator+=(const char& ch)
213 std_string::operator+=(ch);
217 inline G4bool G4String::operator==(const G4String& str) const
219 if (length() != str.length()) return false;
220 return (std_string::compare(str) == 0);
223 inline G4bool G4String::operator==(const char* str) const
225 return (std_string::compare(str) == 0);
228 inline G4bool G4String::operator!=(const G4String& str) const
230 return !(*this == str);
233 inline G4bool G4String::operator!=(const char* str) const
235 return !(*this == str);
238 inline G4String::operator const char*() const
243 inline G4int G4String::strcasecompare(const char* s1, const char* s2) const
245 char* buf1 = new char[strlen(s1)+1];
246 char* buf2 = new char[strlen(s2)+1];
248 for (str_size i=0; i<=strlen(s1); i++)
249 { buf1[i] = tolower(char(s1[i])); }
250 for (str_size j=0; j<=strlen(s2); j++)
251 { buf2[j] = tolower(char(s2[j])); }
253 G4int res = strcmp(buf1, buf2);
259 inline G4int G4String::compareTo(const char* str, caseCompare mode) const
261 return (mode==exact) ? strcmp(c_str(),str)
262 : strcasecompare(c_str(),str);
265 inline G4int G4String::compareTo(const G4String& str, caseCompare mode) const
267 return compareTo(str.c_str(), mode);
270 inline G4String& G4String::prepend (const char* str)
276 inline G4String& G4String::append(const G4String& str)
278 std_string::operator+=(str);
283 G4String::readLine (std::istream& strm, G4bool skipWhite)
289 strm.getline(tmp,1024);
294 strm.getline(tmp,1024);
300 inline G4String& G4String::replace (unsigned int start, unsigned int nbytes,
301 const char* buff, unsigned int n2 )
303 std_string::replace ( start, nbytes, buff, n2 );
307 inline G4String& G4String::replace(str_size pos, str_size n, const char* str)
309 std_string::replace(pos,n,str);
313 inline G4String& G4String::remove(str_size n)
315 if(n<size()) { erase(n,size()-n); }
319 inline G4String& G4String::remove(str_size pos, str_size N)
325 inline G4int G4String::first(char ch) const
330 inline G4int G4String::last(char ch) const
335 inline G4bool G4String::contains(const std::string& str) const
337 return (std_string::find(str) != std_string::npos);
340 inline G4bool G4String::contains(char ch) const
342 return (std_string::find(ch) != std_string::npos);
345 inline G4String G4String::strip (G4int strip_Type, char ch)
347 G4String retVal = *this;
348 if(length()==0) { return retVal; }
350 switch ( strip_Type ) {
353 for(i=0;i<length();i++)
354 { if (std_string::operator[](i) != ch) { break; } }
355 retVal = substr(i,length()-i);
361 for(j=length()-1;j>=0;j--)
362 { if (std_string::operator[](j) != ch) { break; } }
363 retVal = substr(0,j+1);
368 for(i=0;i<length();i++)
369 { if (std_string::operator[](i) != ch) { break; } }
370 G4String tmp(substr(i,length()-i));
372 for(k=tmp.length()-1;k>=0;k--)
373 { if (tmp.std_string::operator[](k) != ch) { break; } }
374 retVal = tmp.substr(0,k+1);
383 inline void G4String::toLower ()
385 for (str_size i=0; i<size();i++)
387 //GB:HP-UX-aCC,Linux-KCC
388 std_string::operator[](i) = tolower(char(std_string::operator[](i)));
389 //at(i) = tolower(at(i));
393 inline void G4String::toUpper ()
395 for (str_size i=0; i<size();i++)
397 //GB:HP-UX-aCC,Linux-KCC
398 std_string::operator[](i) = toupper(char(std_string::operator[](i)));
399 //at(i) = toupper(at(i));
403 inline G4bool G4String::isNull() const
408 // "caseCompare" optional parameter is NOT implemented !
410 inline str_size G4String::index( const G4String& str, str_size ln,
411 str_size st, G4String::caseCompare ) const
413 return std_string::find( str.c_str(), st, ln );
416 inline str_size G4String::index (const char* str, G4int pos) const
418 return std_string::find(str,pos);
421 inline str_size G4String::index (char ch, G4int pos) const
423 return std_string::find(ch,pos);
426 inline G4SubString G4String::operator()(str_size start, str_size extent)
428 return G4SubString(*this,start,extent);
431 inline const char* G4String::data() const
436 inline unsigned int G4String::hash( caseCompare ) const
438 const char* str=c_str();
446 inline unsigned int G4String::stlhash() const
448 const char* str=c_str();