Geant4  10.02
G4String.icc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
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. *
10 // * *
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. *
17 // * *
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 // ********************************************************************
25 //
26 //
27 // $Id: G4String.icc 92939 2015-09-22 07:24:30Z gcosmo $
28 //
29 //
30 //---------------------------------------------------------------
31 // GEANT 4 class implementation file
32 //
33 // G4String
34 //---------------------------------------------------------------
35 
36 // **************************************************************
37 // G4SubString
38 // **************************************************************
39 
40 inline G4SubString::G4SubString(const G4SubString& str)
41  : mystring(str.mystring), mystart(str.mystart), extent(str.extent)
42 {
43 }
44 
45 inline G4SubString::G4SubString(G4String& str, str_size siz, str_size e)
46  : mystring(&str),mystart(siz),extent(e)
47 {
48 }
49 
50 inline G4SubString& G4SubString::operator=(const G4String& str)
51 {
52  return operator=(str);
53 }
54 
55 inline G4SubString& G4SubString::operator=(const G4SubString& str)
56 {
57  mystring->replace(mystart,extent,str.mystring->data(),str.length());
58  extent=str.length();
59  return *this;
60 }
61 
62 inline G4SubString& G4SubString::operator=(const char* str)
63 {
64  mystring->replace(mystart,extent,str,strlen(str));
65  extent=strlen(str);
66  return *this;
67 }
68 
69 inline char& G4SubString::operator()(str_size i)
70 {
71  return mystring->operator[](mystart+i);
72 }
73 
74 inline char G4SubString::operator()(str_size i) const
75 {
76  return mystring->operator[](mystart+i);
77 }
78 
79 inline char& G4SubString::operator[](str_size i)
80 {
81  return mystring->operator[](mystart+i);
82 }
83 
84 inline char G4SubString::operator[](str_size i) const
85 {
86  return mystring->operator[](mystart+i);
87 }
88 
89 inline G4int G4SubString::operator!() const
90 {
91  return (extent==0) ? 1 : 0;
92 }
93 
94 inline G4bool G4SubString::operator==(const G4String& str) const
95 {
96  return (mystring->find(str,mystart,extent) != std::string::npos);
97 }
98 
99 inline G4bool G4SubString::operator==(const char* str) const
100 {
101  return (mystring->find(str,mystart,extent) != std::string::npos);
102 }
103 
104 inline G4bool G4SubString::operator!=(const G4String& str) const
105 {
106  return (mystring->find(str,mystart,extent) == std::string::npos);
107 }
108 
109 inline G4bool G4SubString::operator!=(const char* str) const
110 {
111  return (mystring->find(str,mystart,extent) == std::string::npos);
112 }
113 
114 inline str_size G4SubString::length() const
115 {
116  return extent;
117 }
118 
119 inline str_size G4SubString::start() const
120 {
121  return mystart;
122 }
123 
124 inline G4bool G4SubString::isNull() const
125 {
126  return (extent==0);
127 }
128 
129 // **************************************************************
130 // G4String
131 // **************************************************************
132 
133 inline G4String::G4String () {}
134 
135 inline G4String::G4String ( const char * astring )
136  : std_string ( astring ) {}
137 
138 inline G4String::G4String ( const char * astring, str_size len )
139  : std_string ( astring, len ) {}
140 
141 inline G4String::G4String ( char ch )
142 {
143  char str[2];
144  str[0]=ch;
145  str[1]='\0';
146  std_string::operator=(str);
147 }
148 
149 inline G4String::G4String ( const G4String& str )
150  : std_string(str) {}
151 
152 inline G4String::G4String ( const G4SubString& str )
153  : std_string(*(str.mystring),str.mystart,str.extent) {}
154 
155 inline G4String::G4String ( const std::string& str )
156  : std_string(str) {}
157 
158 inline G4String& G4String::operator=(const G4String& str)
159 {
160  if (&str == this) { return *this; }
161  std_string::operator=(str);
162  return *this;
163 }
164 
165 inline G4String& G4String::operator=(const std::string& str)
166 {
167  std_string::operator=(str);
168  return *this;
169 }
170 
171 inline G4String& G4String::operator=(const char* str)
172 {
173  std_string::operator=(str);
174  return *this;
175 }
176 
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.
181 //
182 inline char G4String::operator () (str_size i) const
183 {
184  return operator[](i);
185 }
186 
187 inline char& G4String::operator () (str_size i)
188 {
189  return std_string::operator[](i);
190 }
191 
192 inline G4String& G4String::operator+=(const G4SubString& str)
193 {
194  G4String tmp(str);
195  std_string::operator+=(tmp);
196  return *this;
197 }
198 
199 inline G4String& G4String::operator+=(const char* str)
200 {
201  std_string::operator+=(str);
202  return *this;
203 }
204 
205 inline G4String& G4String::operator+=(const std::string& str)
206 {
207  std_string::operator+=(str);
208  return *this;
209 }
210 
211 inline G4String& G4String::operator+=(const char& ch)
212 {
213  std_string::operator+=(ch);
214  return *this;
215 }
216 
217 inline G4bool G4String::operator==(const G4String& str) const
218 {
219  if (length() != str.length()) return false;
220  return (std_string::compare(str) == 0);
221 }
222 
223 inline G4bool G4String::operator==(const char* str) const
224 {
225  return (std_string::compare(str) == 0);
226 }
227 
228 inline G4bool G4String::operator!=(const G4String& str) const
229 {
230  return !(*this == str);
231 }
232 
233 inline G4bool G4String::operator!=(const char* str) const
234 {
235  return !(*this == str);
236 }
237 
238 inline G4String::operator const char*() const
239 {
240  return c_str();
241 }
242 
243 inline G4int G4String::strcasecompare(const char* s1, const char* s2) const
244 {
245  char* buf1 = new char[strlen(s1)+1];
246  char* buf2 = new char[strlen(s2)+1];
247 
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])); }
252 
253  G4int res = strcmp(buf1, buf2);
254  delete [] buf1;
255  delete [] buf2;
256  return res;
257 }
258 
259 inline G4int G4String::compareTo(const char* str, caseCompare mode) const
260 {
261  return (mode==exact) ? strcmp(c_str(),str)
262  : strcasecompare(c_str(),str);
263 }
264 
265 inline G4int G4String::compareTo(const G4String& str, caseCompare mode) const
266 {
267  return compareTo(str.c_str(), mode);
268 }
269 
270 inline G4String& G4String::prepend (const char* str)
271 {
272  insert(0,str);
273  return *this;
274 }
275 
276 inline G4String& G4String::append(const G4String& str)
277 {
278  std_string::operator+=(str);
279  return *this;
280 }
281 
282 inline std::istream&
283 G4String::readLine (std::istream& strm, G4bool skipWhite)
284 {
285  char tmp[1024];
286  if ( skipWhite )
287  {
288  strm >> std::ws;
289  strm.getline(tmp,1024);
290  *this=tmp;
291  }
292  else
293  {
294  strm.getline(tmp,1024);
295  *this=tmp;
296  }
297  return strm;
298 }
299 
300 inline G4String& G4String::replace (unsigned int start, unsigned int nbytes,
301  const char* buff, unsigned int n2 )
302 {
303  std_string::replace ( start, nbytes, buff, n2 );
304  return *this;
305 }
306 
307 inline G4String& G4String::replace(str_size pos, str_size n, const char* str)
308 {
309  std_string::replace(pos,n,str);
310  return *this;
311 }
312 
313 inline G4String& G4String::remove(str_size n)
314 {
315  if(n<size()) { erase(n,size()-n); }
316  return *this;
317 }
318 
319 inline G4String& G4String::remove(str_size pos, str_size N)
320 {
321  erase(pos,N+pos);
322  return *this;
323 }
324 
325 inline G4int G4String::first(char ch) const
326 {
327  return find(ch);
328 }
329 
330 inline G4int G4String::last(char ch) const
331 {
332  return rfind(ch);
333 }
334 
335 inline G4bool G4String::contains(const std::string& str) const
336 {
337  return (std_string::find(str) != std_string::npos);
338 }
339 
340 inline G4bool G4String::contains(char ch) const
341 {
342  return (std_string::find(ch) != std_string::npos);
343 }
344 
345 inline G4String G4String::strip (G4int strip_Type, char ch)
346 {
347  G4String retVal = *this;
348  if(length()==0) { return retVal; }
349  str_size i=0;
350  switch ( strip_Type ) {
351  case leading:
352  {
353  for(i=0;i<length();i++)
354  { if (std_string::operator[](i) != ch) { break; } }
355  retVal = substr(i,length()-i);
356  }
357  break;
358  case trailing:
359  {
360  G4int j=0;
361  for(j=length()-1;j>=0;j--)
362  { if (std_string::operator[](j) != ch) { break; } }
363  retVal = substr(0,j+1);
364  }
365  break;
366  case both:
367  {
368  for(i=0;i<length();i++)
369  { if (std_string::operator[](i) != ch) { break; } }
370  G4String tmp(substr(i,length()-i));
371  G4int k=0;
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);
375  }
376  break;
377  default:
378  break;
379  }
380  return retVal;
381 }
382 
383 inline void G4String::toLower ()
384 {
385  for (str_size i=0; i<size();i++)
386  {
387  //GB:HP-UX-aCC,Linux-KCC
388  std_string::operator[](i) = tolower(char(std_string::operator[](i)));
389  //at(i) = tolower(at(i));
390  }
391 }
392 
393 inline void G4String::toUpper ()
394 {
395  for (str_size i=0; i<size();i++)
396  {
397  //GB:HP-UX-aCC,Linux-KCC
398  std_string::operator[](i) = toupper(char(std_string::operator[](i)));
399  //at(i) = toupper(at(i));
400  }
401 }
402 
403 inline G4bool G4String::isNull() const
404 {
405  return empty ();
406 }
407 
408 // "caseCompare" optional parameter is NOT implemented !
409 //
410 inline str_size G4String::index( const G4String& str, str_size ln,
411  str_size st, G4String::caseCompare ) const
412 {
413  return std_string::find( str.c_str(), st, ln );
414 }
415 
416 inline str_size G4String::index (const char* str, G4int pos) const
417 {
418  return std_string::find(str,pos);
419 }
420 
421 inline str_size G4String::index (char ch, G4int pos) const
422 {
423  return std_string::find(ch,pos);
424 }
425 
426 inline G4SubString G4String::operator()(str_size start, str_size extent)
427 {
428  return G4SubString(*this,start,extent);
429 }
430 
431 inline const char* G4String::data() const
432 {
433  return c_str();
434 }
435 
436 inline unsigned int G4String::hash( caseCompare ) const
437 {
438  const char* str=c_str();
439  unsigned long h = 0;
440  for ( ; *str; ++str)
441  { h = 5*h + *str; }
442 
443  return str_size(h);
444 }
445 
446 inline unsigned int G4String::stlhash() const
447 {
448  const char* str=c_str();
449  unsigned long h = 0;
450  for ( ; *str; ++str)
451  { h = 5*h + *str; }
452 
453  return str_size(h);
454 }