>
This is achieved using an alternative design in which instead of using a C structure to represent a string, we use a binary prefix that is stored before the actual pointer to the string that is returned by SDS to the user.I'm not convinced of the advantage in returning a char* to the user.
I see how it's convenient to be able to use all the built-in and other functions that accept a char* as an argument, like printf() or your favorite logging library.
BUT, you can only use the ones that treat the string as read-only. (You can't use strncat(), etc.) And you have no protection against messing this up.
Seems like a better trade-off would be to have a user-visible type that isn't char, then a user-visible function that converts that to char when you need it.
So instead of this:
/* sds is just a typedef to char* */
sds mystring = sdsnew("Hello World!");
printf("%s\n", mystring);
sdsfree(mystring);
You'd have a function like this:
/* get read-only C-style string from an sds */
const char *sdsC(const sds s);
And code like this:
/* sds is its own distinct type, not another name for char* */
sds mystring = sdsnew("Hello World!");
printf("%s\n", sdsC(mystring));
sdsfree(mystring);
Yes, it's more keystrokes, but surely the safety is worth it considering it is only needed when bridging a compatibility gap. (Also, possibly the sds functions could be a tiny bit more efficient if they aren't always doing conversions on their arguments.)
(I do like the idea of putting header and characters into one struct. That's probably good for efficiency compared to a struct that points to a buffer and gives the system a layer of pointer indirection to go through.)