string.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
00002 // Copyright (C) 2006-2008 David Sugar, Tycho Softworks.
00003 //
00004 // This program is free software; you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation; either version 2 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free software
00019 // library without restriction.  Specifically, if other files instantiate
00020 // templates or use macros or inline functions from this file, or you compile
00021 // this file and link it with other files to produce an executable, this
00022 // file does not by itself cause the resulting executable to be covered by
00023 // the GNU General Public License.  This exception does not however
00024 // invalidate any other reasons why the executable file might be covered by
00025 // the GNU General Public License.
00026 //
00027 // This exception applies only to the code released under the name GNU
00028 // Common C++.  If you copy code from other releases into a copy of GNU
00029 // Common C++, as the General Public License permits, the exception does
00030 // not apply to the code that you add in this way.  To avoid misleading
00031 // anyone as to the status of such modified files, you must delete
00032 // this exception notice from them.
00033 //
00034 // If you write modifications of your own for GNU Common C++, it is your choice
00035 // whether to permit this exception to apply to your modifications.
00036 // If you do not wish that, delete this exception notice.
00037 //
00038 
00044 #ifndef CCXX_STRING_H_
00045 #define CCXX_STRING_H_
00046 
00047 #ifndef CCXX_MISSING_H_
00048 #include <cc++/missing.h>
00049 #endif
00050 
00051 #ifndef CCXX_STRCHAR_H_
00052 #include <cc++/strchar.h>
00053 #endif
00054 
00055 #ifdef  CCXX_NAMESPACES
00056 namespace ost {
00057 #endif
00058 
00059 class MemPager;
00060 
00077 class __EXPORT String
00078 {
00079 protected:
00080         static const unsigned minsize;
00081         static const unsigned slotsize;
00082         static const unsigned pagesize;
00083         static const unsigned slotlimit;
00084         static const unsigned slotcount;
00085 
00086         friend class StringObject;
00087 
00088 private:
00089         friend class MemPager;
00090 
00091         static MemPager *pager;
00092         static char **idx;
00093 
00094 #ifdef  CCXX_PACKED
00095 #pragma pack(1)
00096 #endif
00097 
00098         union {
00099                 struct {
00100                         char *text;
00101                         size_t size;
00102                         size_t length;
00103                 }       bigstring;
00104                 struct {
00105                         char text[(sizeof(char *) + (sizeof(size_t) * 2) + 1)];
00106                         char length : 6;
00107                         bool big : 1;
00108                 }       ministring;
00109         }       content;
00110 
00111 #ifdef  CCXX_PACKED
00112 #pragma pack()
00113 #endif
00114 
00115 protected:
00122         inline bool isBig(void) const
00123                 {return content.ministring.big;};
00124 
00133         const char *set(const char *str, size_t len = 0);
00134 
00141         void set(const String &str);
00142 
00143 #ifdef  HAVE_SNPRINTF
00144 
00151         const char *set(size_t size, const char *format, ...);
00152 #endif
00153 
00160         void copy(const String &str);
00161 
00165         void init(void);
00166 
00174         static char *getSpace(size_t size);
00175 
00183         size_t setSize(size_t size);
00184 
00190         void setLength(size_t len);
00191 
00202         virtual int compare(const char *text, size_t len = 0, size_t index = 0) const;
00203 
00213         size_t search(const char *text, size_t clen = 0, size_t offset = 0) const;
00214 
00215 public:
00216         static const size_t npos;
00217 
00218         typedef size_t size_type;
00219 
00223         String();
00224 
00230         String(const String &original);
00231 
00237         String(const char *str);
00238 
00244         String(std::string string);
00245 
00253         String(const String &str, size_t offset, size_t len = npos);
00254 
00255 #ifdef  HAVE_SNPRINTF
00256 
00262         String(size_t size, const char *format, ...);
00263 #else
00264 
00271         String(size_t count, const char *str);
00272 #endif
00273 
00280         String(size_t count, const char fill = ' ');
00281 
00285         virtual ~String();
00286 
00294         const char *getIndex(size_t index) const;
00295 
00301         char *getText(void) const;
00302 
00308         long getValue(long defvalue = 0l) const;
00309 
00315         bool getBool(bool defbool = false) const;
00316 
00322         const size_t getLength(void) const;
00323 
00329         const size_t getSize(void) const;
00330 
00336         bool isEmpty(void) const;
00337 
00343         void resize(size_t size);
00344 
00348         void clear(void);
00349 
00355         char at(ssize_t offset) const;
00356 
00365         unsigned count(const String &s, size_t offset = 0) const;
00366 
00376         unsigned count(const char *s, size_t offset = 0, size_t len = 0) const;
00377 
00385         String token(const char *delim = " \t\n\r", size_t offset = 0);
00386 
00395         size_t find(const String &s, size_t offset = 0, unsigned instance = 1) const;
00396 
00404         size_t rfind(const String &s, size_t offset = 0) const;
00405 
00415         size_t find(const char *s, size_t offset = 0, size_t len = 0, unsigned count = 1) const;
00416 
00425         size_t rfind(const char *s, size_t offset = 0, size_t len = 0) const;
00426 
00432         inline void trim(const char *cs)
00433                 {setLength(strtrim(cs, getText(), getLength()));};
00434 
00440         inline void chop(const char *cs)
00441                 {setLength(strchop(cs, getText(), getLength()));};
00442 
00448         void strip(const char *cs);
00449 
00455         inline void chop(size_t chars)
00456                 {erase(0, chars);};
00457 
00463         void trim(size_t count);
00464 
00471         void erase(size_t start, size_t len = npos);
00472 
00480         void insert(size_t start, const char *text, size_t len = 0);
00481 
00488         void insert(size_t start, const String &str);
00489 
00499         void replace(size_t start, size_t len, const char *text, size_t count = 0);
00500 
00509         void replace(size_t start, size_t len, const String &string);
00510 
00520         inline size_t find(unsigned instance, const char *text, size_t offset = 0, size_t len = 0) const
00521                 {return find(text, offset, len, instance);};
00522 
00531         inline size_t find(unsigned instance, const String &string, size_t offset = 0) const
00532                 {return find(string, offset, instance);};
00533 
00542         inline String substr(size_t start, size_t len) const
00543                 {return String(*this, start, len);};
00544 
00552         inline const char *(index)(size_t ind) const
00553                 {return getIndex(ind);};
00554 
00559         inline void compact(void)
00560                 {resize(getLength() + 1);};
00561 
00567         inline char *c_str(void) const
00568                 {return getText();};
00569 
00575         inline operator char *() const
00576                 {return getText();};
00577 
00583         inline bool operator!(void) const
00584                 {return isEmpty();};
00585 
00591         inline char *text(void) const
00592                 {return getText();};
00593 
00599         inline char *data(void) const
00600                 {return getText();};
00601 
00607         inline size_t length(void) const
00608                 {return strlen(getText());};
00609 
00615         inline size_t size(void) const
00616                 {return getLength();};
00617 
00623         inline size_t capacity(void) const
00624                 {return getSize();};
00625 
00629         bool empty(void) const
00630                 {return isEmpty();};
00631 
00638         void append(const char *str, size_t count = 0);
00639 
00640 #ifdef  HAVE_SNPRINTF
00641 
00647         void append(size_t size, const char *format, ...);
00648 #endif
00649 
00657         void append(const char *str, size_t offset, size_t count);
00658 
00664         void add(char c);
00665 
00671         void append(const String &str);
00672 
00678         inline const char operator[](unsigned ind) const
00679                 {return at(ind);};
00680 
00684         inline const char *operator =(const char *str)
00685                 {return set(str);};
00686 
00690         friend __EXPORT String operator+(const String &s1, const String &s2);
00691 
00692         friend __EXPORT String operator+(const String &s1, const char *s2);
00693 
00694         friend __EXPORT String operator+(const char *s1, const String &s2);
00695 
00696         friend __EXPORT String operator+(const String &s1, const char c2);
00697 
00698         friend __EXPORT String operator+(const char c1, const String &s2);
00699 
00703         inline String &operator+=(const String &str)
00704                 {append(str); return *this;};
00705 
00709         inline String &operator+=(char c)
00710                 {add(c); return *this;};
00711 
00715         inline String &operator+=(const char *str)
00716                 {append(str); return *this;};
00717 
00721         inline String &operator+=(const std::string &str)
00722                 {append(str.c_str()); return *this;};
00723 
00734         friend __EXPORT std::istream &getline(std::istream &is, String &str, char delim = '\n', size_t size = 0);
00735 
00740         friend __EXPORT std::ostream &operator<<(std::ostream &os, const String &str);
00741 
00745         inline friend std::istream &operator>>(std::istream &is, String &str)
00746                 {return getline(is, str);};
00747 
00748 #ifdef  HAVE_SNPRINTF
00749 
00757         friend __EXPORT int strprintf(String &str, size_t size, const char *format, ...);
00758 #endif
00759 
00760         bool operator<(const String &str) const;
00761         bool operator<(const char *str) const;
00762         bool operator>(const String &str) const;
00763         bool operator>(const char *str) const;
00764         bool operator<=(const String &str) const;
00765         bool operator<=(const char *str) const;
00766         bool operator>=(const String &str) const;
00767         bool operator>=(const char *str) const;
00768         bool operator==(const String &str) const;
00769         bool operator==(const char *str) const;
00770         bool operator!=(const String &str) const;
00771         bool operator!=(const char *str) const;
00772 
00773 #ifdef  HAVE_SNPRINTF
00774 
00778         inline String &operator+=(int i)
00779                 {append(16, "%d", i); return *this;};
00780 
00781         inline String &operator+=(unsigned int i)
00782                 {append(16, "%u", i); return *this;};
00783 
00784         inline String &operator+=(long l)
00785                 {append(16, "%l", l); return *this;};
00786 
00787         inline String &operator+=(unsigned long l)
00788                 {append(16, "%ul", l); return *this;};
00789 
00790         inline String &operator+=(float f)
00791                 {append(32, "%f", f); return *this;};
00792 
00793         inline String &operator+=(double d)
00794                 {append(32, "%f", d); return *this;};
00795 
00796         inline String &operator+=(short s)
00797                 {append(8, "%hd", s); return *this;};
00798 
00799         inline String &operator+=(unsigned short s)
00800                 {append(8, "%hu", s); return *this;};
00801 
00802 
00806         inline String &operator=(int i)
00807                 {set(16, "%d", i); return *this;};
00808 
00809         inline String &operator=(unsigned int i)
00810                 {set(16, "%u", i); return *this;};
00811 
00812         inline String &operator=(long l)
00813                 {set(16, "%l", l); return *this;};
00814 
00815         inline String &operator=(unsigned long l)
00816                 {set(16, "%ul", l); return *this;};
00817 
00818         inline String &operator=(float f)
00819                 {set(32, "%f", f); return *this;};
00820 
00821         inline String &operator=(double d)
00822                 {set(32, "%f", d); return *this;};
00823 
00824         inline String &operator=(short s)
00825                 {set(8, "%hd", s); return *this;};
00826 
00827         inline String &operator=(unsigned short s)
00828                 {set(8, "%hu", s); return *this;};
00829 #endif
00830 
00831         inline String &operator=(const String &original)
00832                 {copy(original); return *this;};
00833 
00837         bool operator*=(const String &str) const;
00838 
00842         bool operator*=(const char *str) const;
00843 };
00844 
00845 class __EXPORT SString : public String, protected std::streambuf, public std::ostream
00846 {
00847 protected:
00853         int overflow(int c);
00854 
00855 public:
00859         SString();
00860 
00864         SString(const SString &from);
00865 
00869         ~SString();
00870 };
00871 
00881 class __EXPORT StringObject
00882 {
00883 public:
00887         void *operator new(size_t size) NEW_THROWS;
00888 
00892         void operator delete(void *obj);
00893 };
00894 
00895 #ifdef  CCXX_NAMESPACES
00896 }
00897 #endif
00898 
00899 #endif

Generated on Tue Apr 7 05:42:02 2009 for GNU CommonC++ by  doxygen 1.4.7