Abi string class


Subject: Abi string class
From: Mike Nordell (tamlin@algonet.se)
Date: Thu Feb 01 2001 - 00:50:44 CST


Well, I've now taken the few hours it took to assemble something that at
least resembles a string class to use in AbiWord.

Why?

None of the existing string classes I've seen are fit for use in AW.
std::string is a no-no since it's using templates, exceptions and the world
is still full of bad implementations. For us to have a string class it need
to match at least the following criteria:

- Be completely portable. That means only ANSI C and a small subset of C++.
- Not use new/delete since we don't allow exceptions.
- Not use new(std::nothrow) since ancient libraries don't support it.
- Not use the bool type, portability.

and some others that get lost with these major design restrictions.

This class is currently a very light wrapper around a plain char*, but I
have tried to make it as efficient and exception safe as possible.
Unfortunately there's a tradeof between semantics/syntax of its use, and a
completely "safe" implementation. We want to be able to write

    UT_String s1("foo");
or
    s1 = "bar";

but to do this we would have to use exceptions to be "safe". Since we can't
use exceptions this implementation is "somewhat" unsafe (it keeps a safe
internal state, but in case of out-of-memory conditions it keeps its old
value). To fix this we would have to forbid anything but the default
constructor, and we would have to write assignment as

    if (!s1.assign("bar"))

I traded safety for readability. If this turns out to be an issue of
controversy I'll do it the "safe" way, but it would hurt readability much
IMO.

I also have some ideas of an XML_Char class if this one turns out to be
useful. Such a class could replace more than a few wasteful static buffers
we currently have that most of the time aren't even half-full.

I've written test cases, and it passes all of'em. But, I'd like your input
on the interface.

class UT_String
{
public:
#ifdef UT_STRING_FOR_ABIWORD
 typedef UT_Bool UT_Str_Bool;
#else
 typedef bool UT_Str_Bool;
#endif

 UT_String();
 UT_String(const char* sz);
 UT_String(const UT_String& rhs);
 ~UT_String();
 UT_String& operator=(const UT_String& rhs);
 UT_String& operator=(const char* rhs);
 UT_String& operator+=(const UT_String& rhs);
 UT_String& operator+=(const char* rhs);

 size_t size() const;
 UT_Str_Bool empty() const;

 UT_Str_Bool operator==(const UT_String& rhs) const;
 UT_Str_Bool operator==(const char* rhs) const;

 // The returned pointer is only valid until the
 // next UT_String non-const operation.
 const char* c_str() const;

private:
 class UT_StringImpl* pimpl;
};

Comments, ideas or criticism?

/Mike
P.S.
Wether accepted as-is or with changes, I plan to post the class with its
test cases for other platforms to rey before committing anything.



This archive was generated by hypermail 2b25 : Thu Feb 01 2001 - 00:49:32 CST