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

Last change on this file since 1149 was 1058, checked in by garnier, 15 years ago

file release beta

File size: 10.6 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.11 2008/12/08 14:16:05 gcosmo Exp $
28// GEANT4 tag $Name: geant4-09-02-ref-02 $
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==(G4String s) const
97{
98  return mystring->substr(mystart,extent) == s ? true:false;
99}
100
101G4bool G4SubString::operator==(const char* s) const
102{
103  return mystring->substr(mystart,extent) == std::string(s) ? true:false;
104}
105
106G4bool G4SubString::operator!=(G4String s) const
107{
108  return mystring->substr(mystart,extent) != std::string(s) ? true:false;
109}
110
111G4bool G4SubString::operator!=(const char* s) const
112{
113  return mystring->substr(mystart,extent) != std::string(s) ? true:false;
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 ? true : false;
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  const std_string *a=this;
222  const std_string *b=&s;
223  return *a==*b;
224}
225
226G4bool G4String::operator==(const char* s) const
227{
228  const std_string *a=this;
229  const std_string b=s;
230  return *a==b;
231}
232
233G4bool G4String::operator!=(const G4String &s) const
234{
235  const std_string *a=this;
236  const std_string *b=&s;
237  return *a!=*b;
238}
239
240G4bool G4String::operator!=(const char* s) const
241{
242  const std_string *a=this;
243  const std_string b=s;
244  return *a!=b;
245}
246
247G4String::operator const char*() const
248{
249  return c_str();
250}
251
252G4int G4String::strcasecompare(const char* s1, const char* s2) const
253{
254  char* buf1 = new char[strlen(s1)+1];
255  char* buf2 = new char[strlen(s2)+1];
256
257  for (str_size i=0; i<=strlen(s1); i++)
258    { buf1[i] = tolower(char(s1[i])); }
259  for (str_size j=0; j<=strlen(s2); j++)
260    { buf2[j] = tolower(char(s2[j])); }
261
262  G4int res = strcmp(buf1, buf2);
263  delete [] buf1;
264  delete [] buf2;
265  return res;
266}
267
268G4int G4String::compareTo(const char* s, caseCompare mode)
269{
270  if(mode==exact)
271    { return strcmp(c_str(),s); }
272  else
273    { return strcasecompare(c_str(),s); }
274}
275
276G4int G4String::compareTo(const G4String& s, caseCompare mode)
277{
278  if(mode==exact)
279    { return strcmp(c_str(),s.c_str()); }
280  else
281    { return strcasecompare(c_str(),s.c_str()); }
282}
283
284G4String& G4String::prepend (const char* str)
285{
286  insert(0,str);
287  return *this;
288}
289
290G4String& G4String::append(const G4String& s)
291{
292  std_string::operator+=(s);
293  return *this;
294}
295
296std::istream&
297G4String::readLine (std::istream& s, G4bool skipWhite)
298{
299  char tmp[1024];
300  if ( skipWhite ) {
301    s >> std::ws;
302    s.getline(tmp,1024);
303    *this=tmp;
304  }
305  else {
306    s.getline(tmp,1024);   
307    *this=tmp;
308  }
309  return s;
310}
311
312G4String& G4String::replace (unsigned int start, unsigned int nbytes,
313                             const char* buff, unsigned int n2 )
314{
315  std_string::replace ( start, nbytes, buff, n2 );
316  return *this;                                                             
317}                                                                         
318
319G4String& G4String::replace(str_size pos, str_size n, const char* s)
320{
321  std_string::replace(pos,n,s);
322  return *this;
323}
324
325G4String& G4String::remove(str_size n)
326{
327  if(n<size())
328    { erase(n,size()-n); }
329  return *this;
330}
331
332G4String& G4String::remove(str_size pos, str_size N)
333{
334  erase(pos,N+pos);
335  return *this;
336}
337
338G4int G4String::first(char c) const
339{
340  return find(c);
341}
342
343G4int G4String::last(char c) const
344{
345  return rfind(c);
346}
347
348G4bool G4String::contains(std::string s) const
349{
350  G4int i=std_string::find(s);
351  if(i != G4int(std_string::npos))
352    { return true; }
353  else
354    { return false; }
355}
356
357G4bool G4String::contains(char c) const
358{
359  G4int i=std_string::find(c);
360  if(i != G4int(std_string::npos))
361    { return true; }
362  else
363    { return false; }
364}
365
366G4String G4String::strip (G4int strip_Type, char c)
367{
368  G4String retVal = *this;
369  if(length()==0) { return retVal; }
370  str_size i=0;
371  switch ( strip_Type ) {
372  case leading:
373    {
374      for(i=0;i<length();i++)
375        { if (std_string::operator[](i) != c) { break; } }
376      retVal = substr(i,length()-i);
377    }
378    break;
379  case trailing:
380    {
381      G4int j=0;
382      for(j=length()-1;j>=0;j--)
383        { if (std_string::operator[](j) != c) { break; } }
384      retVal = substr(0,j+1);
385    }
386    break;
387  case both:
388    {
389      for(i=0;i<length();i++)
390        { if (std_string::operator[](i) != c) { break; } }
391      G4String tmp(substr(i,length()-i));
392      G4int k=0;
393      for(k=tmp.length()-1;k>=0;k--)
394        { if (tmp.std_string::operator[](k) != c) { break; } }
395      retVal = tmp.substr(0,k+1);
396    }
397    break;
398  default:
399    break;
400  }
401  return retVal;
402}
403
404void G4String::toLower ()
405{
406  for (str_size i=0; i<size();i++)
407  {
408    //GB:HP-UX-aCC,Linux-KCC
409    std_string::operator[](i) = tolower(char(std_string::operator[](i)));
410    //at(i) = tolower(at(i));
411  }
412}
413
414void G4String::toUpper ()
415{
416  for (str_size i=0; i<size();i++)
417  {
418    //GB:HP-UX-aCC,Linux-KCC
419    std_string::operator[](i) = toupper(char(std_string::operator[](i)));
420    //at(i) = toupper(at(i));
421  }
422}
423
424G4bool G4String::isNull() const
425{
426  return empty ();
427}
428
429// "caseCompare" optional parameter is NOT implemented !
430//
431str_size G4String::index( const G4String& s, str_size ln,
432                          str_size st, G4String::caseCompare ) const
433{
434  return std_string::find( s.c_str(), st, ln );
435}
436
437str_size G4String::index (const char* str, G4int pos) const
438{
439  return std_string::find(str,pos);
440}
441
442str_size G4String::index (char c, G4int pos) const
443{
444  return std_string::find(c,pos);
445}
446
447G4SubString G4String::operator()(str_size start, str_size extent)
448{
449  return G4SubString(*this,start,extent);
450}
451
452const char* G4String::data() const
453{
454  return c_str();
455}
456
457unsigned int G4String::hash( caseCompare ) const
458{
459  const char*s=c_str();
460  unsigned long h = 0;
461  for ( ; *s; ++s)
462    { h = 5*h + *s; }
463
464  return str_size(h);
465}
466
467unsigned int G4String::stlhash() const
468{
469  const char*s=c_str();
470  unsigned long h = 0;
471  for ( ; *s; ++s)
472    { h = 5*h + *s; }
473
474  return str_size(h);
475}
476
477unsigned int G4String::hash(const G4String& s)
478{
479  return s.hash();
480}
Note: See TracBrowser for help on using the repository browser.