GNU CommonC++
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 
61 class String;
62 __EXPORT std::istream &getline(std::istream &is, String &str, char delim = '\n', size_t size = 0);
63 
81 {
82 protected:
83  static const unsigned minsize;
84  static const unsigned slotsize;
85  static const unsigned pagesize;
86  static const unsigned slotlimit;
87  static const unsigned slotcount;
88 
89  friend class StringObject;
90 
91 private:
92  friend class MemPager;
93 
94  static MemPager *pager;
95  static char **idx;
96 
97 #ifdef CCXX_PACKED
98 #pragma pack(1)
99 #endif
100 
101  union {
102  struct {
103  char *text;
104  size_t size;
105  size_t length;
106  } bigstring;
107  struct {
108  char text[(sizeof(char *) + (sizeof(size_t) * 2) + 1)];
109  char length : 6;
110  bool big : 1;
111  } ministring;
112  } content;
113 
114 #ifdef CCXX_PACKED
115 #pragma pack()
116 #endif
117 
118 protected:
125  inline bool isBig(void) const
126  {return content.ministring.big;};
127 
136  const char *set(const char *str, size_t len = 0);
137 
144  void set(const String &str);
145 
146 #ifdef HAVE_SNPRINTF
147 
154  const char *set(size_t size, const char *format, ...);
155 #endif
156 
163  void copy(const String &str);
164 
168  void init(void);
169 
177  static char *getSpace(size_t size);
178 
186  size_t setSize(size_t size);
187 
193  void setLength(size_t len);
194 
205  virtual int compare(const char *text, size_t len = 0, size_t index = 0) const;
206 
216  size_t search(const char *text, size_t clen = 0, size_t offset = 0) const;
217 
218 public:
219  static const size_t npos;
220 
221  typedef size_t size_type;
222 
227 
233  String(const String &original);
234 
240  String(const char *str);
241 
247  String(std::string string);
248 
256  String(const String &str, size_t offset, size_t len = npos);
257 
258 #ifdef HAVE_SNPRINTF
259 
265  String(size_t size, const char *format, ...);
266 #else
267 
274  String(size_t count, const char *str);
275 #endif
276 
283  String(size_t count, const char fill = ' ');
284 
288  virtual ~String();
289 
297  const char *getIndex(size_t index) const;
298 
304  char *getText(void) const;
305 
311  long getValue(long defvalue = 0l) const;
312 
318  bool getBool(bool defbool = false) const;
319 
325  const size_t getLength(void) const;
326 
332  const size_t getSize(void) const;
333 
339  bool isEmpty(void) const;
340 
346  void resize(size_t size);
347 
351  void clear(void);
352 
358  char at(ssize_t offset) const;
359 
368  unsigned count(const String &s, size_t offset = 0) const;
369 
379  unsigned count(const char *s, size_t offset = 0, size_t len = 0) const;
380 
388  String token(const char *delim = " \t\n\r", size_t offset = 0);
389 
398  size_t find(const String &s, size_t offset = 0, unsigned instance = 1) const;
399 
407  size_t rfind(const String &s, size_t offset = 0) const;
408 
418  size_t find(const char *s, size_t offset = 0, size_t len = 0, unsigned count = 1) const;
419 
428  size_t rfind(const char *s, size_t offset = 0, size_t len = 0) const;
429 
435  inline void trim(const char *cs)
436  {setLength(strtrim(cs, getText(), getLength()));};
437 
443  inline void chop(const char *cs)
444  {setLength(strchop(cs, getText(), getLength()));};
445 
451  void strip(const char *cs);
452 
458  inline void chop(size_t chars)
459  {erase(0, chars);};
460 
466  void trim(size_t count);
467 
474  void erase(size_t start, size_t len = npos);
475 
483  void insert(size_t start, const char *text, size_t len = 0);
484 
491  void insert(size_t start, const String &str);
492 
502  void replace(size_t start, size_t len, const char *text, size_t count = 0);
503 
512  void replace(size_t start, size_t len, const String &string);
513 
523  inline size_t find(unsigned instance, const char *text, size_t offset = 0, size_t len = 0) const
524  {return find(text, offset, len, instance);};
525 
534  inline size_t find(unsigned instance, const String &string, size_t offset = 0) const
535  {return find(string, offset, instance);};
536 
545  inline String substr(size_t start, size_t len) const
546  {return String(*this, start, len);};
547 
555  inline const char *(index)(size_t ind) const
556  {return getIndex(ind);};
557 
562  inline void compact(void)
563  {resize(getLength() + 1);};
564 
570  inline char *c_str(void) const
571  {return getText();};
572 
578  inline operator char *() const
579  {return getText();};
580 
586  inline bool operator!(void) const
587  {return isEmpty();};
588 
594  inline char *text(void) const
595  {return getText();};
596 
602  inline char *data(void) const
603  {return getText();};
604 
610  inline size_t length(void) const
611  {return strlen(getText());};
612 
618  inline size_t size(void) const
619  {return getLength();};
620 
626  inline size_t capacity(void) const
627  {return getSize();};
628 
632  bool empty(void) const
633  {return isEmpty();};
634 
641  void append(const char *str, size_t count = 0);
642 
643 #ifdef HAVE_SNPRINTF
644 
650  void append(size_t size, const char *format, ...);
651 #endif
652 
660  void append(const char *str, size_t offset, size_t count);
661 
667  void add(char c);
668 
674  void append(const String &str);
675 
681  inline const char operator[](unsigned ind) const
682  {return at(ind);};
683 
687  inline const char *operator =(const char *str)
688  {return set(str);};
689 
693  friend __EXPORT String operator+(const String &s1, const String &s2);
694 
695  friend __EXPORT String operator+(const String &s1, const char *s2);
696 
697  friend __EXPORT String operator+(const char *s1, const String &s2);
698 
699  friend __EXPORT String operator+(const String &s1, const char c2);
700 
701  friend __EXPORT String operator+(const char c1, const String &s2);
702 
706  inline String &operator+=(const String &str)
707  {append(str); return *this;};
708 
712  inline String &operator+=(char c)
713  {add(c); return *this;};
714 
718  inline String &operator+=(const char *str)
719  {append(str); return *this;};
720 
724  inline String &operator+=(const std::string &str)
725  {append(str.c_str()); return *this;};
726 
737  friend std::istream &getline(std::istream &is, String &str, char delim, size_t size);
738 
743  friend __EXPORT std::ostream &operator<<(std::ostream &os, const String &str);
744 
748  inline friend std::istream &operator>>(std::istream &is, String &str)
749  {return getline(is, str);};
750 
751 #ifdef HAVE_SNPRINTF
752 
760  friend __EXPORT int strprintf(String &str, size_t size, const char *format, ...);
761 #endif
762 
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  bool operator==(const String &str) const;
772  bool operator==(const char *str) const;
773  bool operator!=(const String &str) const;
774  bool operator!=(const char *str) const;
775 
776 #ifdef HAVE_SNPRINTF
777 
781  inline String &operator+=(int i)
782  {append(16, "%d", i); return *this;};
783 
784  inline String &operator+=(unsigned int i)
785  {append(16, "%u", i); return *this;};
786 
787  inline String &operator+=(long l)
788  {append(16, "%l", l); return *this;};
789 
790  inline String &operator+=(unsigned long l)
791  {append(16, "%ul", l); return *this;};
792 
793  inline String &operator+=(float f)
794  {append(32, "%f", f); return *this;};
795 
796  inline String &operator+=(double d)
797  {append(32, "%f", d); return *this;};
798 
799  inline String &operator+=(short s)
800  {append(8, "%hd", s); return *this;};
801 
802  inline String &operator+=(unsigned short s)
803  {append(8, "%hu", s); return *this;};
804 
805 
809  inline String &operator=(int i)
810  {set(16, "%d", i); return *this;};
811 
812  inline String &operator=(unsigned int i)
813  {set(16, "%u", i); return *this;};
814 
815  inline String &operator=(long l)
816  {set(16, "%l", l); return *this;};
817 
818  inline String &operator=(unsigned long l)
819  {set(16, "%ul", l); return *this;};
820 
821  inline String &operator=(float f)
822  {set(32, "%f", f); return *this;};
823 
824  inline String &operator=(double d)
825  {set(32, "%f", d); return *this;};
826 
827  inline String &operator=(short s)
828  {set(8, "%hd", s); return *this;};
829 
830  inline String &operator=(unsigned short s)
831  {set(8, "%hu", s); return *this;};
832 #endif
833 
834  inline String &operator=(const String &original)
835  {copy(original); return *this;};
836 
840  bool operator*=(const String &str) const;
841 
845  bool operator*=(const char *str) const;
846 };
847 
848 class __EXPORT SString : public String, protected std::streambuf, public std::ostream
849 {
850 protected:
856  int overflow(int c);
857 
858 public:
863 
867  SString(const SString &from);
868 
873 };
874 
885 {
886 public:
890  void *operator new(size_t size) NEW_THROWS;
891 
895  void operator delete(void *obj);
896 };
897 
898 #ifdef CCXX_NAMESPACES
899 }
900 #endif
901 
902 #endif
ost::String::getLength
const size_t getLength(void) const
Get the assigned length of string.
ost::String::setLength
void setLength(size_t len)
Set the length value of the string content.
ost::String::insert
void insert(size_t start, const char *text, size_t len=0)
Insert text into a string.
ost::String::slotcount
static const unsigned slotcount
Definition: string.h:87
ost::String::String
String(const String &str, size_t offset, size_t len=npos)
Create a new string from a subset of another string.
ost::String::operator*=
bool operator*=(const char *str) const
Test if text is contained in our string.
ost::String::~String
virtual ~String()
Destroy the string...
ost::String::operator+
friend __EXPORT String operator+(const String &s1, const char *s2)
ost::String::strip
void strip(const char *cs)
Strip lead and trailing characters from a string.
ost::String::getline
friend std::istream & getline(std::istream &is, String &str, char delim, size_t size)
Fetch input from a std::istream into the current string variable until either the string variable is ...
ost::String::operator+=
String & operator+=(unsigned long l)
Definition: string.h:790
ost::String::operator=
String & operator=(const String &original)
Definition: string.h:834
ost::String::append
void append(const String &str)
Append string to the end of the current string.
ost::String::minsize
static const unsigned minsize
Definition: string.h:83
ost::String::erase
void erase(size_t start, size_t len=npos)
Erase a portion of string.
ost::String::slotlimit
static const unsigned slotlimit
Definition: string.h:86
ost::String::chop
void chop(size_t chars)
Chop n leading characters from a string.
Definition: string.h:458
ost::SString::~SString
~SString()
Cancel out the object.
ost::String::size_type
size_t size_type
Definition: string.h:221
ost::String::size
size_t size(void) const
Get actual length of string data.
Definition: string.h:618
ost::String::operator+=
String & operator+=(const std::string &str)
Append operator.
Definition: string.h:724
ost::String::token
String token(const char *delim=" \t\n\r", size_t offset=0)
Extract a new string as a token from the current string.
ost::String::operator+=
String & operator+=(char c)
Append operator.
Definition: string.h:712
ost::SString::SString
SString()
Create an empty streamable string ready for input.
ost::StringObject
The StringObject class is used to derive subclasses that use the String managed memory pool for all s...
Definition: string.h:885
ost::strtrim
__EXPORT size_t strtrim(const char *cs, char *str, size_t len=0)
ost::String::operator=
String & operator=(long l)
Definition: string.h:815
ost::String::chop
void chop(const char *cs)
Chop leading characters from a string.
Definition: string.h:443
ost::String::npos
static const size_t npos
Definition: string.h:219
ost::String::String
String(size_t count, const char fill=' ')
Fill a new string with character data.
ost::String::operator+
friend __EXPORT String operator+(const String &s1, const char c2)
ost::String::getSize
const size_t getSize(void) const
Get the allocation size of the string variable.
ost::String::operator+=
String & operator+=(short s)
Definition: string.h:799
ost::String::capacity
size_t capacity(void) const
Get space allocated to hold current string.
Definition: string.h:626
ost::String::operator=
String & operator=(unsigned short s)
Definition: string.h:830
ost::String::search
size_t search(const char *text, size_t clen=0, size_t offset=0) const
An internal method used to search for a substring starting at a known offset.
ost::String::operator==
bool operator==(const char *str) const
ost::String::String
String(std::string string)
Create a String from std::string.
ost::String::find
size_t find(const char *s, size_t offset=0, size_t len=0, unsigned count=1) const
Find the index to the nth instance of text in our string.
ost::String::getValue
long getValue(long defvalue=0l) const
Get the value of a string.
ost::String::String
String(const String &original)
Copy constructor.
ost::String::operator!
bool operator!(void) const
Logical test for string empty.
Definition: string.h:586
ost::String::insert
void insert(size_t start, const String &str)
Insert other string into our string.
ost::String::find
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:534
ost::String::add
void add(char c)
Add a character to the end of a string.
ost::String::count
unsigned count(const String &s, size_t offset=0) const
Count the number of occurences of a specific string within our string.
ost::String::strprintf
friend __EXPORT int strprintf(String &str, size_t size, const char *format,...)
Print values directly into a string variable.
ost::String::copy
void copy(const String &str)
Impliment the copy constructor, used internally.
ost::String::count
unsigned count(const char *s, size_t offset=0, size_t len=0) const
Count the number of occurrences of a specific text pattern within our string.
ost::String::operator<<
friend __EXPORT std::ostream & operator<<(std::ostream &os, const String &str)
Stream the content of our string variable directly to a C++ streaming source.
ost::String::String
String(const char *str)
Create a string from a cstring.
ost::String::operator=
String & operator=(int i)
Assignment operator.
Definition: string.h:809
ost::SString::overflow
int overflow(int c)
This is the streambuf function that actually outputs the data to the string.
ost::String::operator==
bool operator==(const String &str) const
ost::String::operator>=
bool operator>=(const char *str) const
ost::String::operator+=
String & operator+=(unsigned short s)
Definition: string.h:802
ost::String::trim
void trim(size_t count)
Trim n trailing characters from a string.
ost::String::operator=
String & operator=(unsigned long l)
Definition: string.h:818
ost::String::operator+
friend __EXPORT String operator+(const char c1, const String &s2)
ost::String::isBig
bool isBig(void) const
Determine if string is allocated in local variable or an external reference.
Definition: string.h:125
__EXPORT
#define __EXPORT
Definition: config.h:979
ost::String::operator+=
String & operator+=(const String &str)
Append operator.
Definition: string.h:706
ost::String::operator+=
String & operator+=(double d)
Definition: string.h:796
ost::String::operator[]
const char operator[](unsigned ind) const
Extract a character by array indexing.
Definition: string.h:681
ost::String::operator+
friend __EXPORT String operator+(const String &s1, const String &s2)
Add two strings and return a temporary object.
ost::String::operator<
bool operator<(const char *str) const
ost::String::resize
void resize(size_t size)
Re-allocate buffer space for string.
NEW_THROWS
#define NEW_THROWS
Definition: config.h:945
ost::String::slotsize
static const unsigned slotsize
Definition: string.h:84
ost::String::operator<=
bool operator<=(const char *str) const
ost::String::at
char at(ssize_t offset) const
Return a character at a known offset.
ost::String::set
const char * set(size_t size, const char *format,...)
Set the content of the string variable to that of a formatted printf style string.
ost::MemPager
The memory pager is used to allocate cumulative memory pages for storing object specific "persistant"...
Definition: misc.h:92
ost::SString::SString
SString(const SString &from)
Copy constructor.
ost::String::init
void init(void)
Used to initialize a string object.
ost::String::getBool
bool getBool(bool defbool=false) const
Get the bool flag of a string.
ost::String::rfind
size_t rfind(const char *s, size_t offset=0, size_t len=0) const
Find last occurence of a text in our string.
ost::String::operator>>
friend std::istream & operator>>(std::istream &is, String &str)
Stream input into our variable.
Definition: string.h:748
ost::String::length
char length
Definition: string.h:109
ost::String::pagesize
static const unsigned pagesize
Definition: string.h:85
ost::String::getSpace
static char * getSpace(size_t size)
Used to fetch memory, if needed, based on the size, from the pager, or the system heap.
strchar.h
Common and portable character string related functions.
ost::SString
Definition: string.h:849
ost::String::String
String(size_t size, const char *format,...)
Create a string from formatted text input.
ost::String::operator<=
bool operator<=(const String &str) const
ost::String::operator*=
bool operator*=(const String &str) const
Test if string is contained in our string.
ost::String::length
size_t length
Definition: string.h:105
ost::String::replace
void replace(size_t start, size_t len, const String &string)
Replace text at a specific position in the string with new string,.
ost::String::operator+=
String & operator+=(long l)
Definition: string.h:787
ost::String::operator=
String & operator=(short s)
Definition: string.h:827
missing.h
substitute functions which may be missing in target platform libc.
ost::String::operator>
bool operator>(const String &str) const
ost::String::operator+=
String & operator+=(const char *str)
Append operator.
Definition: string.h:718
ost::String::set
void set(const String &str)
Set the content of the string variable to that of another variable.
ost::String::operator<
bool operator<(const String &str) const
ost::String::append
void append(const char *str, size_t offset, size_t count)
Append text into the current string.
ost::String::operator+
friend __EXPORT String operator+(const char *s1, const String &s2)
ost::strchop
__EXPORT size_t strchop(const char *cs, char *str, size_t len=0)
ost::find
__EXPORT char * find(const char *cs, char *str, size_t len=0)
ost::String::text
char * text(void) const
Alternate get text method.
Definition: string.h:594
ost
Definition: address.h:64
ost::String::rfind
size_t rfind(const String &s, size_t offset=0) const
Find last occurence of a substring in our string.
ost::String::clear
void clear(void)
Clear the contents of the entire string.
ost::String::setSize
size_t setSize(size_t size)
Set the size of allocated space in the string variable (capacity) to a known value.
ost::String::operator>=
bool operator>=(const String &str) const
ost::String::append
void append(const char *str, size_t count=0)
Append text to the end of the current string.
ost::String::operator!=
bool operator!=(const char *str) const
ost::String::text
char * text
Definition: string.h:103
ost::String::String
String()
Construct an empty string.
ost::String::substr
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:545
ost::String::operator=
String & operator=(unsigned int i)
Definition: string.h:812
ost::String::trim
void trim(const char *cs)
Trim trailing characters from a string.
Definition: string.h:435
ost::String::set
const char * set(const char *str, size_t len=0)
Set the content of the string variable to the specified string value, and use smart re-allocation str...
ost::String::getIndex
const char * getIndex(size_t index) const
Get a string pointer to string content based on an indexed offset.
ost::String::c_str
char * c_str(void) const
Old ANSI C++ compatible string pointer extraction.
Definition: string.h:570
ost::String::getText
char * getText(void) const
Get the text of a string.
ost::getline
__EXPORT std::istream & getline(std::istream &is, String &str, char delim='\n', size_t size=0)
ost::String::compare
virtual int compare(const char *text, size_t len=0, size_t index=0) const
A derivable low level comparison operator.
ost::String::find
size_t find(const String &s, size_t offset=0, unsigned instance=1) const
Find the index to the nth instance of a substring in our string.
ost::String::length
size_t length(void) const
Get length as if null terminated string.
Definition: string.h:610
ost::String::size
size_t size
Definition: string.h:104
ost::String::operator+=
String & operator+=(float f)
Definition: string.h:793
ost::String::replace
void replace(size_t start, size_t len, const char *text, size_t count=0)
Replace text at a specific position in the string with new text.
ost::String::operator+=
String & operator+=(unsigned int i)
Definition: string.h:784
ost::String::empty
bool empty(void) const
Return true if string is empty.
Definition: string.h:632
ost::String::append
void append(size_t size, const char *format,...)
Append formatted text to the end of the current string.
ost::String::operator>
bool operator>(const char *str) const
ost::String::compact
void compact(void)
Reduce the size of the string allocation to the minimum needed based on the current effective length.
Definition: string.h:562
ost::String::big
bool big
Definition: string.h:110
ost::String::operator+=
String & operator+=(int i)
Append operator.
Definition: string.h:781
ost::String::data
char * data(void) const
Alternate get text method.
Definition: string.h:602
ost::String
This is a generic and portable string class.
Definition: string.h:81
ost::String::operator!=
bool operator!=(const String &str) const
ost::String::isEmpty
bool isEmpty(void) const
Return true if string is empty.
ost::String::operator=
String & operator=(double d)
Definition: string.h:824
ost::String::operator=
String & operator=(float f)
Definition: string.h:821
ost::String::index
const char *() index(size_t ind) const
Return an indexed string based on the index, such as from a find.
Definition: string.h:555
ost::String::find
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:523