Live data from Hacker News

Simple Dynamic Strings library for C, compatible with null-terminated strings

github.com

1–10 of 85 posts

Re: Simple Dynamic Strings library for C, compatible with null-terminated strings

#3
post #2

I rarely work with C but when I do I am always reminded at how painful it is to use strings. Is this a complete drop-in replacement?? If so it could occasionally make my life much easier!

SDS works by allocating a chunk of memory that can fit a header + your string data. The APIs all consume pointers and internally it mutates it's copy of the pointer so it can get at the metadata. So it can be used to generate strings that are passed to other things. I don't think it's easy to use sds operations on other strings in a zero-copy way.

Re: Simple Dynamic Strings library for C, compatible with null-terminated strings

#4
post #2

I rarely work with C but when I do I am always reminded at how painful it is to use strings. Is this a complete drop-in replacement?? If so it could occasionally make my life much easier!

You might look at the Linux version of CFLite, the Apple reference counted C library. Has other things than just strings which would be useful to a C programmer.

https://github.com/fjolnir/CoreFoundation-Lite-Linux

Re: Simple Dynamic Strings library for C, compatible with null-terminated strings

#5
post #3
post #2

I rarely work with C but when I do I am always reminded at how painful it is to use strings. Is this a complete drop-in replacement?? If so it could occasionally make my life much easier!

SDS works by allocating a chunk of memory that can fit a header + your string data. The APIs all consume pointers and internally it mutates it's copy of the pointer so it can get at the metadata. So it can be used to generate strings that are passed to other things. I don't think it's easy to use sds operations on other strings in a zero-copy way.

Reminds me of libowfat [0]'s stralloc/buffer. It's used in gatling[1], a small (LOC/binary) http/ftp/samba server simple nonblocking IO.

[0]: https://www.fefe.de/libowfat/ [1]: https://www.fefe.de/gatling/

Re: Simple Dynamic Strings library for C, compatible with null-terminated strings

#6
post #2

I rarely work with C but when I do I am always reminded at how painful it is to use strings. Is this a complete drop-in replacement?? If so it could occasionally make my life much easier!

This alternative (by me) covers char, int, etc with a compiletime inlined generic api much the same way SDS does strings

https://tse.gratis/aArray/

aStr("string"); then aMap aFold aConcat, etc, but aAppend(&array,'\0') needs to be done manually if passing to standard str functions. This is so long* or whatever arrays can have the same interface

Re: Simple Dynamic Strings library for C, compatible with null-terminated strings

#9
post #7

I tend to use libdjb[0] if I need to use strings in C. Is this library significantly easier? [0] http://www.fefe.de/djb/

from the top of that page:

  (Note: This has not been touched since 2000, use [2] instead) 
[1] https://www.fefe.de/libowfat/

Re: Simple Dynamic Strings library for C, compatible with null-terminated strings

#10
Just looking at the API "sds" seems to just be a typedef for char* - unfortunately, that means that accidentally passing a char* as an sds into any of the functions will be instant UB and not even a compiler warning.

Considering this is C, there is no way to prevent this easily since you can't express a type that is one-way convertible (i.e. sds -> char* ok, char* -> sds not ok).

I do have to wonder though if avoiding a couple of .str's is really worth the risk. You definitely have to always keep in mind to never accidentally mix a char* with an sds, for the advantage of slightly cleaner looking code.

Post reply on HN