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