A Super String Class
Posted
by Phil Haubert
on April 27th, 1999
Yet another string class?
This class got its start a number of years ago. At that time, it was common to have to write even the simplest data structure class. Since then it has gradually grown and mutated at irregular intervals. The result is fairly ambitious, for a string class.Super String features string and buffer operations, conversions and comparisons, extensible searching, and C compatibility.
The complete documentation is here.
Here is the header, implementation, and example code. These are marked-up html files and so are fairly large.
Examples
Here is some code to demo the various parts of the class:
{
// assign a string some values
SS ss ("hello");
// the new value is "65" not "A"
ss = 65;
// the new value is "65c"
ss += 'c';
}
{
// allocate a buffer
SS ss (SS::Buffer(6));
// indexing, length() and size() are the same
for (int i=0; i<ss.length(); i++) ss[i] = 'a' + i;
// substring, the current value is "abcdef"
ss (2,3) == "cde";
// the new value is "ab12f"
ss (2,3) = "12";
// the new value is "abxyf"
ss.set ("xy", 2);
// the new value is "abf", "xy" is returned
ss.cut (2, 2) == "xy";
// now back to "abcdef"
ss.paste ("cde", 2);
// fill with x's, "xxxxxx"
ss.fill ('x');
}
{
// comparisons
SS ss(12);
// 12 is less than 13
ss < 13;
ss = "one";
// "three" is greater than "one"
"three" >= ss;
}
{
// searching
SS ss ("The next one");
// find the word "next"
int p = ss.findNext ("next");
// can't find the word "first"
p = ss.findNext ("first"); // p == SS::notfound
// find the first whitespace character
p = ss.findNext (SS::whitespace);
// find the next whitespace character
p = ss.findNext (SS::whitespace, p + 1);
// find the word "one"
SS result;
ss.findNext (SS::FindOneOrMore (SS::blackspace), p, result);
result == "one";
// ss contains "The"
bool b = ss.contains ("The"); // b == true
// there are 9 lowercase characters
int n = ss.population (SS::lowercase); // n == 9
// remove whitespace, ss is now "Thenextone"
ss.remove (SS::whitespace);
// replace the 'T', ss is now "thenextone"
ss.replace (SS::uppercase, "t");
std::cout << "the string is \"" << ss << "\"" << std::endl;
}
{
// numeric
SS ss ("100");
// extract the number 100
long v = ss.toLong();
// create a base 16 string
SS::toBase (v, 16) == "64";
}
{
// matching
SS ss ("theOne1 theTwo2 theThree3");
// an empty vector for results
// each element will be a position and a length
SS::BegLenVect result;
// get positions of all the "the"s
ss.match ("the", result);
// reverse the "the"s
ss.reverse (result) == "ehtOne1 ehtTwo2 ehtThree3";
// remove the now reversed "the"s
ss.removeRange (result) == "One1 Two2 Three3";
// break up into tokens, reusing result
ss.tokenize (result);
// change string to "One1 Two2 Four4"
ss (result[2]) = "Four4";
// get a vector with the digits
ss.match (SS::digit, result);
// cut the '4'
ss.cut (result[2]);
// remove the '2' by limiting the remove range
ss.remove (SS::digit, result[1]);
// remove the 1 via an 'X'
ss.fill ('X', result[0]).remove ('X') == "One Two Four";
std::cout << "the string is \"" << ss << "\"" << std::endl;
}
CString compatibility
Support for additional types can be added externally to the class. A header file, "QString.h", is provided to supply compatibility for CString. To allow a CString to convert to a Super String:
typedef SS QString;
inline void SSconvert (CString const & u, QString & w)
{
w.assign ((char const *)u, u.GetLength());
}
A Super String can convert to a CString via operator char*. To allow relational comparisons, e.g. ==, >, etc. define:
inline int SScompare (QString const & u, CString const & w)
{
return u.compare((char const *)w,w.GetLength());
}
Portability
The source and test code have been compiled with VC6.0, sp2. The only things Microsoft-specific should be the numeric type __int64, often referred to as long long, and _vsnprintf, a variant of sprintf.

Comments
Nothing but errors.
Posted by Legacy on 03/18/2003 12:00amOriginally posted by: Nathan M.
ReplyLink error with eVC
Posted by Legacy on 08/08/2002 12:00amOriginally posted by: Terry
First when I look at this SuperClass I found that it is very useful, so I am trying to apply it to my C program written in Embeded Visual C. I understand eVC is not 100% compatable to VC, would you help me to address these problem.
Compiling resources...
Compiling...
StdAfx.cpp
Compiling...
TestString.cpp
TestStringDlg.cpp
Generating Code...
Linking...
TestStringDlg.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall SS::~SS(void)" (??1SS@@UAE@XZ) referenced in function "protected: void __thiscall CTestStringDlg::OnButton1(void)" (?OnButton1@CTestStringDlg@@IAEXXZ)
TestStringDlg.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall SS::destroy(void)" (?destroy@SS@@MAEXXZ)
TestStringDlg.obj : error LNK2019: unresolved external symbol "void __cdecl SSconvert(char const *,class SS &)" (?SSconvert@@YAXPBDAAVSS@@@Z) referenced in function "public: __thiscall SS::SS(char const (&)[6])" (??0SS@@QAE@AAY05$$CBD@Z)
TestStringDlg.obj : error LNK2019: unresolved external symbol "protected: void __thiscall SS::_zero(void)" (?_zero@SS@@IAEXXZ) referenced in function "public: __thiscall SS::SS(char const (&)[6])" (??0SS@@QAE@AAY05$$CBD@Z)
X86Dbg/TestString.exe : fatal error LNK1120: 4 unresolved externals
Error executing link.exe.
TestString.exe - 5 error(s), 0 warning(s)
ReplyEfficient code
Posted by Legacy on 11/25/2001 12:00amOriginally posted by: Haribaskar
Code is written in an efficient way
ReplyWon't Compile
Posted by Legacy on 02/04/2000 12:00amOriginally posted by: Jeff WIlliams
I can't get my code to compile properly with the SuperString added in
when I try to compile SuperString.cpp I get the following error:
c:\program files\microsoft visual studio\vc98\include\superstr.cpp(22) : warning C4005: 'ErrorMacro' : macro redefinition
c:\program files\microsoft visual studio\vc98\include\superstr.cpp(20) : see previous definition of 'ErrorMacro'
Replyany ideas on what I'm doing wrong?
Great job!
Posted by Legacy on 09/09/1999 12:00amOriginally posted by: Rick Lawson
Using this class heavily in an industrial hardware interface program. Handles all the string chores (coming from the GUI) very nicely.
Reply