Geant4-11
string.icc
Go to the documentation of this file.
1// -*- C++ -*-
2// ---------------------------------------------------------------------------
3
4#ifndef HEP_STRING_SRC
5#define HEP_STRING_SRC
6
7#include <iostream>
8#include <string.h>
9
10/*
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.
16 *
17 * This file should be used exclusively inside *.cc files.
18 * Usage inside header files can result to a clash with standard <string>.
19 */
20struct string {
21 struct srep {
22 char* s; // pointer to data
23 int n; // reference count
24 srep() : n(1) {}
25 } *p;
26
27 // Default constructor.
28 string() { p = new srep; p->s = 0; }
29
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);
33 }
34
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';
38 }
39
40 // Copy constructor from string.
41 string(const string& x) { x.p->n++; p = x.p; }
42
43 // Destructor.
44 ~string() { if (--p->n == 0) { delete [] p->s; delete p; } }
45
46 // Assignment from character string.
47 string& operator=(const char* s) {
48 if (p->n > 1) { // disconnect self
49 p->n--;
50 p = new srep;
51 }else{
52 delete [] p->s; // free old string
53 }
54 p->s = new char[strlen(s)+1];
55 strcpy(p->s, s);
56 return *this;
57 }
58
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; }
63 p = x.p;
64 return *this;
65 }
66
67 // Returns C-style character string.
68 const char* c_str() const { return p->s; }
69};
70
71//
72// Concatinations.
73//
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());
77 return s;
78}
79
80inline string operator+(const char * a, const string & b) {
81 int lena = strlen(a);
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());
84 return s;
85}
86
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);
91 return s;
92}
93
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());
98 return s;
99}
100
101//
102// Comparisons.
103//
104inline bool operator==(const string & x, const char* s) {
105 return strcmp(x.p->s, s) == 0;
106}
107inline bool operator==(const string & x, const string & y) {
108 return strcmp(x.p->s, y.p->s) == 0;
109}
110inline bool operator!=(const string & x, const char* s) {
111 return strcmp(x.p->s, s) != 0;
112}
113inline bool operator!=(const string & x, const string & y) {
114 return strcmp(x.p->s, y.p->s) != 0;
115}
116
117//
118// Output to a stream.
119//
120std::ostream & operator<<(std::ostream & s, const string & x) {
121 return s << x.p->s;
122}
123
124#endif /* HEP_STRING_SRC */