Wednesday, April 10, 2013

STL String Class

So it's kind of funny, I chose the name for my blog because I'm a software developer, and yet very few posts deal with computer programming.  But today's post is very much about computer programming.

My preferred programming language is C++.  When working in C++ you pretty much need a good string class to help out.  One of my favorite string classes is the MFC class CString.  The downside is it's only available to MFC apps and sometimes you're developing non-MFC apps, or writing for other platforms.  Another common string class is the Standard Template Library (STL) string classes; std::string and std::wstring.  However, I've found the STL string classes to have some major drawbacks.  I have two main problems with these classes.

1)  They implement few useful functions compared to other string classes.  The STL string classes do not offer the ability to A) perform case-insensitive searches and compare, B) format a string like printf, C) lowercase/uppercase a string, or D) tokenize a string using delimiter characters just to name a few.  These are all very common tasks and the STL string class doesn't implement them.

2)  The second problem is a crash bug that shouldn't be.  Take the following code for example
char *sz = NULL;
std::string str(sz);

This code will result in a crash 100% of the time.  If you pass NULL into the constructor of an STL string it will crash.  This shouldn't be.  A string class that accepts pointers should check them for NULL before dereferencing them.

The STL string class has a few benefits.  First, it's pretty much ubiquitous these days.  Every C++ compiler includes a version of the STL library so you can always count on it being there.  Also, whereas it doesn't offer many advanced functions it does offer memory management and the most basic of functions.  So it's better than nothing.

I think what shocks me the most about the STL string classes is how poor they are compared to the rest of the STL.  The STL container classes like vector, list, and queue are extremely well written and I use them a lot.  So I would have expected the string classes to be better than they are.

Even though CString is my preferred class, I actually use a different string class in most of my projects.  Years ago I wrote my own string class, which I've continued to improve over the years.  It is pretty much the best of all the string classes I've come across.  The function names very closely follows that of CString.  I've also profiled the class for performance against both CString and STL.  The STL (because of it's simplicity) is faster than CString.  My class is as fast or faster than the STL.  Plus I've added additional functions I've felt CString was missing such as constructors that perform printf style formats.  So it's the best of both worlds; the speed and performance of the STL string classes, with all of the functions in CString and then some.  I guess the only downside to the class is it calls some Windows APIs and therefore is not cross platform.  But other than that it's a really nice, fast, and stable string class.  I truly wish I could upload this class for everyone to use, but alas I developed it at work which makes it the property of my employer.

No comments:

Post a Comment