Bayonne2 / Common C++ 2 Framework
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
string.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 // As a special exception, you may use this file as part of a free software
19 // library without restriction. Specifically, if other files instantiate
20 // templates or use macros or inline functions from this file, or you compile
21 // this file and link it with other files to produce an executable, this
22 // file does not by itself cause the resulting executable to be covered by
23 // the GNU General Public License. This exception does not however
24 // invalidate any other reasons why the executable file might be covered by
25 // the GNU General Public License.
26 //
27 // This exception applies only to the code released under the name GNU
28 // Common C++. If you copy code from other releases into a copy of GNU
29 // Common C++, as the General Public License permits, the exception does
30 // not apply to the code that you add in this way. To avoid misleading
31 // anyone as to the status of such modified files, you must delete
32 // this exception notice from them.
33 //
34 // If you write modifications of your own for GNU Common C++, it is your choice
35 // whether to permit this exception to apply to your modifications.
36 // If you do not wish that, delete this exception notice.
37 //
38 
44 #ifndef CCXX_STRING_H_
45 #define CCXX_STRING_H_
46 
47 #ifndef CCXX_MISSING_H_
48 #include <cc++/missing.h>
49 #endif
50 
51 #ifndef CCXX_STRCHAR_H_
52 #include <cc++/strchar.h>
53 #endif
54 
55 #ifdef CCXX_NAMESPACES
56 namespace ost {
57 #endif
58 
59 class MemPager;
60 
78 {
79 protected:
80  static const unsigned minsize;
81  static const unsigned slotsize;
82  static const unsigned pagesize;
83  static const unsigned slotlimit;
84  static const unsigned slotcount;
85 
86  friend class StringObject;
87 
88 private:
89  friend class MemPager;
90 
91  static MemPager *pager;
92  static char **idx;
93 
94 #ifdef CCXX_PACKED
95 #pragma pack(1)
96 #endif
97 
98  union {
99  struct {
100  char *text;
101  size_t size;
102  size_t length;
103  } bigstring;
104  struct {
105  char text[(sizeof(char *) + (sizeof(size_t) * 2) + 1)];
106  char length : 6;
107  bool big : 1;
108  } ministring;
109  } content;
110 
111 #ifdef CCXX_PACKED
112 #pragma pack()
113 #endif
114 
115 protected:
122  inline bool isBig(void) const
123  {return content.ministring.big;};
124 
133  const char *set(const char *str, size_t len = 0);
134 
141  void set(const String &str);
142 
143 #ifdef HAVE_SNPRINTF
144 
151  const char *set(size_t size, const char *format, ...);
152 #endif
153 
160  void copy(const String &str);
161 
165  void init(void);
166 
174  static char *getSpace(size_t size);
175 
183  size_t setSize(size_t size);
184 
190  void setLength(size_t len);
191 
202  virtual int compare(const char *text, size_t len = 0, size_t index = 0) const;
203 
213  size_t search(const char *text, size_t clen = 0, size_t offset = 0) const;
214 
215 public:
216  static const size_t npos;
217 
218  typedef size_t size_type;
219 
223  String();
224 
230  String(const String &original);
231 
237  String(const char *str);
238 
244  String(std::string string);
245 
253  String(const String &str, size_t offset, size_t len = npos);
254 
255 #ifdef HAVE_SNPRINTF
256 
262  String(size_t size, const char *format, ...);
263 #else
264 
271  String(size_t count, const char *str);
272 #endif
273 
280  String(size_t count, const char fill = ' ');
281 
285  virtual ~String();
286 
294  const char *getIndex(size_t index) const;
295 
301  char *getText(void) const;
302 
308  long getValue(long defvalue = 0l) const;
309 
315  bool getBool(bool defbool = false) const;
316 
322  const size_t getLength(void) const;
323 
329  const size_t getSize(void) const;
330 
336  bool isEmpty(void) const;
337 
343  void resize(size_t size);
344 
348  void clear(void);
349 
355  char at(ssize_t offset) const;
356 
365  unsigned count(const String &s, size_t offset = 0) const;
366 
376  unsigned count(const char *s, size_t offset = 0, size_t len = 0) const;
377 
385  String token(const char *delim = " \t\n\r", size_t offset = 0);
386 
395  size_t find(const String &s, size_t offset = 0, unsigned instance = 1) const;
396 
404  size_t rfind(const String &s, size_t offset = 0) const;
405 
415  size_t find(const char *s, size_t offset = 0, size_t len = 0, unsigned count = 1) const;
416 
425  size_t rfind(const char *s, size_t offset = 0, size_t len = 0) const;
426 
432  inline void trim(const char *cs)
433  {setLength(strtrim(cs, getText(), getLength()));};
434 
440  inline void chop(const char *cs)
441  {setLength(strchop(cs, getText(), getLength()));};
442 
448  void strip(const char *cs);
449 
455  inline void chop(size_t chars)
456  {erase(0, chars);};
457 
463  void trim(size_t count);
464 
471  void erase(size_t start, size_t len = npos);
472 
480  void insert(size_t start, const char *text, size_t len = 0);
481 
488  void insert(size_t start, const String &str);
489 
499  void replace(size_t start, size_t len, const char *text, size_t count = 0);
500 
509  void replace(size_t start, size_t len, const String &string);
510 
520  inline size_t find(unsigned instance, const char *text, size_t offset = 0, size_t len = 0) const
521  {return find(text, offset, len, instance);};
522 
531  inline size_t find(unsigned instance, const String &string, size_t offset = 0) const
532  {return find(string, offset, instance);};
533 
542  inline String substr(size_t start, size_t len) const
543  {return String(*this, start, len);};
544 
552  inline const char *(index)(size_t ind) const
553  {return getIndex(ind);};
554 
559  inline void compact(void)
560  {resize(getLength() + 1);};
561 
567  inline char *c_str(void) const
568  {return getText();};
569 
575  inline operator char *() const
576  {return getText();};
577 
583  inline bool operator!(void) const
584  {return isEmpty();};
585 
591  inline char *text(void) const
592  {return getText();};
593 
599  inline char *data(void) const
600  {return getText();};
601 
607  inline size_t length(void) const
608  {return strlen(getText());};
609 
615  inline size_t size(void) const
616  {return getLength();};
617 
623  inline size_t capacity(void) const
624  {return getSize();};
625 
629  bool empty(void) const
630  {return isEmpty();};
631 
638  void append(const char *str, size_t count = 0);
639 
640 #ifdef HAVE_SNPRINTF
641 
647  void append(size_t size, const char *format, ...);
648 #endif
649 
657  void append(const char *str, size_t offset, size_t count);
658 
664  void add(char c);
665 
671  void append(const String &str);
672 
678  inline const char operator[](unsigned ind) const
679  {return at(ind);};
680 
684  inline const char *operator =(const char *str)
685  {return set(str);};
686 
690  friend __EXPORT String operator+(const String &s1, const String &s2);
691 
692  friend __EXPORT String operator+(const String &s1, const char *s2);
693 
694  friend __EXPORT String operator+(const char *s1, const String &s2);
695 
696  friend __EXPORT String operator+(const String &s1, const char c2);
697 
698  friend __EXPORT String operator+(const char c1, const String &s2);
699 
703  inline String &operator+=(const String &str)
704  {append(str); return *this;};
705 
709  inline String &operator+=(char c)
710  {add(c); return *this;};
711 
715  inline String &operator+=(const char *str)
716  {append(str); return *this;};
717 
721  inline String &operator+=(const std::string &str)
722  {append(str.c_str()); return *this;};
723 
734  friend __EXPORT std::istream &getline(std::istream &is, String &str, char delim = '\n', size_t size = 0);
735 
740  friend __EXPORT std::ostream &operator<<(std::ostream &os, const String &str);
741 
745  inline friend std::istream &operator>>(std::istream &is, String &str)
746  {return getline(is, str);};
747 
748 #ifdef HAVE_SNPRINTF
749 
757  friend __EXPORT int strprintf(String &str, size_t size, const char *format, ...);
758 #endif
759 
760  bool operator<(const String &str) const;
761  bool operator<(const char *str) const;
762  bool operator>(const String &str) const;
763  bool operator>(const char *str) const;
764  bool operator<=(const String &str) const;
765  bool operator<=(const char *str) const;
766  bool operator>=(const String &str) const;
767  bool operator>=(const char *str) const;
768  bool operator==(const String &str) const;
769  bool operator==(const char *str) const;
770  bool operator!=(const String &str) const;
771  bool operator!=(const char *str) const;
772 
773 #ifdef HAVE_SNPRINTF
774 
778  inline String &operator+=(int i)
779  {append(16, "%d", i); return *this;};
780 
781  inline String &operator+=(unsigned int i)
782  {append(16, "%u", i); return *this;};
783 
784  inline String &operator+=(long l)
785  {append(16, "%l", l); return *this;};
786 
787  inline String &operator+=(unsigned long l)
788  {append(16, "%ul", l); return *this;};
789 
790  inline String &operator+=(float f)
791  {append(32, "%f", f); return *this;};
792 
793  inline String &operator+=(double d)
794  {append(32, "%f", d); return *this;};
795 
796  inline String &operator+=(short s)
797  {append(8, "%hd", s); return *this;};
798 
799  inline String &operator+=(unsigned short s)
800  {append(8, "%hu", s); return *this;};
801 
802 
806  inline String &operator=(int i)
807  {set(16, "%d", i); return *this;};
808 
809  inline String &operator=(unsigned int i)
810  {set(16, "%u", i); return *this;};
811 
812  inline String &operator=(long l)
813  {set(16, "%l", l); return *this;};
814 
815  inline String &operator=(unsigned long l)
816  {set(16, "%ul", l); return *this;};
817 
818  inline String &operator=(float f)
819  {set(32, "%f", f); return *this;};
820 
821  inline String &operator=(double d)
822  {set(32, "%f", d); return *this;};
823 
824  inline String &operator=(short s)
825  {set(8, "%hd", s); return *this;};
826 
827  inline String &operator=(unsigned short s)
828  {set(8, "%hu", s); return *this;};
829 #endif
830 
831  inline String &operator=(const String &original)
832  {copy(original); return *this;};
833 
837  bool operator*=(const String &str) const;
838 
842  bool operator*=(const char *str) const;
843 };
844 
845 class __EXPORT SString : public String, protected std::streambuf, public std::ostream
846 {
847 protected:
853  int overflow(int c);
854 
855 public:
859  SString();
860 
864  SString(const SString &from);
865 
869  ~SString();
870 };
871 
882 {
883 public:
887  void *operator new(size_t size) NEW_THROWS;
888 
892  void operator delete(void *obj);
893 };
894 
895 #ifdef CCXX_NAMESPACES
896 }
897 #endif
898 
899 #endif
String & operator+=(const std::string &str)
Append operator.
Definition: string.h:721
size_t size(void) const
Get actual length of string data.
Definition: string.h:615
The memory pager is used to allocate cumulative memory pages for storing object specific "persistant"...
Definition: misc.h:91
const char *() index(size_t ind) const
Return an indexed string based on the index, such as from a find.
Definition: string.h:552
__EXPORT char * strip(const char *cs, char *str, size_t len=0)
void chop(size_t chars)
Chop n leading characters from a string.
Definition: string.h:455
String & operator=(const String &original)
Definition: string.h:831
__EXPORT char * rfind(const char *cs, char *str, size_t len=0)
__EXPORT size_t strtrim(const char *cs, char *str, size_t len=0)
String & operator+=(const String &str)
Append operator.
Definition: string.h:703
static char ** idx
Definition: string.h:92
size_t length(void) const
Get length as if null terminated string.
Definition: string.h:607
substitute functions which may be missing in target platform libc.
void compact(void)
Reduce the size of the string allocation to the minimum needed based on the current effective length...
Definition: string.h:559
bool empty(void) const
Return true if string is empty.
Definition: string.h:629
__EXPORT char * find(const char *cs, char *str, size_t len=0)
This is a generic and portable string class.
Definition: string.h:77
String & operator+=(const char *str)
Append operator.
Definition: string.h:715
char * text(void) const
Alternate get text method.
Definition: string.h:591
Common and portable character string related functions.
__EXPORT std::ostream & operator<<(std::ostream &os, const IPV4Address &ia)
friend std::istream & operator>>(std::istream &is, String &str)
Stream input into our variable.
Definition: string.h:745
static const size_t npos
Definition: string.h:216
const char operator[](unsigned ind) const
Extract a character by array indexing.
Definition: string.h:678
size_t length
Definition: string.h:102
bool isBig(void) const
Determine if string is allocated in local variable or an external reference.
Definition: string.h:122
char * c_str(void) const
Old ANSI C++ compatible string pointer extraction.
Definition: string.h:567
size_t find(unsigned instance, const char *text, size_t offset=0, size_t len=0) const
A more convenient version of find for nth occurences, by putting the instance first.
Definition: string.h:520
String substr(size_t start, size_t len) const
Return a new string that contains a specific substring of the current string.
Definition: string.h:542
size_t size_type
Definition: string.h:218
static const unsigned slotcount
Definition: string.h:84
void trim(const char *cs)
Trim trailing characters from a string.
Definition: string.h:432
static const unsigned slotsize
Definition: string.h:81
void chop(const char *cs)
Chop leading characters from a string.
Definition: string.h:440
#define __EXPORT
Definition: audio2.h:51
static const unsigned minsize
Definition: string.h:80
size_t find(unsigned instance, const String &string, size_t offset=0) const
A more convenient version of find for nth occurences, by putting the instance first.
Definition: string.h:531
size_t capacity(void) const
Get space allocated to hold current string.
Definition: string.h:623
static const unsigned slotlimit
Definition: string.h:83
The StringObject class is used to derive subclasses that use the String managed memory pool for all s...
Definition: string.h:881
__EXPORT size_t strchop(const char *cs, char *str, size_t len=0)
static MemPager * pager
Definition: string.h:91
bool operator!(void) const
Logical test for string empty.
Definition: string.h:583
char * data(void) const
Alternate get text method.
Definition: string.h:599
static const unsigned pagesize
Definition: string.h:82
String & operator+=(char c)
Append operator.
Definition: string.h:709
size_t size
Definition: string.h:101
char * text
Definition: string.h:100