This is a generic and portable string class.
More...
#include <string.h>
Inheritance diagram for String:
Collaboration diagram for String:
Public Types
Public Member Functions
Create a string from a cstring.
More...
Create a new string from a subset of another string.
More...
Create a string of a known size, and optionally fill with content.
More...
Fill a new string with character data.
More...
Get a string pointer to string content based on an indexed offset.
More...
bool
getBool (bool defbool=false) const
Get the bool flag of a string.
More...
Get the assigned length of string.
More...
Get the allocation size of the string variable.
More...
Return true if string is empty.
More...
Re-allocate buffer space for string.
More...
Clear the contents of the entire string.
More...
char
at (ssize_t offset) const
Return a character at a known offset.
More...
unsigned
count (const
String &s, size_t offset=0) const
Count the number of occurences of a specific string within our string.
More...
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.
More...
String token (const char *delim=" \t\n\r", size_t offset=0)
Extract a new string as a token from the current string.
More...
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.
More...
Find last occurence of a substring in our string.
More...
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.
More...
size_t
rfind (const char *s, size_t offset=0, size_t len=0) const
Find last occurence of a text in our string.
More...
void
trim (const char *cs)
Trim trailing characters from a string.
More...
void
chop (const char *cs)
Chop leading characters from a string.
More...
void
strip (const char *cs)
Strip lead and trailing characters from a string.
More...
Chop n leading characters from a string.
More...
Trim n trailing characters from a string.
More...
void
insert (size_t start, const char *
text, size_t len=0)
Insert other string into our string.
More...
Replace text at a specific position in the string with new text.
More...
Replace text at a specific position in the string with new string,.
More...
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.
More...
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.
More...
Return a new string that contains a specific substring of the current string.
More...
const char *()
index (size_t ind) const
Return an indexed string based on the index, such as from a find.
More...
Reduce the size of the string allocation to the minimum needed based on the current effective length.
More...
char *
c_str (void) const
Old ANSI C++ compatible string pointer extraction.
More...
Get our string data through dereference operator.
More...
Logical test for string empty.
More...
Get length as if null terminated string.
More...
Get actual length of string data.
More...
Get space allocated to hold current string.
More...
Return true if string is empty.
More...
Append text to the end of the current string.
More...
void
append (const char *str, size_t offset, size_t
count)
Append text into the current string.
More...
Add a character to the end of a string.
More...
Append string to the end of the current string.
More...
Extract a character by array indexing.
More...
Assign our string for c string.
More...
Test if string is contained in our string.
More...
Test if text is contained in our string.
More...
Static Public Attributes
Protected Member Functions
Determine if string is allocated in local variable or an external reference.
More...
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 strategies if appropriate to shrink the size of the variable.
More...
Set the content of the string variable to that of another variable.
More...
Impliment the copy constructor, used internally.
More...
Used to initialize a string object.
More...
Set the size of allocated space in the string variable (capacity) to a known value.
More...
Set the length value of the string content.
More...
A derivable low level comparison operator.
More...
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.
More...
Static Protected Member Functions
Used to fetch memory, if needed, based on the size, from the pager, or the system heap.
More...
Static Protected Attributes
Private Attributes
union {
struct {
struct {
char
text [(sizeof(char *)+(sizeof(size_t)*2)+1)]
Static Private Attributes
Friends
Add two strings and return a temporary object.
More...
Fetch input from a std::istream into the current string variable until either the string variable is filled (based on current length) or the deliminator is read.
More...
Stream the content of our string variable directly to a C++ streaming source.
More...
Stream input into our variable.
More...
Detailed Description
This is a generic and portable string class.
It uses optimized memory allocation strategies to efficiently handle smaller string content by grouping strings into 32 byte aligned slots that can be re-allocated from a free list directly.
While meant to cover the basic functionality of the ANSI C++ string class in form and function, this class offers some important enhancements, including the ability to derive class type specific versions of itself. The latter might be used to derive a unicode string, a string for data and time data types, or to add case insensitive comparisons, for example.
- Author
- David Sugar dyfet.nosp@m.@ost.nosp@m.el.co.nosp@m.m Generic string class.
Definition at line 77 of file string.h.
Member Typedef Documentation
Constructor & Destructor Documentation
Construct an empty string.
String::String
(
const
String &
original )
Copy constructor.
- Parameters
-
original string to copy from.
String::String
(
const char *
str )
Create a string from a cstring.
- Parameters
-
str text to set with.
String::String
(
std::string
string )
Create a String from std::string.
- Parameters
-
string from std::string to copy from.
String::String
(
const
String &
str,
size_t
offset,
)
Create a new string from a subset of another string.
- Parameters
-
str reference of source string.
offset offset to start of data in prior string.
len length of our substring.
String::String
(
size_t
count,
const char *
str
)
Create a string of a known size, and optionally fill with content.
- Parameters
-
count size to allocate for our new string.
str content to put into it.
String::String
(
size_t
count,
const char
fill = ' '
)
Fill a new string with character data.
- Parameters
-
count size of new string.
fill char to fill string with.
virtual String::~String
(
)
virtual
Member Function Documentation
void String::add
(
char
c )
Add a character to the end of a string.
- Parameters
-
c char to add.
void String::append
(
const char *
str,
size_t
count = 0
)
Append text to the end of the current string.
- Parameters
-
str text to append.
count size of text to append.
void String::append
(
const char *
str,
size_t
offset,
size_t
count
)
Append text into the current string.
- Parameters
-
str text to append.
offset offset to overlay.
count size of text to append.
void String::append
(
const
String &
str )
Append string to the end of the current string.
- Parameters
-
str string to append.
char String::at
(
ssize_t
offset )
const
Return a character at a known offset.
- Returns
- character at offset.
char* String::c_str
(
void
)
const
inline
Old ANSI C++ compatible string pointer extraction.
- Returns
- string data.
Definition at line 567 of file string.h.
size_t String::capacity
(
void
)
const
inline
Get space allocated to hold current string.
- Returns
- space of memory buffer from heap or local.
Definition at line 623 of file string.h.
void String::chop
(
const char *
cs )
inline
Chop leading characters from a string.
- Parameters
-
cs list of chars to chop.
Definition at line 440 of file string.h.
void String::chop
(
size_t
chars )
inline
Chop n leading characters from a string.
- Parameters
-
chars count to chop.
Definition at line 455 of file string.h.
void String::clear
(
void
)
Clear the contents of the entire string.
void String::compact
(
void
)
inline
Reduce the size of the string allocation to the minimum needed based on the current effective length.
Definition at line 559 of file string.h.
virtual int String::compare
(
const char *
text,
size_t
len = 0
,
size_t
index = 0
)
const
protectedvirtual
A derivable low level comparison operator.
This can be used to create custom comparison data types in derived string classes.
- Returns
- 0 if match, or value for ordering.
- Parameters
-
text text to compare.
len length of text to compare.
index offset from start of string, used in searchs.
void String::copy
(
const
String &
str )
protected
Impliment the copy constructor, used internally.
Will always create a minimum sized string allocation.
- Parameters
-
str string to copy from.
unsigned String::count
(
const
String &
s,
size_t
offset = 0
)
const
Count the number of occurences of a specific string within our string.
- Returns
- count of instances.
- Parameters
-
s string to test.
offset offset to start from.
unsigned String::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.
- Returns
- count of instances.
- Parameters
-
s text pattern to find
offset offset to start from.
len length of text pattern if specified.
char* String::data
(
void
)
const
inline
Alternate get text method.
- Returns
- string data.
Definition at line 599 of file string.h.
bool String::empty
(
void
)
const
inline
Return true if string is empty.
Definition at line 629 of file string.h.
void String::erase
(
size_t
start,
)
Erase a portion of string.
- Parameters
-
start starting index to erase from.
len number of characters to erase.
size_t String::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.
- Returns
- index of found substring.
- Parameters
-
s string to search for.
offset offset to start at.
instance instance to look for.
size_t String::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.
- Returns
- index of found substring.
- Parameters
-
s string to search for.
offset offset to start at.
len size of string text.
count instance to look for.
size_t String::find
(
unsigned
instance,
const char *
text,
size_t
offset = 0
,
size_t
len = 0
)
const
inline
A more convenient version of find for nth occurences, by putting the instance first.
- Parameters
-
instance nth instance to look for.
text text to look for.
offset offset to start at.
len length of text.
Definition at line 520 of file string.h.
size_t String::find
(
unsigned
instance,
size_t
offset = 0
)
const
inline
A more convenient version of find for nth occurences, by putting the instance first.
- Parameters
-
instance nth instance to look for.
string reference to look for.
offset offset to start at.
Definition at line 531 of file string.h.
bool String::getBool
(
bool
defbool = false
)
const
Get the bool flag of a string.
- Returns
- boolean value.
const char* String::getIndex
(
size_t
index )
const
Get a string pointer to string content based on an indexed offset.
A NULL is returned if the index is outsize of range.
- Returns
- string content or NULL if invalid index.
- Parameters
-
index
const size_t String::getLength
(
void
)
const
Get the assigned length of string.
- Returns
- string length.
const size_t String::getSize
(
void
)
const
Get the allocation size of the string variable.
- Returns
- allocation size.
static char* String::getSpace
(
size_t
size )
staticprotected
Used to fetch memory, if needed, based on the size, from the pager, or the system heap.
- Returns
- string pointer to space.
- Parameters
-
size of space needed.
char* String::getText
(
void
)
const
Get the text of a string.
- Returns
- string content.
long String::getValue
(
long
defvalue = 0l
)
const
Get the value of a string.
- Returns
- string value as number.
const char*() String::index
(
size_t
ind )
const
inline
Return an indexed string based on the index, such as from a find.
If out of range, a NULL string is returned.
- Returns
- pointer to string data from our string,
- Parameters
-
ind index or offset to use.
Definition at line 552 of file string.h.
void String::init
(
void
)
protected
Used to initialize a string object.
void String::insert
(
size_t
start,
const char *
text,
size_t
len = 0
)
Insert text into a string.
- Parameters
-
start starting offset to insert at.
text text to insert.
len size of text to insert.
void String::insert
(
size_t
start,
)
Insert other string into our string.
- Parameters
-
start string offset to insert at.
str string to insert.
bool String::isBig
(
void
)
const
inlineprotected
Determine if string is allocated in local variable or an external reference.
- Returns
- true if external heap is used.
Definition at line 122 of file string.h.
bool String::isEmpty
(
void
)
const
Return true if string is empty.
- Returns
- true if string is empty string.
size_t String::length
(
void
)
const
inline
Get length as if null terminated string.
- Returns
- cstring length.
Definition at line 607 of file string.h.
String::operator char *
(
)
const
inline
Get our string data through dereference operator.
- Returns
- string data.
Definition at line 575 of file string.h.
bool String::operator!
(
void
)
const
inline
Logical test for string empty.
- Returns
- true if is empty.
Definition at line 583 of file string.h.
bool String::operator*=
(
const
String &
str )
const
Test if string is contained in our string.
bool String::operator*=
(
const char *
str )
const
Test if text is contained in our string.
Append operator.
Definition at line 703 of file string.h.
String& String::operator+=
(
char
c )
inline
Append operator.
Definition at line 709 of file string.h.
String& String::operator+=
(
const char *
str )
inline
Append operator.
Definition at line 715 of file string.h.
String& String::operator+=
(
const std::string &
str )
inline
Append operator.
Definition at line 721 of file string.h.
bool String::operator<
(
const
String &
str )
const
bool String::operator<
(
const char *
str )
const
bool String::operator<=
(
const
String &
str )
const
bool String::operator<=
(
const char *
str )
const
const char* String::operator=
(
const char *
str )
inline
Assign our string for c string.
Definition at line 684 of file string.h.
bool String::operator==
(
const
String &
str )
const
bool String::operator==
(
const char *
str )
const
bool String::operator>
(
const
String &
str )
const
bool String::operator>
(
const char *
str )
const
bool String::operator>=
(
const
String &
str )
const
bool String::operator>=
(
const char *
str )
const
const char String::operator[]
(
unsigned
ind )
const
inline
Extract a character by array indexing.
- Returns
- character code.
Definition at line 678 of file string.h.
void String::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.
- Parameters
-
start starting offset to replace at.
len length of text to remove.
text text to replace with.
count size of replacement text.
void String::replace
(
size_t
start,
size_t
len,
)
Replace text at a specific position in the string with new string,.
- Parameters
-
start starting offset to replace at.
len length of text to remove.
string reference to replace with.
void String::resize
(
size_t
size )
Re-allocate buffer space for string.
- Parameters
-
size new size to use.
size_t String::rfind
(
const
String &
s,
size_t
offset = 0
)
const
Find last occurence of a substring in our string.
- Returns
- index of last instance found,
- Parameters
-
s string to search for.
offset offset to start from.
size_t String::rfind
(
const char *
s,
size_t
offset = 0
,
size_t
len = 0
)
const
Find last occurence of a text in our string.
- Returns
- index of last instance found,
- Parameters
-
s string to search for.
offset offset to start from.
len size of string to look for.
size_t String::search
(
const char *
text,
size_t
clen = 0
,
size_t
offset = 0
)
const
protected
An internal method used to search for a substring starting at a known offset.
Used by find and count methods.
- Returns
- npos if fails, or offset to text found.
- Parameters
-
text text to search for.
clen length of search text.
offset offset to start from.
const char* String::set
(
const char *
str,
size_t
len = 0
)
protected
Set the content of the string variable to the specified string value, and use smart re-allocation strategies if appropriate to shrink the size of the variable.
- Parameters
-
str string to set.
len length of string if passed.
void String::set
(
const
String &
str )
protected
Set the content of the string variable to that of another variable.
Uses the string set method.
- Parameters
-
str string to copy from.
void String::setLength
(
size_t
len )
protected
Set the length value of the string content.
- Parameters
-
len size in bytes.
size_t String::setSize
(
size_t
size )
protected
Set the size of allocated space in the string variable (capacity) to a known value.
The value is recomputed and adjusted based on allocation method.
- Parameters
-
size in bytes.
size_t String::size
(
void
)
const
inline
Get actual length of string data.
- Returns
- actual size of string.
Definition at line 615 of file string.h.
void String::strip
(
const char *
cs )
Strip lead and trailing characters from a string.
- Parameters
-
cs list of chars to strip.
String String::substr
(
size_t
start,
size_t
len
)
const
inline
Return a new string that contains a specific substring of the current string.
- Returns
- new string.
- Parameters
-
start starting offset for extracted substring.
len length of substring.
Definition at line 542 of file string.h.
char* String::text
(
void
)
const
inline
Alternate get text method.
- Returns
- string data.
Definition at line 591 of file string.h.
String String::token
(
const char *
delim =
" \t\n\r"
,
size_t
offset = 0
)
Extract a new string as a token from the current string.
- Returns
- string containing token.
- Parameters
-
delim deliminator characters.
offset offset to start from.
void String::trim
(
const char *
cs )
inline
Trim trailing characters from a string.
- Parameters
-
cs list of chars to trim.
Definition at line 432 of file string.h.
void String::trim
(
size_t
count )
Trim n trailing characters from a string.
- Parameters
-
count number of bytes to trim.
Friends And Related Function Documentation
__EXPORT std::istream& getline
(
std::istream &
is,
char
delim = '\n'
,
size_t
size = 0
)
friend
Fetch input from a std::istream into the current string variable until either the string variable is filled (based on current length) or the deliminator is read.
- Parameters
-
is stream to read.
str string to save into.
delim deliminator to use.
size optional size limitor.
Add two strings and return a temporary object.
__EXPORT std::ostream& operator<<
(
std::ostream &
os,
)
friend
Stream the content of our string variable directly to a C++ streaming source.
std::istream& operator>>
(
std::istream &
is,
)
friend
Stream input into our variable.
Definition at line 745 of file string.h.
Field Documentation
struct { ... } String::bigstring
union { ... } String::content
char** String::idx
staticprivate
struct { ... } String::ministring
const unsigned String::minsize
staticprotected
const size_t String::npos
static
const unsigned String::pagesize
staticprotected
const unsigned String::slotcount
staticprotected
const unsigned String::slotlimit
staticprotected
const unsigned String::slotsize
staticprotected
char String::text[(sizeof(char *)+(sizeof(size_t)*2)+1)]
The documentation for this class was generated from the following file: