dcsimg

A Super String Class

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.

To do

Possible performance enhancements, UNICODE support.

Download source + doc + demo proj + test code - 62 KB

Download source only - 24 KB



This article was originally published on April 27th, 1999

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date