3 ms·
It would be a pity not to mention here the Simple Dynamic String (SDS) library made by the maker of Redis : https://github.com/antirez/sds https://github.com/an
by cassepipe 6y ago
It would be a pity not to mention here the Simple Dynamic String (SDS) library made by the maker of Redis : https://github.com/antirez/sds https://github.com/antirez/sds
It is also very well documented. And all you need to embed it in your project is :
sds.c
sds.h
sdsalloc.h
The source code is small and every C99 compiler should deal with it without issues.
- lifthrasiir 6y agoIt seems that everyone implementing their own string library (including, eh, antirez) thinks masquerading pointers is cute, but in my opinion and experience it's very dangerous because it requires a specific coding convention that can't be checked by compilers. SDS is no exception to this problem: sds a = sdsnew("hell"); sds b = a; a = sdscat(a, "o"); // this invalidates b Masqueraded pointers are inherently linear (or affine if you are pedantic). Any length-changing updates to such pointers can potentially reallocate them, so any value can't be "updated" more than once; values should be consumed and returned by many operations. No typical C types behave like this: primitive values or structs can be updated by assignments and pointers can be updated by dereference. C doesn't support linear types and, while normal pointers do need care, masqueraded pointers need much more care to use correctly. Yes, you can replicate the same bug with normal pointers by replacing the third like to `free(a);`, but you wouldn't expect a bug for non-destructive operations. (Put in the other way, masqueraded pointers make many otherwise non-destructive operations destructive.) While technically not a string library, this and the strict-aliasing issue for type-generic routines prompted me to write my own small extensible vector library [1] years ago. [1] https://gist.github.com/lifthrasiir/4422136 https://gist.github.com/lifthrasiir/4422136