basix_doc 0.1
/Users/mourrain/Devel/mmx/basix/include/basix/string.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : string.hpp
00004 * DESCRIPTION: Strings with possible zero-characters
00005 * COPYRIGHT  : (C) 2004  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license and comes WITHOUT
00008 * ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details.
00009 * If you don't have this file, write to the Free Software Foundation, Inc.,
00010 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00011 ******************************************************************************/
00012 
00013 #ifndef __STRING_HPP
00014 #define __STRING_HPP
00015 #include <basix/type_props.hpp>
00017 
00018 namespace mmx {
00019 
00020 /******************************************************************************
00021 * The string class
00022 ******************************************************************************/
00023 
00024 class string;
00025 class string_rep REP_STRUCT {
00026 private:
00027   char* a;  // the string
00028   nat   n;  // length of string
00029   nat   l;  // allocated number of bytes
00030 
00031 public:
00032   inline string_rep (nat n2): a ((char*) mmx_malloc (n2)), n(n2), l(n2) {}
00033   inline ~string_rep () { mmx_free ((void*) a, l); }
00034   void resize (nat n);
00035   void extend (nat n);
00036 
00037   friend class string;
00038   friend inline nat N (const string& s);
00039   friend inline bool busy (const string& s);
00040   friend inline char* S (string& s);
00041   friend inline const char* S (const string& s);
00042   friend inline char read (const string& s, nat i);
00043   friend string copy (const string& s);
00044   friend string operator * (const string& s, const string& t);
00045   friend int as_int (const string& s);
00046   friend double as_double (const string& s);
00047   friend inline char* inside (const string& s, nat pos);
00048 };
00049 
00050 class string {
00051 INDIRECT_PROTO (string, string_rep)
00052 public:
00053   inline string (nat n=0): rep (new string_rep (n)) {}
00054   string (char c);
00055   string (const char *s);
00056   string (const char *s, nat n);
00057 
00059   friend inline nat N (const string& s) { return s.rep->n; }
00061   friend inline bool busy (const string& s) { return s.rep->n != 0; }
00063   friend inline char* S (string& s) { s.secure (); return s.rep->a; }
00065   friend inline const char* S (const string& s) { return s.rep->a; }
00067   friend inline char read (const string& s, nat i) { return s.rep->a[i]; }
00069   inline char operator [] (nat i) const { return rep->a[i]; }
00070   inline char& operator [] (nat i) { secure (); return rep->a[i]; }
00072   string operator () (nat start, nat end) const;
00074   string& operator << (char c);
00076   string& operator << (const string& t);
00078   string& operator >> (char& c);
00079   bool operator == (const char* s) const;
00080   bool operator != (const char* s) const;
00081   bool operator == (const string& s) const;
00082   bool operator != (const string& s) const;
00083   friend nat hash (const string& s);
00084   friend string copy (const string& s);
00085   friend string operator * (const string& s, const string& t);
00086   friend string as_string (void* ptr);
00087   friend string as_string (short int i);
00088   friend string as_string (short unsigned int i);
00089   friend string as_string (int i);
00090   friend string as_string (unsigned int i);
00091   friend string as_string (long int i);
00092   friend string as_string (long unsigned int i);
00093   friend string as_string (long long int i);
00094   friend string as_string (long long unsigned int i);
00095   friend string as_string (float x);
00096   friend string as_string (double x);
00097   friend string as_string_hexa (int i);
00098   friend int as_int (const string& s);
00099   friend double as_double (const string& s);
00100   friend string unescape (const string& s);
00101   friend char* inside (const string& s, nat pos);
00102 
00103   friend class string_rep;
00104 };
00105 INDIRECT_IMPL (string, string_rep)
00106 
00107 TRUE_TO_EXACT_IDENTITY_SUGAR(,string)
00108 template<> inline string duplicate (const string& s) { return copy (s); }
00109 
00110 inline char* inside (const string& s, nat pos) {
00111   return s.rep->a + pos; }
00112 inline void mem_copy (char* d, char* s, nat n) {
00113   for (nat i=0; i<n; i++) d[i]= s[i]; }
00114 
00115 /******************************************************************************
00116 * Conversion routines
00117 ******************************************************************************/
00118 
00119 string as_string (int i);
00120 string as_string (unsigned int i);
00121 string as_string (void* ptr);
00122 inline string as_string (short int i) {
00123   return as_string ((int) i); }
00124 inline string as_string (short unsigned int i) {
00125   return as_string ((unsigned int) i); }
00126 inline string as_string (signed char i) {
00127   return as_string ((int) i); }
00128 inline string as_string (unsigned char i) {
00129   return as_string ((unsigned int) i); }
00130 string as_string (long int i);
00131 string as_string (long unsigned int i);
00132 string as_string (long long int i);
00133 string as_string (long long unsigned int i);
00134 string as_string (float x);
00135 string as_string (double x);
00136 string as_string (long double x);
00137 
00138 inline string as_string_hexa (int i) {
00139   static char buf[16];  
00140   sprintf (buf, "%x", i);
00141   return buf; }
00142 int as_int (const string& s);
00143 double as_double (const string& s);
00144 
00145 /******************************************************************************
00146 * Further routines for exact numeric input and output
00147 ******************************************************************************/
00148 
00149 template<typename C> void
00150 numeric_to_string (const C& x, string& s) {
00151   if (x == 0) s << '0';
00152   else if (x <= 0) { // NOTE x < 0 would be nicer, but yields warnings
00153     s << '-';
00154     numeric_to_string (-x, s);
00155   }
00156   else {
00157     if (x >= 10) numeric_to_string (x/10, s);
00158     s << (char) (((char) '0') + ((char) ((int) (x%10))));
00159   }
00160 }
00161 
00162 template<typename C> void
00163 string_to_numeric (const string& s, C& val) {
00164   int i=0, n=N(s);
00165   val=0;
00166   if (n==0) return;
00167   if (s[0]=='-') i++;
00168   while (i<n) {
00169     if (s[i]<'0') break;
00170     if (s[i]>'9') break;
00171     val *= 10;
00172     val += (int) (s[i]-'0');
00173     i++;
00174   }
00175   if (s[0]=='-') val=-val;
00176 }
00177 
00178 STMPL void numeric_to_string (const float& x, string& s);
00179 STMPL void numeric_to_string (const double& x, string& s);
00180 STMPL void numeric_to_string (const long double& x, string& s);
00181 STMPL void string_to_numeric (const string& s, float& val);
00182 STMPL void string_to_numeric (const string& s, double& val);
00183 STMPL void string_to_numeric (const string& s, long double& val);
00184 
00185 template<typename C> string
00186 numeric_as_string (const C& x) {
00187   string s;
00188   numeric_to_string<C> (x, s);
00189   return s;
00190 }
00191 
00192 template<typename C> C
00193 string_as_numeric (const string& s) {
00194   C val;
00195   string_to_numeric<C> (s, val);
00196   return val;
00197 }
00198 
00199 /******************************************************************************
00200 * Further routines on strings
00201 ******************************************************************************/
00202 
00203 bool operator <  (const string& s, const string& t);
00204 bool operator <= (const string& s, const string& t);
00205 bool operator >  (const string& s, const string& t);
00206 bool operator >= (const string& s, const string& t);
00209 char* as_charp (const string& s);
00211 void free_charp (char* s);
00213 string escape (const string& s);
00215 string unescape (const string& s);
00217 string quote (const string& s);
00219 string unquote (const string& s);
00221 string locase (const string& s);
00223 string upcase (const string& s);
00225 string locase_first (const string& s);
00227 string upcase_first (const string& s);
00229 bool starts (const string& s, const string& what);
00231 bool ends (const string& s, const string& what);
00233 string replace (const string& s, const string& what, const string& by);
00235 int search_forwards (const string& s, const string& what, const int& pos);
00237 int search_backwards (const string& s, const string& what, const int& pos);
00239 string reverse (const string& s);
00241 vector<string> tokenize (const string& s, const string& sep, bool keep=false);
00243 string recompose (const vector<string>& v, const string& sep, bool last=true);
00244 
00245 } // namespace mmx
00246 #endif // __STRING_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines