GNU CommonC++
string.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
43 #ifndef CCXX_STRING_H_
44 #define CCXX_STRING_H_
45 
46 #ifndef CCXX_MISSING_H_
47 #include <cc++/missing.h>
48 #endif
49 
50 #ifndef CCXX_STRCHAR_H_
51 #include <cc++/strchar.h>
52 #endif
53 
54 #ifdef CCXX_NAMESPACES
55 namespace ost {
56 #endif
57 
58 class MemPager;
59 
77 {
78 protected:
79  static const unsigned minsize;
80  static const unsigned slotsize;
81  static const unsigned pagesize;
82  static const unsigned slotlimit;
83  static const unsigned slotcount;
84 
85  friend class StringObject;
86 
87 private:
88  friend class MemPager;
89 
90  static MemPager *pager;
91  static char **idx;
92 
93 #ifdef CCXX_PACKED
94 #pragma pack(1)
95 #endif
96 
97  union {
98  struct {
99  char *text;
100  size_t size;
101  size_t length;
102  } bigstring;
103  struct {
104  char text[(sizeof(char *) + (sizeof(size_t) * 2) + 1)];
105  char length : 6;
106  bool big : 1;
107  } ministring;
108  } content;
109 
110 #ifdef CCXX_PACKED
111 #pragma pack()
112 #endif
113 
114 protected:
121  inline bool isBig(void) const
122  {return content.ministring.big;};
123 
132  const char *set(const char *str, size_t len = 0);
133 
140  void set(const String &str);
141 
142 #ifdef HAVE_SNPRINTF
143 
150  const char *set(size_t size, const char *format, ...);
151 #endif
152 
159  void copy(const String &str);
160 
164  void init(void);
165 
173  static char *getSpace(size_t size);
174 
182  size_t setSize(size_t size);
183 
189  void setLength(size_t len);
190 
201  virtual int compare(const char *text, size_t len = 0, size_t index = 0) const;
202 
212  size_t search(const char *text, size_t clen = 0, size_t offset = 0) const;
213 
214 public:
215  static const size_t npos;
216 
217  typedef size_t size_type;
218 
222  String();
223 
229  String(const String &original);
230 
236  String(const char *str);
237 
243  String(std::string string);
244 
252  String(const String &str, size_t offset, size_t len = npos);
253 
254 #ifdef HAVE_SNPRINTF
255 
261  String(size_t size, const char *format, ...);
262 #else
263 
270  String(size_t count, const char *str);
271 #endif
272 
279  String(size_t count, const char fill = ' ');
280 
284  virtual ~String();
285 
293  const char *getIndex(size_t index) const;
294 
300  char *getText(void) const;
301 
307  long getValue(long defvalue = 0l) const;
308 
314  bool getBool(bool defbool = false) const;
315 
321  const size_t getLength(void) const;
322 
328  const size_t getSize(void) const;
329 
335  bool isEmpty(void) const;
336 
342  void resize(size_t size);
343 
347  void clear(void);
348 
354  char at(ssize_t offset) const;
355 
364  unsigned count(const String &s, size_t offset = 0) const;
365 
375  unsigned count(const char *s, size_t offset = 0, size_t len = 0) const;
376 
384  String token(const char *delim = " \t\n\r", size_t offset = 0);
385 
394  size_t find(const String &s, size_t offset = 0, unsigned instance = 1) const;
395 
403  size_t rfind(const String &s, size_t offset = 0) const;
404 
414  size_t find(const char *s, size_t offset = 0, size_t len = 0, unsigned count = 1) const;
415 
424  size_t rfind(const char *s, size_t offset = 0, size_t len = 0) const;
425 
431  inline void trim(const char *cs)
432  {setLength(strtrim(cs, getText(), getLength()));};
433 
439  inline void chop(const char *cs)
440  {setLength(strchop(cs, getText(), getLength()));};
441 
447  void strip(const char *cs);
448 
454  inline void chop(size_t chars)
455  {erase(0, chars);};
456 
462  void trim(size_t count);
463 
470  void erase(size_t start, size_t len = npos);
471 
479  void insert(size_t start, const char *text, size_t len = 0);
480 
487  void insert(size_t start, const String &str);
488 
498  void replace(size_t start, size_t len, const char *text, size_t count = 0);
499 
508  void replace(size_t start, size_t len, const String &string);
509 
519  inline size_t find(unsigned instance, const char *text, size_t offset = 0, size_t len = 0) const
520  {return find(text, offset, len, instance);};
521 
530  inline size_t find(unsigned instance, const String &string, size_t offset = 0) const
531  {return find(string, offset, instance);};
532 
541  inline String substr(size_t start, size_t len) const
542  {return String(*this, start, len);};
543 
551  inline const char *(index)(size_t ind) const
552  {return getIndex(ind);};
553 
558  inline void compact(void)
559  {resize(getLength() + 1);};
560 
566  inline char *c_str(void) const
567  {return getText();};
568 
574  inline operator char *() const
575  {return getText();};
576 
582  inline bool operator!(void) const
583  {return isEmpty();};
584 
590  inline char *text(void) const
591  {return getText();};
592 
598  inline char *data(void) const
599  {return getText();};
600 
606  inline size_t length(void) const
607  {return strlen(getText());};
608 
614  inline size_t size(void) const
615  {return getLength();};
616 
622  inline size_t capacity(void) const
623  {return getSize();};
624 
628  bool empty(void) const
629  {return isEmpty();};
630 
637  void append(const char *str, size_t count = 0);
638 
639 #ifdef HAVE_SNPRINTF
640 
646  void append(size_t size, const char *format, ...);
647 #endif
648 
656  void append(const char *str, size_t offset, size_t count);
657 
663  void add(char c);
664 
670  void append(const String &str);
671 
677  inline const char operator[](unsigned ind) const
678  {return at(ind);};
679 
683  inline const char *operator =(const char *str)
684  {return set(str);};
685 
689  friend __EXPORT String operator+(const String &s1, const String &s2);
690 
691  friend __EXPORT String operator+(const String &s1, const char *s2);
692 
693  friend __EXPORT String operator+(const char *s1, const String &s2);
694 
695  friend __EXPORT String operator+(const String &s1, const char c2);
696 
697  friend __EXPORT String operator+(const char c1, const String &s2);
698 
702  inline String &operator+=(const String &str)
703  {append(str); return *this;};
704 
708  inline String &operator+=(char c)
709  {add(c); return *this;};
710 
714  inline String &operator+=(const char *str)
715  {append(str); return *this;};
716 
720  inline String &operator+=(const std::string &str)
721  {append(str.c_str()); return *this;};
722 
733  friend __EXPORT std::istream &getline(std::istream &is, String &str, char delim = '\n', size_t size = 0);
734 
739  friend __EXPORT std::ostream &operator<<(std::ostream &os, const String &str);
740 
744  inline friend std::istream &operator>>(std::istream &is, String &str)
745  {return getline(is, str);};
746 
747 #ifdef HAVE_SNPRINTF
748 
756  friend __EXPORT int strprintf(String &str, size_t size, const char *format, ...);
757 #endif
758 
759  bool operator<(const String &str) const;
760  bool operator<(const char *str) const;
761  bool operator>(const String &str) const;
762  bool operator>(const char *str) const;
763  bool operator<=(const String &str) const;
764  bool operator<=(const char *str) const;
765  bool operator>=(const String &str) const;
766  bool operator>=(const char *str) const;
767  bool operator==(const String &str) const;
768  bool operator==(const char *str) const;
769  bool operator!=(const String &str) const;
770  bool operator!=(const char *str) const;
771 
772 #ifdef HAVE_SNPRINTF
773 
777  inline String &operator+=(int i)
778  {append(16, "%d", i); return *this;};
779 
780  inline String &operator+=(unsigned int i)
781  {append(16, "%u", i); return *this;};
782 
783  inline String &operator+=(long l)
784  {append(16, "%l", l); return *this;};
785 
786  inline String &operator+=(unsigned long l)
787  {append(16, "%ul", l); return *this;};
788 
789  inline String &operator+=(float f)
790  {append(32, "%f", f); return *this;};
791 
792  inline String &operator+=(double d)
793  {append(32, "%f", d); return *this;};
794 
795  inline String &operator+=(short s)
796  {append(8, "%hd", s); return *this;};
797 
798  inline String &operator+=(unsigned short s)
799  {append(8, "%hu", s); return *this;};
800 
801 
805  inline String &operator=(int i)
806  {set(16, "%d", i); return *this;};
807 
808  inline String &operator=(unsigned int i)
809  {set(16, "%u", i); return *this;};
810 
811  inline String &operator=(long l)
812  {set(16, "%l", l); return *this;};
813 
814  inline String &operator=(unsigned long l)
815  {set(16, "%ul", l); return *this;};
816 
817  inline String &operator=(float f)
818  {set(32, "%f", f); return *this;};
819 
820  inline String &operator=(double d)
821  {set(32, "%f", d); return *this;};
822 
823  inline String &operator=(short s)
824  {set(8, "%hd", s); return *this;};
825 
826  inline String &operator=(unsigned short s)
827  {set(8, "%hu", s); return *this;};
828 #endif
829 
830  inline String &operator=(const String &original)
831  {copy(original); return *this;};
832 
836  bool operator*=(const String &str) const;
837 
841  bool operator*=(const char *str) const;
842 };
843 
844 class __EXPORT SString : public String, protected std::streambuf, public std::ostream
845 {
846 protected:
852  int overflow(int c);
853 
854 public:
858  SString();
859 
863  SString(const SString &from);
864 
868  ~SString();
869 };
870 
881 {
882 public:
886  void *operator new(size_t size) NEW_THROWS;
887 
891  void operator delete(void *obj);
892 };
893 
894 #ifdef CCXX_NAMESPACES
895 }
896 #endif
897 
898 #endif