00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00043 #ifndef CCXX_STRING_H_
00044 #define CCXX_STRING_H_
00045
00046 #ifndef CCXX_MISSING_H_
00047 #include <cc++/missing.h>
00048 #endif
00049
00050 #ifndef CCXX_STRCHAR_H_
00051 #include <cc++/strchar.h>
00052 #endif
00053
00054 #ifdef CCXX_NAMESPACES
00055 namespace ost {
00056 #endif
00057
00058 class MemPager;
00059
00076 class __EXPORT String
00077 {
00078 protected:
00079 static const unsigned minsize;
00080 static const unsigned slotsize;
00081 static const unsigned pagesize;
00082 static const unsigned slotlimit;
00083 static const unsigned slotcount;
00084
00085 friend class StringObject;
00086
00087 private:
00088 friend class MemPager;
00089
00090 static MemPager *pager;
00091 static char **idx;
00092
00093 #ifdef CCXX_PACKED
00094 #pragma pack(1)
00095 #endif
00096
00097 union
00098 {
00099 struct
00100 {
00101 char *text;
00102 size_t size;
00103 size_t length;
00104 } bigstring;
00105 struct
00106 {
00107 char text[(sizeof(char *) + (sizeof(size_t) * 2) + 1)];
00108 char length : 6;
00109 bool big : 1;
00110 } ministring;
00111 } content;
00112
00113 #ifdef CCXX_PACKED
00114 #pragma pack()
00115 #endif
00116
00117 protected:
00124 inline bool isBig(void) const
00125 {return content.ministring.big;};
00126
00135 const char *set(const char *str, size_t len = 0);
00136
00143 void set(const String &str);
00144
00145 #ifdef HAVE_SNPRINTF
00146
00152 const char *set(size_t size, const char *format, ...);
00153 #endif
00154
00161 void copy(const String &str);
00162
00166 void init(void);
00167
00175 static char *getSpace(size_t size);
00176
00184 size_t setSize(size_t size);
00185
00191 void setLength(size_t len);
00192
00203 virtual int compare(const char *text, size_t len = 0, size_t index = 0) const;
00204
00214 size_t search(const char *text, size_t clen = 0, size_t offset = 0) const;
00215
00216 public:
00217 static const size_t npos;
00218
00219 typedef size_t size_type;
00220
00224 String();
00225
00231 String(const String &original);
00232
00238 String(const char *str);
00239
00245 String(std::string str);
00246
00254 String(const String &str, size_t offset, size_t len = npos);
00255
00256 #ifdef HAVE_SNPRINTF
00257
00264 String(size_t count, const char *fmt, ...);
00265 #else
00266
00273 String(size_t count, const char *str);
00274 #endif
00275
00282 String(size_t count, const char fill = ' ');
00283
00287 virtual ~String();
00288
00296 const char *getIndex(size_t index) const;
00297
00303 char *getText(void) const;
00304
00310 long getValue(long defvalue = 0l) const;
00311
00317 bool getBool(bool defbool = false) const;
00318
00324 const size_t getLength(void) const;
00325
00331 const size_t getSize(void) const;
00332
00338 bool isEmpty(void) const;
00339
00345 void resize(size_t size);
00346
00350 void clear(void);
00351
00357 char at(ssize_t offset) const;
00358
00367 unsigned count(const String &s, size_t offset = 0) const;
00368
00378 unsigned count(const char *s, size_t offset = 0, size_t len = 0) const;
00379
00387 String token(const char *delim = " \t\n\r", size_t offset = 0);
00388
00397 size_t find(const String &s, size_t offset = 0, unsigned instance = 1) const;
00398
00406 size_t rfind(const String &s, size_t offset = 0) const;
00407
00417 size_t find(const char *s, size_t offset = 0, size_t len = 0, unsigned count = 1) const;
00418
00427 size_t rfind(const char *s, size_t offset = 0, size_t len = 0) const;
00428
00434 inline void trim(const char *cs)
00435 {setLength(strtrim(cs, getText(), getLength()));};
00436
00442 inline void chop(const char *cs)
00443 {setLength(strchop(cs, getText(), getLength()));};
00444
00450 void strip(const char *cs);
00451
00457 inline void chop(size_t chars)
00458 {erase(0, chars);};
00459
00465 void trim(size_t count);
00466
00473 void erase(size_t start, size_t len = npos);
00474
00482 void insert(size_t start, const char *text, size_t len = 0);
00483
00490 void insert(size_t start, const String &str);
00491
00501 void replace(size_t start, size_t len, const char *text, size_t count = 0);
00502
00511 void replace(size_t start, size_t len, const String &s);
00512
00522 inline size_t find(unsigned instance, const char *cp, size_t offset = 0, size_t len = 0) const
00523 {return find(cp, offset, len, instance);};
00524
00533 inline size_t find(unsigned instance, const String &s, size_t offset = 0) const
00534 {return find(s, offset, instance);};
00535
00544 inline String substr(size_t start, size_t len) const
00545 {return String(*this, start, len);};
00546
00554 inline const char *(index)(size_t ind) const
00555 {return getIndex(ind);};
00556
00561 inline void compact(void)
00562 {resize(getLength() + 1);};
00563
00569 inline char *c_str(void) const
00570 {return getText();};
00571
00577 inline operator char *() const
00578 {return getText();};
00579
00585 inline bool operator!(void) const
00586 {return isEmpty();};
00587
00593 inline char *text(void) const
00594 {return getText();};
00595
00601 inline char *data(void) const
00602 {return getText();};
00603
00609 inline size_t length(void) const
00610 {return strlen(getText());};
00611
00617 inline size_t size(void) const
00618 {return getLength();};
00619
00625 inline size_t capacity(void) const
00626 {return getSize();};
00627
00631 bool empty(void) const
00632 {return isEmpty();};
00633
00640 void append(const char *str, size_t count = 0);
00641
00642 #ifdef HAVE_SNPRINTF
00643
00649 void append(size_t size, const char *format, ...);
00650 #endif
00651
00659 void append(const char *str, size_t offset, size_t count);
00660
00666 void add(char c);
00667
00673 void append(const String &str);
00674
00680 inline const char operator[](unsigned ind) const
00681 {return at(ind);};
00682
00686 inline const char *operator =(const char *str)
00687 {return set(str);};
00688
00692 friend __EXPORT String operator+(const String &s1, const String &s2);
00693
00694 friend __EXPORT String operator+(const String &s1, const char *s2);
00695
00696 friend __EXPORT String operator+(const char *s1, const String &s2);
00697
00698 friend __EXPORT String operator+(const String &s1, const char c2);
00699
00700 friend __EXPORT String operator+(const char c1, const String &s2);
00701
00705 inline String &operator+=(const String &str)
00706 {append(str); return *this;};
00707
00711 inline String &operator+=(char c)
00712 {add(c); return *this;};
00713
00717 inline String &operator+=(const char *str)
00718 {append(str); return *this;};
00719
00723 inline String &operator+=(const std::string &str)
00724 {append(str.c_str()); return *this;};
00725
00736 friend __EXPORT std::istream &getline(std::istream &is, String &str, char delim = '\n', size_t size = 0);
00737
00742 friend __EXPORT std::ostream &operator<<(std::ostream &os, const String &str);
00743
00747 inline friend std::istream &operator>>(std::istream &is, String &str)
00748 {return getline(is, str);};
00749
00750 #ifdef HAVE_SNPRINTF
00751
00759 friend __EXPORT int strprintf(String &str, size_t size, const char *fmt, ...);
00760 #endif
00761
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 bool operator!=(const String &str) const;
00773 bool operator!=(const char *str) const;
00774
00775 #ifdef HAVE_SNPRINTF
00776
00780 inline String &operator+=(int i)
00781 {append(16, "%d", i); return *this;};
00782
00783 inline String &operator+=(unsigned int i)
00784 {append(16, "%u", i); return *this;};
00785
00786 inline String &operator+=(long l)
00787 {append(16, "%l", l); return *this;};
00788
00789 inline String &operator+=(unsigned long l)
00790 {append(16, "%ul", l); return *this;};
00791
00792 inline String &operator+=(float f)
00793 {append(32, "%f", f); return *this;};
00794
00795 inline String &operator+=(double d)
00796 {append(32, "%f", d); return *this;};
00797
00798 inline String &operator+=(short s)
00799 {append(8, "%hd", s); return *this;};
00800
00801 inline String &operator+=(unsigned short s)
00802 {append(8, "%hu", s); return *this;};
00803
00804
00808 inline String &operator=(int i)
00809 {set(16, "%d", i); return *this;};
00810
00811 inline String &operator=(unsigned int i)
00812 {set(16, "%u", i); return *this;};
00813
00814 inline String &operator=(long l)
00815 {set(16, "%l", l); return *this;};
00816
00817 inline String &operator=(unsigned long l)
00818 {set(16, "%ul", l); return *this;};
00819
00820 inline String &operator=(float f)
00821 {set(32, "%f", f); return *this;};
00822
00823 inline String &operator=(double d)
00824 {set(32, "%f", d); return *this;};
00825
00826 inline String &operator=(short s)
00827 {set(8, "%hd", s); return *this;};
00828
00829 inline String &operator=(unsigned short s)
00830 {set(8, "%hu", s); return *this;};
00831 #endif
00832
00833 inline String &operator=(const String &original)
00834 {copy(original); return *this;};
00835
00839 bool operator*=(const String &str) const;
00840
00844 bool operator*=(const char *str) const;
00845 };
00846
00847 class __EXPORT SString : public String, protected std::streambuf, public std::ostream
00848 {
00849 protected:
00855 int overflow(int c);
00856
00857 public:
00861 SString();
00862
00866 SString(const SString &from);
00867
00871 ~SString();
00872 };
00873
00883 class __EXPORT StringObject
00884 {
00885 public:
00889 void *operator new(size_t size) NEW_THROWS;
00890
00894 void operator delete(void *obj);
00895 };
00896
00897 #ifdef CCXX_NAMESPACES
00898 }
00899 #endif
00900
00901 #endif