Geant4  10.00.p01
string.icc
Go to the documentation of this file.
1 // -*- C++ -*-
2 // $Id:$
3 // ---------------------------------------------------------------------------
4 
5 #ifndef HEP_STRING_SRC
6 #define HEP_STRING_SRC
7 
8 #include <iostream>
9 #include <string.h>
10 
11 /*
12  * Simplified string class.
13  * It provides only few basic functions of the standard <string> and
14  * is intended to be used as a replacement of the standard class where
15  * full functionality of <string> is not required, but it is essential
16  * to have highly portable and effective code.
17  *
18  * This file should be used exclusively inside *.cc files.
19  * Usage inside header files can result to a clash with standard <string>.
20  */
21 struct string {
22  struct srep {
23  char* s; // pointer to data
24  int n; // reference count
25  srep() : n(1) {}
26  } *p;
27 
28  // Default constructor.
29  string() { p = new srep; p->s = 0; }
30 
31  // Constructor from character string.
32  string(const char* s) {
33  p = new srep; p->s = new char[strlen(s)+1]; strcpy(p->s, s);
34  }
35 
36  // Constructor from character substring.
37  string(const char* s, unsigned int n) {
38  p = new srep; p->s = new char[n+1]; strncpy(p->s, s, n); *(p->s+n) = '\0';
39  }
40 
41  // Copy constructor from string.
42  string(const string& x) { x.p->n++; p = x.p; }
43 
44  // Destructor.
45  ~string() { if (--p->n == 0) { delete [] p->s; delete p; } }
46 
47  // Assignment from character string.
48  string& operator=(const char* s) {
49  if (p->n > 1) { // disconnect self
50  p->n--;
51  p = new srep;
52  }else{
53  delete [] p->s; // free old string
54  }
55  p->s = new char[strlen(s)+1];
56  strcpy(p->s, s);
57  return *this;
58  }
59 
60  // Assignment from string.
61  string& operator=(const string & x) {
62  x.p->n++; // protect against "st = st"
63  if (--p->n == 0) { delete [] p->s; delete p; }
64  p = x.p;
65  return *this;
66  }
67 
68  // Returns C-style character string.
69  const char* c_str() const { return p->s; }
70 };
71 
72 //
73 // Concatinations.
74 //
75 inline string operator+(char a, const string & b) {
76  string s; s.p->s = new char[strlen(b.c_str())+2];
77  s.p->s[0] = a; strcpy(s.p->s+1, b.c_str());
78  return s;
79 }
80 
81 inline string operator+(const char * a, const string & b) {
82  int lena = strlen(a);
83  string s; s.p->s = new char[lena+strlen(b.c_str())+1];
84  strcpy(s.p->s, a); strcpy(s.p->s+lena, b.c_str());
85  return s;
86 }
87 
88 inline string operator+(const string & a, const char * b) {
89  int lena = strlen(a.c_str());
90  string s; s.p->s = new char[lena+strlen(b)+1];
91  strcpy(s.p->s, a.c_str()); strcpy(s.p->s+lena, b);
92  return s;
93 }
94 
95 inline string operator+(const string & a, const string & b) {
96  int lena = strlen(a.c_str());
97  string s; s.p->s = new char[lena+strlen(b.c_str())+1];
98  strcpy(s.p->s, a.c_str()); strcpy(s.p->s+lena, b.c_str());
99  return s;
100 }
101 
102 //
103 // Comparisons.
104 //
105 inline bool operator==(const string & x, const char* s) {
106  return strcmp(x.p->s, s) == 0;
107 }
108 inline bool operator==(const string & x, const string & y) {
109  return strcmp(x.p->s, y.p->s) == 0;
110 }
111 inline bool operator!=(const string & x, const char* s) {
112  return strcmp(x.p->s, s) != 0;
113 }
114 inline bool operator!=(const string & x, const string & y) {
115  return strcmp(x.p->s, y.p->s) != 0;
116 }
117 
118 //
119 // Output to a stream.
120 //
121 std::ostream & operator<<(std::ostream & s, const string & x) {
122  return s << x.p->s;
123 }
124 
125 #endif /* HEP_STRING_SRC */