edelib  2.0.0
edelib/String.h
00001 /*
00002  * $Id: String.h 2839 2009-09-28 11:36:20Z karijes $
00003  *
00004  * A simple string class
00005  * Copyright (c) 2005-2007 edelib authors
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 #ifndef __EDELIB_STRING_H__
00022 #define __EDELIB_STRING_H__
00023 
00024 #include "edelib-global.h"
00025 #include <string.h>
00026 
00027 EDELIB_NS_BEGIN
00028 
00082 class EDELIB_API String {
00083 public: 
00084 #ifndef SKIP_DOCS
00085         typedef unsigned int size_type;
00086 #endif
00087 
00088 private:
00089 #ifndef SKIP_DOCS
00090         struct StringData {
00091                 size_type length;
00092                 size_type capacity;
00093                 char      *chars;
00094         };
00095 #endif
00096         static StringData null_data;
00097         StringData* sdata;
00098 
00099         void init(size_type len, size_type cap);
00100         void dispose(void);
00101 
00102 public:
00111         static const size_type npos;
00112 
00116         String();
00117 
00123         String(const char* str);
00124 
00130         String(const String& str);
00131 
00135         ~String();
00136 
00144         String& assign(const char* str, size_type len);
00145 
00152         String& assign(const char* str);
00153 
00160         String& assign(const String& str);
00161 
00169         String& append(const char* str, size_type len);
00170 
00177         String& append(const char* str);
00178 
00185         String& append(const String& str);
00186 
00187 
00195         String& append(size_type num, const char& ch);
00196 
00202         void reserve(size_type len);
00203 
00209         void swap(String& from);
00210 
00219         String substr(size_type index, size_type num = npos) const;
00220 
00228         size_type find(const char* str, size_type offset) const;
00229 
00238         size_type find(char ch, size_type offset) const;
00239 
00243         size_type find(const char* str) const;
00244 
00248         void clear(void);
00249 
00253         void printf(const char* fmt, ...);
00254 
00258         void trim_left(void);
00259 
00263         void trim_right(void);
00264 
00268         void trim(void);
00269 
00279         const char* c_str(void)  { return sdata->chars; }
00280 
00282         const char* c_str(void) const { return sdata->chars; }
00283 
00289         const char* data(void) const  { return sdata->chars; }
00290 
00292         size_type length(void) const { return sdata->length; }
00293 
00295         size_type capacity(void) const { return sdata->capacity; }
00296 
00298         bool empty(void) const  { return length() == 0; }
00299 
00307         String& replace(char c1, char c2);
00308 
00310         char& operator[](size_type index);
00311 
00313         char  operator[](size_type index) const;
00314 
00316         String& operator=(const char* str);
00317 
00319         String& operator=(const String& str);
00320 
00322         String& operator+=(const char* str);
00323 
00325         String& operator+=(const String& str);
00326 
00328         String& operator+=(const char& ch);
00329 };
00330 
00335 EDELIB_API String operator+(const String& s1, const String& s2);
00336 
00341 EDELIB_API String operator+(const char* s1, const String& s2);
00342 
00347 EDELIB_API String operator+(const String& s1, const char* s2);
00348 
00353 inline bool operator==(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) == 0); }
00354 
00359 inline bool operator!=(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) != 0); }
00360 
00365 inline bool operator>(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) > 0); }
00366 
00371 inline bool operator>=(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) >= 0); }
00372 
00377 inline bool operator<(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) < 0); }
00378 
00383 inline bool operator<=(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) <= 0); }
00384 
00389 inline bool operator==(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) == 0); }
00390 
00395 inline bool operator!=(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) != 0); }
00396 
00401 inline bool operator>(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) > 0); }
00402 
00407 inline bool operator>=(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) >= 0); }
00408 
00413 inline bool operator<(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) < 0); }
00414 
00419 inline bool operator<=(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) <= 0); }
00420 
00425 inline bool operator==(const String& str1, const String& str2) 
00426 { return (str1.length() == str2.length()) && (strcmp(str1.c_str(), str2.c_str()) == 0); }
00427 
00432 inline bool operator!=(const String& str1, const String& str2) { return (strcmp(str1.c_str(), str2.c_str()) != 0); }
00433 
00438 inline bool operator>(const String& str1, const String& str2) { return (strcmp(str1.c_str(), str2.c_str()) > 0); }
00439 
00444 inline bool operator>=(const String& str1, const String& str2) { return (strcmp(str1.c_str(), str2.c_str()) >= 0); }
00445 
00450 inline bool operator<(const String& str1, const String& str2) { return (strcmp(str1.c_str(), str2.c_str()) < 0); }
00451 
00456 inline bool operator<=(const String& str1, const String& str2) { return (strcmp(str1.c_str(), str2.c_str()) <= 0); }
00457 
00458 EDELIB_NS_END
00459 #endif