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

Last change on this file since 1006 was 850, checked in by garnier, 16 years ago

geant4.8.2 beta

File size: 10.5 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.9 2007/11/13 17:35:06 gcosmo Exp $
28// GEANT4 tag $Name: HEAD $
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 ( char c )
141{
142  char str[2];
143  str[0]=c;
144  str[1]='\0';
145  std_string::operator=(str);
146}
147
148G4String::G4String ( const G4String& s )
149 : std_string(s) {}
150
151G4String::G4String ( const G4SubString& s )
152 : std_string(*(s.mystring),s.mystart,s.extent) {}
153
154G4String::G4String ( const std::string &s )
155 : std_string(s) {}
156
157G4String& G4String::operator=(const G4String& s)
158{
159  if (&s == this) { return *this; }
160  std_string::operator=(s);
161  return *this;
162}
163
164G4String& G4String::operator=(const std::string &s)
165{
166  std_string::operator=(s);
167  return *this;
168}
169
170G4String& G4String::operator=(const char* s)
171{
172  std_string::operator=(s);
173  return *this;
174}
175
176// "cmp" optional parameter is NOT implemented !
177// N.B.: The hash value returned is generally DIFFERENT from the
178//       one returned by the original RW function.
179//       Users should not rely on the specific return value.
180//
181char G4String::operator () (str_size i) const
182{
183  return operator[](i);
184}
185
186char& G4String::operator () (str_size i)
187{
188  return std_string::operator[](i);
189}
190
191G4String& G4String::operator+=(const G4SubString&s)
192{
193  G4String tmp(s);
194  std_string::operator+=(tmp);
195  return *this;
196}
197
198G4String& G4String::operator+=(const char*s)
199{
200  std_string::operator+=(s);
201  return *this;
202}
203
204G4String& G4String::operator+=(const std::string &s)
205{
206  std_string::operator+=(s);
207  return *this;
208}
209
210G4String& G4String::operator+=(const char &c)
211{
212  std_string::operator+=(c);
213  return *this;
214}
215
216G4bool G4String::operator==(const G4String &s) const
217{
218  const std_string *a=this;
219  const std_string *b=&s;
220  return *a==*b;
221}
222
223G4bool G4String::operator==(const char* s) const
224{
225  const std_string *a=this;
226  const std_string b=s;
227  return *a==b;
228}
229
230G4bool G4String::operator!=(const G4String &s) const
231{
232  const std_string *a=this;
233  const std_string *b=&s;
234  return *a!=*b;
235}
236
237G4bool G4String::operator!=(const char* s) const
238{
239  const std_string *a=this;
240  const std_string b=s;
241  return *a!=b;
242}
243
244G4String::operator const char*() const
245{
246  return c_str();
247}
248
249G4int G4String::strcasecompare(const char* s1, const char* s2) const
250{
251  char* buf1 = new char[strlen(s1)+1];
252  char* buf2 = new char[strlen(s2)+1];
253
254  for (str_size i=0; i<=strlen(s1); i++)
255    { buf1[i] = tolower(char(s1[i])); }
256  for (str_size j=0; j<=strlen(s2); j++)
257    { buf2[j] = tolower(char(s2[j])); }
258
259  G4int res = strcmp(buf1, buf2);
260  delete [] buf1;
261  delete [] buf2;
262  return res;
263}
264
265G4int G4String::compareTo(const char* s, caseCompare mode)
266{
267  if(mode==exact)
268    { return strcmp(c_str(),s); }
269  else
270    { return strcasecompare(c_str(),s); }
271}
272
273G4int G4String::compareTo(const G4String& s, caseCompare mode)
274{
275  if(mode==exact)
276    { return strcmp(c_str(),s.c_str()); }
277  else
278    { return strcasecompare(c_str(),s.c_str()); }
279}
280
281G4String& G4String::prepend (const char* str)
282{
283  insert(0,str);
284  return *this;
285}
286
287G4String& G4String::append(const G4String& s)
288{
289  std_string::operator+=(s);
290  return *this;
291}
292
293std::istream&
294G4String::readLine (std::istream& s, G4bool skipWhite)
295{
296  char tmp[1024];
297  if ( skipWhite ) {
298    s >> std::ws;
299    s.getline(tmp,1024);
300    *this=tmp;
301  }
302  else {
303    s.getline(tmp,1024);   
304    *this=tmp;
305  }
306  return s;
307}
308
309G4String& G4String::replace (unsigned int start, unsigned int nbytes,
310                             const char* buff, unsigned int n2 )
311{
312  std_string::replace ( start, nbytes, buff, n2 );
313  return *this;                                                             
314}                                                                         
315
316G4String& G4String::replace(str_size pos, str_size n, const char* s)
317{
318  std_string::replace(pos,n,s);
319  return *this;
320}
321
322G4String& G4String::remove(str_size n)
323{
324  if(n<size())
325    { erase(n,size()-n); }
326  return *this;
327}
328
329G4String& G4String::remove(str_size pos, str_size N)
330{
331  erase(pos,N+pos);
332  return *this;
333}
334
335G4int G4String::first(char c) const
336{
337  return find(c);
338}
339
340G4int G4String::last(char c) const
341{
342  return rfind(c);
343}
344
345G4bool G4String::contains(std::string s) const
346{
347  G4int i=std_string::find(s);
348  if(i != G4int(std_string::npos))
349    { return true; }
350  else
351    { return false; }
352}
353
354G4bool G4String::contains(char c) const
355{
356  G4int i=std_string::find(c);
357  if(i != G4int(std_string::npos))
358    { return true; }
359  else
360    { return false; }
361}
362
363G4String G4String::strip (G4int strip_Type, char c)
364{
365  G4String retVal = *this;
366  if(length()==0) { return retVal; }
367  str_size i=0;
368  switch ( strip_Type ) {
369  case leading:
370    {
371      for(i=0;i<length();i++)
372        { if (std_string::operator[](i) != c) { break; } }
373      retVal = substr(i,length()-i);
374    }
375    break;
376  case trailing:
377    {
378      G4int j=0;
379      for(j=length()-1;j>=0;j--)
380        { if (std_string::operator[](j) != c) { break; } }
381      retVal = substr(0,j+1);
382    }
383    break;
384  case both:
385    {
386      for(i=0;i<length();i++)
387        { if (std_string::operator[](i) != c) { break; } }
388      G4String tmp(substr(i,length()-i));
389      G4int k=0;
390      for(k=tmp.length()-1;k>=0;k--)
391        { if (tmp.std_string::operator[](k) != c) { break; } }
392      retVal = tmp.substr(0,k+1);
393    }
394    break;
395  default:
396    break;
397  }
398  return retVal;
399}
400
401void G4String::toLower ()
402{
403  for (str_size i=0; i<size();i++)
404  {
405    //GB:HP-UX-aCC,Linux-KCC
406    std_string::operator[](i) = tolower(char(std_string::operator[](i)));
407    //at(i) = tolower(at(i));
408  }
409}
410
411void G4String::toUpper ()
412{
413  for (str_size i=0; i<size();i++)
414  {
415    //GB:HP-UX-aCC,Linux-KCC
416    std_string::operator[](i) = toupper(char(std_string::operator[](i)));
417    //at(i) = toupper(at(i));
418  }
419}
420
421G4bool G4String::isNull() const
422{
423  return empty ();
424}
425
426// "caseCompare" optional parameter is NOT implemented !
427//
428str_size G4String::index( const G4String& s, str_size ln,
429                          str_size st, G4String::caseCompare ) const
430{
431  return std_string::find( s.c_str(), st, ln );
432}
433
434str_size G4String::index (const char* str, G4int pos) const
435{
436  return std_string::find(str,pos);
437}
438
439str_size G4String::index (char c, G4int pos) const
440{
441  return std_string::find(c,pos);
442}
443
444G4SubString G4String::operator()(str_size start, str_size extent)
445{
446  return G4SubString(*this,start,extent);
447}
448
449const char* G4String::data() const
450{
451  return c_str();
452}
453
454unsigned int G4String::hash( caseCompare ) const
455{
456  const char*s=c_str();
457  unsigned long h = 0;
458  for ( ; *s; ++s)
459    { h = 5*h + *s; }
460
461  return str_size(h);
462}
463
464unsigned int G4String::stlhash() const
465{
466  const char*s=c_str();
467  unsigned long h = 0;
468  for ( ; *s; ++s)
469    { h = 5*h + *s; }
470
471  return str_size(h);
472}
473
474unsigned int G4String::hash(const G4String& s)
475{
476  return s.hash();
477}
Note: See TracBrowser for help on using the repository browser.