String handling

Bron Gondwana brong at fastmail.fm
Thu Mar 19 17:46:49 EDT 2009


So we have a stack of interfaces that take a character
pointer, a length - sometimes even a malloced length,
and have reallocation possibilities.  If you want to
pass back the length you realloced to, you need to
pass _three_ different variables to the function like
so:

int f(char *base, int *len, int *alloc, ...)

Plus whatever else you actually need f() to do.

... and then you have to figure out who's responsible
for freeing it!

Now, I don't know that many C string libraries - I'm
spoilt by spending a lot of my time in Perl where
string management is handled very nicely thank-you-
very-much.

But - I wound up writing a very simple string library
just for the charset stuff's internal 'buffer'
structure:

struct buffer_state {
    unsigned char *base;
    size_t offset;
    size_t alloced;
};

And functions to manipulate that.  Obviously a more
general string library would have a flags item as
well, with at least BUF_ISREADONLY, BUF_CANREALLOC,
BUF_NEEDFREE and maybe even BUF_ISUTF8 if we wanted
to get fancy.

But before I go generalise one of these and start
rewriting interfaces to use it, does anybody know
of a good lightweight, compatible licence string
library?  I particularly want to make the final
consumer of functions responsible for both 
allocating and freeing the wrapper datastructure,
so you can pass them down with impunity and not
have to worry about freeing them in every error
case.

(I note that timsieved already has a 'mystring'
datatype, which is particularly lightweight, but
doesn't have all the properties I want - in
particular it doesn't support "slop", or any sort
of append at all for that matter!)

Bron.


More information about the Cyrus-devel mailing list