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