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
00038
00039
00040
00046 #ifndef CCXX_STRING_H_
00047 #define CCXX_STRING_H_
00048
00049 #ifndef CCXX_MISSING_H_
00050 #include <cc++/missing.h>
00051 #endif
00052
00053 #ifndef CCXX_STRCHAR_H_
00054 #include <cc++/strchar.h>
00055 #endif
00056
00057 #ifdef CCXX_NAMESPACES
00058 namespace ost {
00059 #endif
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 slots;
00085
00086 private:
00087 friend class MemPager;
00088
00089 static MemPager *pager;
00090 static char **idx;
00091
00092 #pragma pack(1)
00093 union
00094 {
00095 struct
00096 {
00097 char *text;
00098 unsigned size;
00099 unsigned length;
00100 } bigstring;
00101 struct
00102 {
00103 char text[(sizeof(char *) + (sizeof(unsigned) * 2) + 1)];
00104 char length : 6;
00105 bool big : 1;
00106 } ministring;
00107 } content;
00108 #pragma pack()
00109
00110 protected:
00117 inline bool isBig(void) const
00118 {return content.ministring.big;};
00119
00128 const char *set(const char *str, unsigned len = 0);
00129
00136 void set(const String &str);
00137
00138 #ifdef HAVE_SNPRINTF
00139
00145 const char *set(unsigned size, const char *format, ...);
00146 #endif
00147
00154 void copy(const String &str);
00155
00159 void init(void);
00160
00168 char *getSpace(unsigned size);
00169
00177 unsigned setSize(unsigned size);
00178
00184 void setLength(unsigned len);
00185
00196 virtual int compare(const char *text, unsigned len = 0, unsigned index = 0) const;
00197
00207 unsigned search(const char *text, unsigned clen = 0, unsigned offset = 0) const;
00208
00209 public:
00210 static const unsigned npos;
00211
00212 typedef unsigned size_type;
00213
00217 String();
00218
00224 String(const String &original)
00225 {copy(original);};
00226
00232 String(const char *str);
00233
00241 String(const String &str, unsigned offset, unsigned len = npos);
00242
00243 #ifdef HAVE_SNPRINTF
00244
00251 String(unsigned count, const char *fmt, ...);
00252 #else
00253
00260 String(unsigned count, const char *str);
00261 #endif
00262
00269 String(unsigned count, char fill = ' ');
00270
00274 virtual ~String();
00275
00283 const char *getIndex(unsigned index) const;
00284
00290 char *getText(void) const;
00291
00297 const unsigned getLength(void) const;
00298
00304 const unsigned getSize(void) const;
00305
00311 bool isEmpty(void) const;
00312
00318 void resize(unsigned size);
00319
00323 void clear(void);
00324
00330 char at(int offset) const;
00331
00340 unsigned count(const String &s, unsigned offset = 0) const;
00341
00350 unsigned count(const char *s, unsigned offset = 0, unsigned len = 0) const;
00351
00359 String token(const char *delim = " \t\n\r", unsigned offset = 0);
00360
00369 unsigned find(const String &s, unsigned offset = 0, unsigned instance = 1) const;
00370
00378 unsigned rfind(const String &s, unsigned offset = 0) const;
00379
00389 unsigned find(const char *s, unsigned offset = 0, unsigned len = 0, unsigned count = 1) const;
00390
00399 unsigned rfind(const char *s, unsigned offset = 0, unsigned len = 0) const;
00400
00406 inline void trim(const char *cs)
00407 {setLength(strtrim(cs, getText(), getLength()));};
00408
00414 inline void chop(const char *cs)
00415 {setLength(strchop(cs, getText(), getLength()));};
00416
00422 void strip(const char *cs);
00423
00429 inline void chop(unsigned count)
00430 {erase(0, count);};
00431
00437 void trim(unsigned count);
00438
00445 void erase(unsigned start, unsigned len = npos);
00446
00454 void insert(unsigned start, const char *text, unsigned len = 0);
00455
00462 void insert(unsigned start, const String &str);
00463
00473 void replace(unsigned start, unsigned len, const char *text, unsigned count = 0);
00474
00483 void replace(unsigned start, unsigned len, const String &s);
00484
00494 inline unsigned find(unsigned instance, const char *text, unsigned offset = 0, unsigned len = 0) const
00495 {return find(text, offset, len, instance);};
00496
00505 inline unsigned find(unsigned instance, const String &s, unsigned offset = 0) const
00506 {return find(s, offset, instance);};
00507
00516 inline String substr(unsigned start, unsigned len) const
00517 {return String(*this, start, len);};
00518
00526 inline const char *index(unsigned idx) const
00527 {return getIndex(idx);};
00528
00533 inline void compact(void)
00534 {resize(getLength() + 1);};
00535
00541 inline char *c_str(void) const
00542 {return getText();};
00543
00549 inline operator char *() const
00550 {return getText();};
00551
00557 inline bool operator!(void) const
00558 {return isEmpty();};
00559
00565 inline char *text(void) const
00566 {return getText();};
00567
00573 inline char *data(void) const
00574 {return getText();};
00575
00581 inline unsigned length(void) const
00582 {return strlen(getText());};
00583
00589 inline unsigned size(void) const
00590 {return getLength();};
00591
00597 inline unsigned capacity(void) const
00598 {return getSize();};
00599
00603 bool empty(void) const
00604 {return isEmpty();};
00605
00612 void append(const char *str, unsigned count = 0);
00613
00614 #ifdef HAVE_SNPRINTF
00615
00621 void append(unsigned size, const char *format, ...);
00622 #endif
00623
00631 void append(const char *str, unsigned offset, unsigned count);
00632
00638 void add(char c);
00639
00645 void append(const String &str);
00646
00652 inline const char operator[](unsigned index) const
00653 {return at(index);};
00654
00658 inline void operator =(const String& str)
00659 {set(str);};
00660
00664 inline const char *operator =(const char *str)
00665 {return set(str);};
00666
00670 friend __EXPORT String operator+(const String &s1, const String &s2);
00671
00672 friend __EXPORT String operator+(const String &s1, const char *s2);
00673
00674 friend __EXPORT String operator+(const char *s1, const String &s2);
00675
00676 friend __EXPORT String operator+(const String &s1, const char c2);
00677
00678 friend __EXPORT String operator+(const char c1, const String &s2);
00679
00683 inline String &operator+=(const String &str)
00684 {append(str); return *this;};
00685
00689 inline String &operator+=(char c)
00690 {add(c); return *this;};
00691
00695 inline String &operator+=(const char *str)
00696 {append(str); return *this;};
00697
00708 friend __EXPORT std::istream &getline(std::istream &is, String &str, char delim = '\n', unsigned size = 0);
00709
00714 friend __EXPORT std::ostream &operator<<(std::ostream &os, const String &str);
00715
00719 inline friend std::istream &operator>>(std::istream &is, String &str)
00720 {return getline(is, str);};
00721
00722 #ifdef HAVE_SNPRINTF
00723
00731 friend __EXPORT int strprintf(String &str, unsigned size, const char *fmt, ...);
00732 #endif
00733
00734 bool operator<(const String &str);
00735 bool operator<(const char *str);
00736 bool operator>(const String &str);
00737 bool operator>(const char *str);
00738 bool operator<=(const String &str);
00739 bool operator<=(const char *str);
00740 bool operator>=(const String &str);
00741 bool operator>=(const char *str);
00742 bool operator==(const String &str);
00743 bool operator==(const char *str);
00744 bool operator!=(const String &str);
00745 bool operator!=(const char *str);
00746
00747 #ifdef HAVE_SNPRINTF
00748
00752 inline String &operator+=(int i)
00753 {append(16, "%d", i); return *this;};
00754
00755 inline String &operator+=(unsigned int i)
00756 {append(16, "%u", i); return *this;};
00757
00758 inline String &operator+=(long l)
00759 {append(16, "%l", l); return *this;};
00760
00761 inline String &operator+=(unsigned long l)
00762 {append(16, "%ul", l); return *this;};
00763
00764 inline String &operator+=(float f)
00765 {append(32, "%f", f); return *this;};
00766
00767 inline String &operator+=(double d)
00768 {append(32, "%f", d); return *this;};
00769
00770 inline String &operator+=(short s)
00771 {append(8, "%hd", s); return *this;};
00772
00773 inline String &operator+=(unsigned short s)
00774 {append(8, "%hu", s); return *this;};
00775
00776
00780 inline String &operator=(int i)
00781 {set(16, "%d", i); return *this;};
00782
00783 inline String &operator=(unsigned int i)
00784 {set(16, "%u", i); return *this;};
00785
00786 inline String &operator=(long l)
00787 {set(16, "%l", l); return *this;};
00788
00789 inline String &operator=(unsigned long l)
00790 {set(16, "%ul", l); return *this;};
00791
00792 inline String &operator=(float f)
00793 {set(32, "%f", f); return *this;};
00794
00795 inline String &operator=(double d)
00796 {set(32, "%f", d); return *this;};
00797
00798 inline String &operator=(short s)
00799 {set(8, "%hd", s); return *this;};
00800
00801 inline String &operator=(unsigned short s)
00802 {set(8, "%hu", s); return *this;};
00803
00804 #endif
00805
00809 bool operator*=(const String &str);
00810
00814 bool operator*=(const char *str);
00815 };
00816
00817 class __EXPORT SString : public String, protected std::streambuf, public std::ostream
00818 {
00819 protected:
00825 int overflow(int c);
00826
00827 public:
00831 SString();
00832
00836 SString(const SString &from);
00837
00841 ~SString();
00842 };
00843
00844 #ifdef CCXX_NAMESPACES
00845 };
00846 #endif
00847
00848 #endif