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 #pragma pack(1)
00094 union
00095 {
00096 struct
00097 {
00098 char *text;
00099 size_t size;
00100 size_t length;
00101 } bigstring;
00102 struct
00103 {
00104 char text[(sizeof(char *) + (sizeof(size_t) * 2) + 1)];
00105 char length : 6;
00106 bool big : 1;
00107 } ministring;
00108 } content;
00109 #pragma pack()
00110
00111 protected:
00118 inline bool isBig(void) const
00119 {return content.ministring.big;};
00120
00129 const char *set(const char *str, size_t len = 0);
00130
00137 void set(const String &str);
00138
00139 #ifdef HAVE_SNPRINTF
00140
00146 const char *set(size_t size, const char *format, ...);
00147 #endif
00148
00155 void copy(const String &str);
00156
00160 void init(void);
00161
00169 static char *getSpace(size_t size);
00170
00178 size_t setSize(size_t size);
00179
00185 void setLength(size_t len);
00186
00197 virtual int compare(const char *text, size_t len = 0, size_t index = 0) const;
00198
00208 size_t search(const char *text, size_t clen = 0, size_t offset = 0) const;
00209
00210 public:
00211 static const size_t npos;
00212
00213 typedef size_t size_type;
00214
00218 String();
00219
00225 String(const String &original);
00226
00232 String(const char *str);
00233
00239 String(std::string 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, const 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
00877 class __EXPORT StringObject
00878 {
00879 public:
00883 void *operator new(size_t size) NEW_THROWS;
00884
00888 void operator delete(void *obj);
00889 };
00890
00891 #ifdef CCXX_NAMESPACES
00892 }
00893 #endif
00894
00895 #endif