4 ms·
The SDS library (http://github.com/antirez/sds http://github.com/antirez/sds) is the middle ground between the two approaches. SDS strings are apparently C null
by antirez 11y ago
The SDS library (http://github.com/antirez/sds http://github.com/antirez/sds) is the middle ground between the two approaches. SDS strings are apparently C null-terminated strings, but before the pointer that you pass around there is metadata with the length. This makes SDS strings compatible with everything there is in the C standard, but has allows to easily manipulate the string without managing the allocation manually. Also sdslen() is O(1) and binary safe.
The main drawback is that most functions have to reassign the pointer back, like in:
sds foo = sdsnew("foo"); // Creates an SDS string
printf("%s\n", foo); // You can print it with printf()
foo = sdscatlen(foo,buffer,10); // Append 10 bytes.
Failing to reassign sdscatlen() return value back to "foo" creates a bug. SDS originated with Redis but is now a standalone library. Recently version 2.0 was released that makes it synchronized with the version we have inside Redis.
- JoshTriplett 11y agoSeems like you could avoid that potential for error by making functions that modify an SDS take &foo rather than just foo, and modify it in place. As a stopgap, though, SDS could use GCC's attribute "warn_unused_result" on all of those return values.
- jzwinck 11y agoLast time I tried it, that GCC attribute was only implemented for C, not C++. It seems like a good idea anyway.
- tonyedgecombe 11y agoI seem to remember BSTR in COM is similar to this.