source: trunk/source/global/management/include/G4String.icc @ 1315

Last change on this file since 1315 was 1193, checked in by garnier, 15 years ago

CVS update

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