string.icc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // $Id:$
00003 // ---------------------------------------------------------------------------
00004 
00005 #ifndef HEP_STRING_SRC
00006 #define HEP_STRING_SRC
00007 
00008 #include <iostream>
00009 #include <string.h>
00010 
00011 /*
00012  * Simplified string class.
00013  * It provides only few basic functions of the standard <string> and
00014  * is intended to be used as a replacement of the standard class where
00015  * full functionality of <string> is not required, but it is essential
00016  * to have highly portable and effective code.
00017  *
00018  * This file should be used exclusively inside *.cc files.
00019  * Usage inside header files can result to a clash with standard <string>.
00020  */
00021 struct string {
00022   struct srep {
00023     char* s;            // pointer to data
00024     int   n;            // reference count
00025     srep() : n(1) {}
00026   } *p;
00027   
00028   // Default constructor.
00029   string() { p = new srep; p->s = 0; } 
00030 
00031   // Constructor from character string.
00032   string(const char* s) {
00033     p = new srep; p->s = new char[strlen(s)+1]; strcpy(p->s, s);
00034   }
00035 
00036   // Constructor from character substring.
00037   string(const char* s, unsigned int n) {
00038     p = new srep; p->s = new char[n+1]; strncpy(p->s, s, n); *(p->s+n) = '\0';
00039   }
00040 
00041   // Copy constructor from string.
00042   string(const string& x) { x.p->n++; p = x.p; }
00043 
00044   // Destructor.
00045   ~string() { if (--p->n == 0) { delete [] p->s; delete p; } }
00046     
00047   // Assignment from character string.
00048   string& operator=(const char* s) {
00049     if (p->n > 1) {     // disconnect self
00050       p->n--;
00051       p = new srep;
00052     }else{
00053       delete [] p->s;   // free old string
00054     } 
00055     p->s = new char[strlen(s)+1];
00056     strcpy(p->s, s);
00057     return *this;
00058   } 
00059 
00060   // Assignment from string.
00061   string& operator=(const string & x) {
00062     x.p->n++;           // protect against "st = st"
00063     if (--p->n == 0) { delete [] p->s; delete p; }
00064     p = x.p;
00065     return *this;
00066   }
00067 
00068   // Returns C-style character string.
00069   const char* c_str() const { return p->s; }
00070 };
00071 
00072 //
00073 // Concatinations.
00074 //
00075 inline string operator+(char a, const string & b) {
00076   string s; s.p->s = new char[strlen(b.c_str())+2];
00077   s.p->s[0] = a; strcpy(s.p->s+1, b.c_str());
00078   return s;
00079 }
00080 
00081 inline string operator+(const char * a, const string & b) {
00082   int lena = strlen(a);
00083   string s; s.p->s = new char[lena+strlen(b.c_str())+1];
00084   strcpy(s.p->s, a); strcpy(s.p->s+lena, b.c_str());
00085   return s;
00086 }
00087 
00088 inline string operator+(const string & a, const char * b) {
00089   int lena = strlen(a.c_str());
00090   string s; s.p->s = new char[lena+strlen(b)+1];
00091   strcpy(s.p->s, a.c_str()); strcpy(s.p->s+lena, b);
00092   return s;
00093 }
00094 
00095 inline string operator+(const string & a, const string & b) {
00096   int lena = strlen(a.c_str());
00097   string s; s.p->s = new char[lena+strlen(b.c_str())+1];
00098   strcpy(s.p->s, a.c_str()); strcpy(s.p->s+lena, b.c_str());
00099   return s;
00100 }
00101   
00102 //
00103 // Comparisons.
00104 //
00105 inline bool operator==(const string & x, const char* s) {
00106   return strcmp(x.p->s, s) == 0;
00107 }
00108 inline bool operator==(const string & x, const string & y) {
00109   return strcmp(x.p->s, y.p->s) == 0;
00110 }
00111 inline bool operator!=(const string & x, const char* s) {
00112   return strcmp(x.p->s, s) != 0;
00113 }
00114 inline bool operator!=(const string & x, const string & y) {
00115   return strcmp(x.p->s, y.p->s) != 0;
00116 }
00117 
00118 //
00119 // Output to a stream.
00120 //
00121 std::ostream & operator<<(std::ostream & s, const string & x) {
00122   return s << x.p->s;
00123 } 
00124 
00125 #endif /* HEP_STRING_SRC */

Generated on Mon May 27 17:50:35 2013 for Geant4 by  doxygen 1.4.7