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 size_t size;
00099 size_t length;
00100 } bigstring;
00101 struct
00102 {
00103 char text[(sizeof(char *) + (sizeof(size_t) * 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, size_t len = 0);
00129
00136 void set(const String &str);
00137
00138 #ifdef HAVE_SNPRINTF
00139
00145 const char *set(size_t size, const char *format, ...);
00146 #endif
00147
00154 void copy(const String &str);
00155
00159 void init(void);
00160
00168 char *getSpace(size_t size);
00169
00177 size_t setSize(size_t size);
00178
00184 void setLength(size_t len);
00185
00196 virtual int compare(const char *text, size_t len = 0, size_t index = 0) const;
00197
00207 size_t search(const char *text, size_t clen = 0, size_t offset = 0) const;
00208
00209 public:
00210 static const size_t npos;
00211
00212 typedef size_t size_type;
00213
00217 String();
00218
00224 String(const String &original);
00225
00231 String(const char *str);
00232
00238 inline String(std::string str)
00239 {String(str.c_str());};
00240
00248 String(const String &str, size_t offset, size_t len = npos);
00249
00250 #ifdef HAVE_SNPRINTF
00251
00258 String(size_t count, const char *fmt, ...);
00259 #else
00260
00267 String(size_t count, const char *str);
00268 #endif
00269
00276 String(size_t count, char fill = ' ');
00277
00281 virtual ~String();
00282
00290 const char *getIndex(size_t index) const;
00291
00297 char *getText(void) const;
00298
00304 long getValue(long defvalue = 0l) const;
00305
00311 bool getBool(bool defbool = false) const;
00312
00318 const size_t getLength(void) const;
00319
00325 const size_t getSize(void) const;
00326
00332 bool isEmpty(void) const;
00333
00339 void resize(size_t size);
00340
00344 void clear(void);
00345
00351 char at(ssize_t offset) const;
00352
00361 unsigned count(const String &s, size_t offset = 0) const;
00362
00372 unsigned count(const char *s, size_t offset = 0, size_t len = 0) const;
00373
00381 String token(const char *delim = " \t\n\r", size_t offset = 0);
00382
00391 size_t find(const String &s, size_t offset = 0, unsigned instance = 1) const;
00392
00400 size_t rfind(const String &s, size_t offset = 0) const;
00401
00411 size_t find(const char *s, size_t offset = 0, size_t len = 0, unsigned count = 1) const;
00412
00421 size_t rfind(const char *s, size_t offset = 0, size_t len = 0) const;
00422
00428 inline void trim(const char *cs)
00429 {setLength(strtrim(cs, getText(), getLength()));};
00430
00436 inline void chop(const char *cs)
00437 {setLength(strchop(cs, getText(), getLength()));};
00438
00444 void strip(const char *cs);
00445
00451 inline void chop(size_t chars)
00452 {erase(0, chars);};
00453
00459 void trim(size_t count);
00460
00467 void erase(size_t start, size_t len = npos);
00468
00476 void insert(size_t start, const char *text, size_t len = 0);
00477
00484 void insert(size_t start, const String &str);
00485
00495 void replace(size_t start, size_t len, const char *text, size_t count = 0);
00496
00505 void replace(size_t start, size_t len, const String &s);
00506
00516 inline size_t find(unsigned instance, const char *cp, size_t offset = 0, size_t len = 0) const
00517 {return find(cp, offset, len, instance);};
00518
00527 inline size_t find(unsigned instance, const String &s, size_t offset = 0) const
00528 {return find(s, offset, instance);};
00529
00538 inline String substr(size_t start, size_t len) const
00539 {return String(*this, start, len);};
00540
00548 inline const char *(index)(size_t ind) const
00549 {return getIndex(ind);};
00550
00555 inline void compact(void)
00556 {resize(getLength() + 1);};
00557
00563 inline char *c_str(void) const
00564 {return getText();};
00565
00571 inline operator char *() const
00572 {return getText();};
00573
00579 inline bool operator!(void) const
00580 {return isEmpty();};
00581
00587 inline char *text(void) const
00588 {return getText();};
00589
00595 inline char *data(void) const
00596 {return getText();};
00597
00603 inline size_t length(void) const
00604 {return strlen(getText());};
00605
00611 inline size_t size(void) const
00612 {return getLength();};
00613
00619 inline size_t capacity(void) const
00620 {return getSize();};
00621
00625 bool empty(void) const
00626 {return isEmpty();};
00627
00634 void append(const char *str, size_t count = 0);
00635
00636 #ifdef HAVE_SNPRINTF
00637
00643 void append(size_t size, const char *format, ...);
00644 #endif
00645
00653 void append(const char *str, size_t offset, size_t count);
00654
00660 void add(char c);
00661
00667 void append(const String &str);
00668
00674 inline const char operator[](unsigned ind) const
00675 {return at(ind);};
00676
00680 inline const char *operator =(const char *str)
00681 {return set(str);};
00682
00686 friend __EXPORT String operator+(const String &s1, const String &s2);
00687
00688 friend __EXPORT String operator+(const String &s1, const char *s2);
00689
00690 friend __EXPORT String operator+(const char *s1, const String &s2);
00691
00692 friend __EXPORT String operator+(const String &s1, const char c2);
00693
00694 friend __EXPORT String operator+(const char c1, const String &s2);
00695
00699 inline String &operator+=(const String &str)
00700 {append(str); return *this;};
00701
00705 inline String &operator+=(char c)
00706 {add(c); return *this;};
00707
00711 inline String &operator+=(const char *str)
00712 {append(str); return *this;};
00713
00717 inline String &operator+=(const std::string &str)
00718 {append(str.c_str()); return *this;};
00719
00730 friend __EXPORT std::istream &getline(std::istream &is, String &str, char delim = '\n', size_t size = 0);
00731
00736 friend __EXPORT std::ostream &operator<<(std::ostream &os, const String &str);
00737
00741 inline friend std::istream &operator>>(std::istream &is, String &str)
00742 {return getline(is, str);};
00743
00744 #ifdef HAVE_SNPRINTF
00745
00753 friend __EXPORT int strprintf(String &str, size_t size, const char *fmt, ...);
00754 #endif
00755
00756 bool operator<(const String &str) const;
00757 bool operator<(const char *str) const;
00758 bool operator>(const String &str) const;
00759 bool operator>(const char *str) const;
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
00769 #ifdef HAVE_SNPRINTF
00770
00774 inline String &operator+=(int i)
00775 {append(16, "%d", i); return *this;};
00776
00777 inline String &operator+=(unsigned int i)
00778 {append(16, "%u", i); return *this;};
00779
00780 inline String &operator+=(long l)
00781 {append(16, "%l", l); return *this;};
00782
00783 inline String &operator+=(unsigned long l)
00784 {append(16, "%ul", l); return *this;};
00785
00786 inline String &operator+=(float f)
00787 {append(32, "%f", f); return *this;};
00788
00789 inline String &operator+=(double d)
00790 {append(32, "%f", d); return *this;};
00791
00792 inline String &operator+=(short s)
00793 {append(8, "%hd", s); return *this;};
00794
00795 inline String &operator+=(unsigned short s)
00796 {append(8, "%hu", s); return *this;};
00797
00798
00802 inline String &operator=(int i)
00803 {set(16, "%d", i); return *this;};
00804
00805 inline String &operator=(unsigned int i)
00806 {set(16, "%u", i); return *this;};
00807
00808 inline String &operator=(long l)
00809 {set(16, "%l", l); return *this;};
00810
00811 inline String &operator=(unsigned long l)
00812 {set(16, "%ul", l); return *this;};
00813
00814 inline String &operator=(float f)
00815 {set(32, "%f", f); return *this;};
00816
00817 inline String &operator=(double d)
00818 {set(32, "%f", d); return *this;};
00819
00820 inline String &operator=(short s)
00821 {set(8, "%hd", s); return *this;};
00822
00823 inline String &operator=(unsigned short s)
00824 {set(8, "%hu", s); return *this;};
00825 #endif
00826
00827 inline String &operator=(const String &original)
00828 {copy(original); return *this;};
00829
00833 bool operator*=(const String &str) const;
00834
00838 bool operator*=(const char *str) const;
00839 };
00840
00841 class __EXPORT SString : public String, protected std::streambuf, public std::ostream
00842 {
00843 protected:
00849 int overflow(int c);
00850
00851 public:
00855 SString();
00856
00860 SString(const SString &from);
00861
00865 ~SString();
00866 };
00867
00868 #ifdef CCXX_NAMESPACES
00869 }
00870 #endif
00871
00872 #endif