2// ---------------------------------------------------------------------------
11 * Simplified string class.
12 * It provides only few basic functions of the standard <string> and
13 * is intended to be used as a replacement of the standard class where
14 * full functionality of <string> is not required, but it is essential
15 * to have highly portable and effective code.
17 * This file should be used exclusively inside *.cc files.
18 * Usage inside header files can result to a clash with standard <string>.
22 char* s; // pointer to data
23 int n; // reference count
27 // Default constructor.
28 string() { p = new srep; p->s = 0; }
30 // Constructor from character string.
31 string(const char* s) {
32 p = new srep; p->s = new char[strlen(s)+1]; strcpy(p->s, s);
35 // Constructor from character substring.
36 string(const char* s, unsigned int n) {
37 p = new srep; p->s = new char[n+1]; strncpy(p->s, s, n); *(p->s+n) = '\0';
40 // Copy constructor from string.
41 string(const string& x) { x.p->n++; p = x.p; }
44 ~string() { if (--p->n == 0) { delete [] p->s; delete p; } }
46 // Assignment from character string.
47 string& operator=(const char* s) {
48 if (p->n > 1) { // disconnect self
52 delete [] p->s; // free old string
54 p->s = new char[strlen(s)+1];
59 // Assignment from string.
60 string& operator=(const string & x) {
61 x.p->n++; // protect against "st = st"
62 if (--p->n == 0) { delete [] p->s; delete p; }
67 // Returns C-style character string.
68 const char* c_str() const { return p->s; }
74inline string operator+(char a, const string & b) {
75 string s; s.p->s = new char[strlen(b.c_str())+2];
76 s.p->s[0] = a; strcpy(s.p->s+1, b.c_str());
80inline string operator+(const char * a, const string & b) {
82 string s; s.p->s = new char[lena+strlen(b.c_str())+1];
83 strcpy(s.p->s, a); strcpy(s.p->s+lena, b.c_str());
87inline string operator+(const string & a, const char * b) {
88 int lena = strlen(a.c_str());
89 string s; s.p->s = new char[lena+strlen(b)+1];
90 strcpy(s.p->s, a.c_str()); strcpy(s.p->s+lena, b);
94inline string operator+(const string & a, const string & b) {
95 int lena = strlen(a.c_str());
96 string s; s.p->s = new char[lena+strlen(b.c_str())+1];
97 strcpy(s.p->s, a.c_str()); strcpy(s.p->s+lena, b.c_str());
104inline bool operator==(const string & x, const char* s) {
105 return strcmp(x.p->s, s) == 0;
107inline bool operator==(const string & x, const string & y) {
108 return strcmp(x.p->s, y.p->s) == 0;
110inline bool operator!=(const string & x, const char* s) {
111 return strcmp(x.p->s, s) != 0;
113inline bool operator!=(const string & x, const string & y) {
114 return strcmp(x.p->s, y.p->s) != 0;
118// Output to a stream.
120std::ostream & operator<<(std::ostream & s, const string & x) {
124#endif /* HEP_STRING_SRC */