Last update: 3/29/99
Synopsis: A string class that includes many operations, buffer functionality, numeric conversions, complex searching, and of course char* compatibility.
Contents
Overview what’s it all about Basic Operations typical string class construction and access String Operations substrings, cut and paste, and such Buffer Operations it’s only memory Searching and Matching finding things Find Predicates concerning the type SS::Find Predefined Find Objects whitespace, uppercase, etc. Numerical Operations formatting to/from numbers Supported Types types that can be converted and compared Bounds Checking and Adjusting when it does one of these Exceptions who throws what and when Typedefs a few type aliases Constants some constants Supporting Classes call them intermediate data types STL Compatibility mostly iterators C Compatibility the lowest common denominator
Overview
This class works pretty much like any other string class: feed it string literals, index the characters, let it do the char* memory management. Beyond that there’s basic stuff, more methods, buffer methods, searching, and finally numbers.
Basic Operations
This is standard fare for C++ string classes. The functionality of length, size, empty, operator [], at, c_str, data, and erase is compatible with std::string. The operator [] and at can throw an exception, see Exceptions.
SS (); | construct a Super String with a value of "" |
---|---|
template <class T> SS (T const & t); | construct a Super String for any supported type |
SS (SS const & s); | copy constructor for Super String |
SS (void const * v, int n); | construct a Super String by copying n bytes starting at v |
int length () const; int size () const; |
the number of characters in the string |
bool empty () const; | returns true if the string is empty; false otherwise an empty string will have a length of zero and a value of "" |
char & operator [] (int ind); char const & operator [] (int ind) const; |
return a reference to the character at position ind |
char & at (int ind); char const & at (int ind) const; |
same as operator [] (int) |
operator char * (); operator char const * () const; |
returns a pointer to the first char of the string see C Compatibility for more |
char * c_str (); char const * c_str () const; char * data (); char const * data () const; |
same as operator char * () |
SS clone () const; | return a copy of the string |
SS& erase (); | erase the string, i.e. set it to "" return a reference to the erased string |
String Operations
These methods deal with accessing or manipulating ranges of characters. Such as:
- substring – a range of characters that can be copied or assigned to.
- get – copy a range of characters.
- set – change some of a string’s characters.
- cut – remove and return a range of characters.
- paste – insert some characters.
- removeRange – delete a range of characters.
- replaceRange – replace a range of characters with some others.
- reverse – reverse the order.
- sort – sort by character value.
- fill – fill a range with some value.
- repeat – fill with a string.
They operate on a range of characters. There are three kinds of ranges:
int beg, int len where beg is the starting position, len the number of characters. Pair<int> beglen with beglen._0 the start and beglen._1 the length. See Supporting Classes std::vector <Pair<int>> beglen multiple ranges can be operated on.
Some methods can take any of these kinds of ranges as input. Methods that return ranges do so as either Pair<int> or std::vector <Pair<int>> beglen.
firstChar and lastChar can throw an exception, see Exceptions.
substring SubSS sub (int beg, int len); SS sub (int beg, int len) const; SubSS operator () (int beg, int len); SS operator () (int beg, int len) const; SubSS operator () (Pair<int> const & beglen); SS operator () (Pair<int> const & beglen) const; |
return a substring representing a range of characters a substring of a non-constant string can be assigned to see Supporting Classes for more on SS::SubSS |
---|---|
get SS get (int beg, int len=1) const; SS getFrom (int beg) const; SS get (Pair<int> const & beglen) const; SS get (std::vector< Pair<int> > const & beglen) const; std::vector<SS>& get (std::vector<SS>& s, std::vector< Pair<int> > const & beglen) const; |
copy a range of characters into a new string getFrom gets all the way to the end of the string get(std::vector< Pair<int> > const & beglen) concatenates the ranges get(std::vector<SS>& s, std::vector< Pair<int> > const & beglen), stores each range as an element of s and returns s |
set SS& set (SS const & s, int pos=0); SS& set (SS const & s, int pos, int beg, int len); SS& set (SS const & s, int pos, Pair<int> const & beglen); SS& set (SS const & s, std::vector<int> const & pos); SS& set (std::vector<SS> const & s, std::vector<int> const & pos); |
assign new values to a range of characters starting at pos beg, len, and beglen refer to the argument string s set does not extend the string the updated string is returned the last two set s or an element of s at the elements of pos |
cut SS cut (int beg, int len=1); SS cutFrom (int beg); SS cut (Pair<int> const & beglen); SS& cut (SS& s, int beg, int len=1); SS& cut (SS& s, Pair<int> const & beglen); std::vector<SS>& cut (std::vector<SS>& s, std::vector< Pair<int> > const & beglen); |
delete a range of characters and return them beg and len or beglen specify the range or ranges to be cut cutFrom cuts all the way to the end of the string either a new string or s is returned the last makes multiple cuts at once |
paste SS& paste (SS const & s, int pos=0); SS& paste (SS const & s, int pos, int beg, int len); SS& paste (SS const & s, int pos, Pair<int> const & beglen); SS& paste (SS const & s, std::vector<int> const & pos); SS& paste (std::vector<SS> const & s, std::vector<int> const & pos); |
insert s before the position pos beg, len, and beglen refer to the argument string s a value for pos of the string’s length or SS::fullength results in an append the updated string is returned the last two paste s or an element of s at the elements of pos |
removeRange SS& removeRange (int beg=0, int len=fullength); SS& removeRange (Pair<int> const & beglen); SS& removeRange (std::vector< Pair<int> > const & beglen); |
delete the specified range or ranges of characters return the updated string |
replaceRange SS& replaceRange (SS const & newseq, int beg=0, int len=fullength); SS& replaceRange (SS const & newseq, Pair<int> const & beglen); SS& replaceRange (SS const & newseq, std::vector< Pair<int> > const & beglen); SS& replaceRange (std::vector<SS> const & s, std::vector< Pair<int> > const & beglen); |
replace a range of characters with newseq beg and len or beglen specify the range or ranges to be replaced the updated string is returned the last replaces the corresponding range from beglen with an element of s |
reverse SS& reverse (int beg=0, int len=fullength); SS& reverse (Pair<int> const & beglen); SS& reverse (std::vector< Pair<int> > const & beglen); SS& itemReverse (std::vector< Pair<int> > const & beglen); SS& tailReverse (int len); |
reverse the order of characters within a range or ranges with multiple ranges each range is considered distinct itemReverse reverses the ranges considered as distinct strings, rather than as individual characters tailReverse reverses the last len characters return the updated string |
sort SS& sort (int beg=0, int len=fullength); SS& sort (Pair<int> const & beglen); SS& sort (std::vector< Pair<int> > const & beglen); SS& itemSort (std::vector< Pair<int> > const & beglen); SS& tailSort (int len); |
sort the characters within a range or ranges with multiple ranges each range is considered distinct itemSort sorts the ranges considered as distinct strings, rather than as individual characters tailSort sorts the last len characters return the updated string |
fill SS& fill (char c, int beg=0, int len=fullength); SS& fill (char c, Pair<int> const & beglen); SS& fill (char c, std::vector< Pair<int> > const & beglen); |
set each character in the range or ranges to c return the updated string |
repeat SS& repeat (SS const & s, int beg=0, int len=fullength); SS& repeat (SS const & s, Pair<int> const & beglen); SS& repeat (SS const & s, std::vector< Pair<int> > const & beglen); |
like fill except with a string argument does not change the length of the string |
SS head (int len) const; | return up to the first len characters of the string |
SS tail (int len) const; | return up to the last len characters of the string |
char & firstChar (); char const & firstChar () const; |
return a reference to the first character of the string |
char & lastChar (); char const & lastChar () const; |
return a reference to the last character of the string |
bool isUpperCase (int pos) const; bool isLowerCase (int pos) const; bool isWhiteSpace (int pos) const; bool isBlackSpace (int pos) const; bool isAlpha (int pos) const; bool isDigit (int pos) const; bool isAlphaNumeric (int pos) const; bool isPunct (int pos) const; bool isPrintable (int pos) const; bool isHexDigit (int pos) const; bool isCntrl (int pos) const; bool isGraph (int pos) const; |
determine a classification of the character at pos these are as per clib isspace, isupper, etc. |
SS& toLower (); SS& toLower (int pos); |
convert the character at pos or the entire string to lowercase return the updated string |
SS& toUpper (); SS& toUpper (int pos); |
convert the character at pos or the entire string to uppercase return the updated string |
int compare (X x) const; | compare string to an object x see Supported Types |
int compareNoCase (SS const & s) const; | do a case insensitive compare |
int compare (void const * v, int n) const; | compare string to the buffer v with a length of n characters |
bool compare (Find const & f) const; | determine if the entire string is matched by f see Find Predicates |
SS trim () const; | remove leading and trailing whitespace |
SS dup (int n=1) const; | create a string that duplicates the current string n times |
Buffer Operations
A buffer is either:
- a block of memory, ready for some other operation or agent to use.
- an arbitrary region of memory, on which string operations may be performed in place.
What constitutes a buffer operation is a little loose:
- some constraint for being well behaved is violated.
- value semantics are violated.
- primarily concerned with raw bytes.
- getting bytes in or out of the string in an unusual or low-level manner.
- directly accessing a Super String’s representation.
A region (also reference) has a representation that points to an internal part of its parent string. The advantages are that no copying need be done to refer to a part of a string and that the parent may be manipulated via this part of itself, perhaps with simplified logic. A region is a buffer in that there is not an extra null-termination byte. Also a copy of a reference is a reference not a separate string. It is ill-behaved in that it is invalidated when the parent goes out of scope, is deleted, or assigned a different value. Value semantics are violated here.
Note: to create a buffer of, say, size 10 use
SS s (SS::Buffer(10)) |
SS s(10) |
When Super String allocates the memory for its representation it always allocates an additional byte for zero termination. Since a non zero terminated Super String can be easily constructed with these methods generalized code to handle Super Strings should not assume zero termination.
SS (Buffer const & b); | construct a Super String via an SS::Buffer object see Supporting Classes currently implemented by template, see Supported Types |
---|---|
SS::Buffer (int n=0, SS const & fillvalue=””); | direct SS (Buffer const & b) to create a string of length n, possibly filled with fillvalue |
SS::Buffer (void const * start, int n); | direct SS (Buffer const & b) to assign start and n to the string’s internal representation see SS::buffer (void const * s, int n) |
SS& buffer (int n); | change string to hold n characters current contents are destroyed; buffer is zero terminated returns the altered string |
SS& buffer (void const * v, int n); | assign v and n to the string’s internal representation no memory is allocated or copied the string will not try to free s when it goes out of scope the string is not guarantied to be zero terminated returns the altered string |
SS& resize (int n); | shrink or expand the string current contents are preserved returns the altered string |
SS& resizeToNullTerminator (); | resize the string, keeping the contents up to the first embedded null |
SS getRegion (int beg=0, int len=fullength) const; SS getRegion (Pair<int> const & beglen) const; std::vector<SS>& getRegion (std::vector<SS>& s, std::vector< Pair<int> > const & beglen) const; |
create a string that is a reference, or region, to a part of its parent beg and len or beglen is the range to which the region refers the last creates a region for each range in beglen |
char* extract (); | take over the memory management for the internal representation the internal rep will not be deleted when the string goes out of scope returns the char* internal rep |
SS& zero (); SS& zero (int beg, int len); |
fill the string or a range with the null character, ‘ |