sprint 1-alpha
|
00001 /****************************************************************************** 00002 * sprint::light_string 00003 * 00004 * Copyright (C) 2005-2011 Paolo Medici <www.pmx.it> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 * 00020 *******************************************************************************/ 00021 00030 #ifndef _SPRINT_LIGHT_STRING_H 00031 #define _SPRINT_LIGHT_STRING_H 00032 00033 #include <string.h> 00034 // User should include before 00035 // #include <sprint/buffer_cast.h> 00036 00037 namespace sprint { 00038 00044 class light_string { 00045 char *m_str; 00046 protected: 00048 inline void _dtor() 00049 { 00050 if(m_str) 00051 delete [] m_str; 00052 } 00054 inline void alloc(unsigned int size) 00055 { 00056 m_str = new char[size]; 00057 } 00058 00059 void _ctor(const char *str, int __lenght) 00060 { 00061 if(str) 00062 { 00063 int len = __lenght + 1; 00064 alloc(len); 00065 memcpy(m_str, str, __lenght); 00066 m_str[__lenght]=0; 00067 } 00068 else 00069 m_str = NULL; 00070 } 00072 void _ctor(const char *str) 00073 { 00074 if(str) 00075 { 00076 _ctor(str, strlen(str)); 00077 } 00078 else 00079 m_str = NULL; 00080 } 00081 00083 void assign(const char *str) 00084 { 00085 _dtor(); 00086 _ctor(str); 00087 } 00088 00089 public: 00090 00092 typedef int size_type; 00093 00095 static const size_type npos = -1; 00096 00097 public: 00098 inline 00099 light_string() : m_str(NULL) {} 00100 00101 inline 00102 light_string(const light_string &s, size_type __pos = 0) 00103 { 00104 _ctor(s.m_str + __pos); 00105 } 00106 00107 inline 00108 light_string(const light_string &s, size_type __pos, size_type __lenght) 00109 { 00110 _ctor(s.m_str + __pos, __lenght); 00111 } 00112 00114 inline 00115 light_string(const char *c, size_type __pos = 0) 00116 { 00117 _ctor(c + __pos); 00118 } 00119 00121 inline 00122 light_string(const char *c, size_type __pos, size_type __lenght) 00123 { 00124 _ctor(c + __pos, __lenght); 00125 } 00126 00128 char *reserve(unsigned int size) 00129 { 00130 alloc(size); 00131 return m_str; 00132 } 00133 00134 00136 inline 00137 light_string(const char *a, const char *b) 00138 { 00139 int len1 = strlen(a); 00140 int len2 = strlen(b); 00141 alloc(len1 + len2 + 1); 00142 memcpy(m_str, a, len1); 00143 memcpy(m_str + len1, b, len2); 00144 m_str[len1+len2]=0; 00145 } 00146 00147 #ifdef ENABLE_STRING_CAT_COSTRUCTOR 00148 00149 inline 00150 light_string(const char *a, const char *b, const char *c) 00151 { 00152 int len1 = strlen(a); 00153 int len2 = strlen(b); 00154 int len3 = strlen(c) + 1; 00155 alloc(len1 + len2 + len3); 00156 memcpy(m_str, a, len1); 00157 memcpy(m_str + len1, b, len2); 00158 memcpy(m_str + len1 + len2, c, len3); 00159 } 00160 00161 inline 00162 light_string(const char *a, const char *b, const char *c, const char *d) 00163 { 00164 int len1 = strlen(a); 00165 int len2 = strlen(b); 00166 int len3 = strlen(c); 00167 int len4 = strlen(d) + 1; 00168 alloc(len1 + len2 + len3 + len4); 00169 memcpy(m_str, a, len1); 00170 memcpy(m_str + len1, b, len2); 00171 memcpy(m_str + len1 + len2, c, len3); 00172 memcpy(m_str + len1 + len2 + len3, d, len4); 00173 } 00174 #endif 00175 00176 ~light_string() 00177 { 00178 _dtor(); 00179 } 00180 00181 inline bool IsValid() const { return m_str!=NULL;} 00182 00184 inline bool empty() const { return (m_str==NULL) || (m_str[0]==0); } 00185 00187 void assignf(const char *format, ...); 00188 00189 inline light_string & operator = (const char *str) 00190 { 00191 assign(str); 00192 return *this; 00193 } 00194 inline light_string & operator = (const light_string &s) 00195 { 00196 assign(s.m_str); 00197 return *this; 00198 } 00199 00201 void push_back(char c) 00202 { 00203 if(m_str) 00204 { 00205 int src_len = strlen(m_str); 00206 char *new_str = new char[src_len + 1 + 1]; 00207 memcpy(new_str, m_str, src_len); 00208 new_str[src_len]=c; 00209 new_str[src_len + 1]=0; 00210 delete [] m_str; 00211 m_str = new_str; 00212 } 00213 else 00214 _ctor(&c, 1); 00215 } 00216 00218 void append(const char *str, int add_len) 00219 { 00220 if(m_str) 00221 { 00222 int src_len = strlen(m_str); 00223 00224 char *new_str = new char[src_len + add_len+1]; 00225 memcpy(new_str, m_str, src_len); 00226 memcpy(new_str + src_len, str, add_len); 00227 new_str[src_len + add_len]=0; 00228 delete [] m_str; 00229 m_str = new_str; 00230 } 00231 else 00232 { 00233 _ctor(str, add_len); 00234 } 00235 } 00236 00237 void append(const char *str) 00238 { 00239 append(str, strlen(str)); 00240 } 00241 00242 void append(const light_string & str) 00243 { 00244 append(str.c_str()); 00245 } 00246 00247 void operator += (const char * str) 00248 { 00249 append(str); 00250 } 00251 00252 void operator += (const light_string & str) 00253 { 00254 append(str.c_str()); 00255 } 00256 00257 // operatore per ritornare il buffer 00258 inline char operator [](int i) const 00259 { 00260 return m_str[i]; 00261 } 00262 00263 inline char & operator [](int i) 00264 { 00265 return m_str[i]; 00266 } 00267 00268 // compatibilita verso STL 00269 // TODO: garantire di non ritornare NULL a scapito di un confronto aggiuntivo 00270 inline const char *c_str() const 00271 { 00272 return m_str; 00273 } 00274 00275 inline unsigned int size() const 00276 { 00277 return strlen(m_str); 00278 } 00279 00280 inline 00281 bool operator == (const char *c) const 00282 { 00283 return (IsValid()&&(c)) ? (!strcmp(m_str, c)) : false; 00284 } 00285 00286 inline 00287 bool operator == (const light_string & s) const 00288 { 00289 return !strcmp(m_str, s.m_str); 00290 } 00291 00293 bool Like(const char *c) const 00294 { 00295 return (IsValid()&&(c)) ? (!strcasecmp(m_str, c)) : false; 00296 } 00297 00298 // TODO case insensitive version? 00299 inline 00300 bool operator < (const char *string) const 00301 { 00302 return strcmp(m_str, string)==-1; 00303 } 00304 inline 00305 bool operator < (const light_string & string) const 00306 { 00307 return strcmp(m_str, string.m_str)==-1; 00308 } 00309 00310 inline 00311 bool operator > (const char *string) const 00312 { 00313 return strcmp(m_str, string)==+1; 00314 } 00315 00316 inline 00317 bool operator > (const light_string & string) const 00318 { 00319 return strcmp(m_str, string.m_str)==+1; 00320 } 00321 00323 size_type find_first_of(char c, size_type __pos = 0) const 00324 { 00325 const char *e = strchr(m_str + __pos, c); 00326 return (e == NULL) ? npos : (long) (e - m_str); 00327 } 00328 00329 size_type find_first_not_of(char c, size_type __pos = 0) const 00330 { 00331 if(m_str) 00332 { 00333 size_type i = __pos; 00334 while(m_str[i] != 0) { 00335 if(m_str[i] != c) 00336 return i; 00337 ++i; 00338 } 00339 return npos; 00340 } 00341 else 00342 { 00343 return npos; 00344 } 00345 } 00346 00347 size_type find_last_not_of(char c) const 00348 { 00349 if(m_str) 00350 { 00351 size_type i = strlen(m_str) - 1; 00352 while(i>0) { 00353 if(m_str[i] != c) 00354 return i; 00355 } 00356 return npos; 00357 } 00358 else 00359 { 00360 return npos; 00361 } 00362 } 00363 00364 size_type find_last_not_of(const char *pattern) const 00365 { 00366 if(m_str) 00367 { 00368 int npat = strlen(pattern); 00369 size_type i = strlen(m_str) - 1; 00370 while(i>0) { 00371 if(memchr(pattern, m_str[i], npat)==NULL) 00372 return i; 00373 } 00374 return npos; 00375 } 00376 else 00377 { 00378 return npos; 00379 } 00380 } 00381 00383 size_type find_last_of(char c, size_type __pos = 0) const 00384 { 00385 const char *e = strrchr(m_str + __pos, c); 00386 return (e == NULL) ? npos : (long) (e - m_str); 00387 } 00388 00389 }; 00390 00391 inline 00392 light_string operator +(const light_string &a, const char *str) 00393 { 00394 return light_string(a.c_str(), str); 00395 } 00396 00397 inline 00398 light_string operator +(const light_string &a, const light_string & b) 00399 { 00400 return light_string(a.c_str(), b.c_str()); 00401 } 00402 00403 } 00404 00405 #ifdef _BUFFER_CAST_H 00406 00407 namespace sprint { 00408 00409 template<> 00410 inline buffer_cast::buffer_cast<sprint::light_string>(const sprint::light_string & value) 00411 { 00412 ptr = value.c_str(); 00413 } 00414 00415 } 00416 00417 #endif 00418 00419 #endif