Live data from Hacker News

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

github.com

11–20 of 85 posts

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

#11
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/

Yeah, I may have downloaded it from somewhere else, or just grabbed the string routines out of qmail or some other djb code. I tend to trust djb's code more than someone else's reimplementation of the same interface, which is what libowfat is since it is GPL. Anyway, it was several years ago already that I last did so (but later than 2000).

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

#12
This is a bit on the unsafe side since it blindly trusts user input. At minimum, there needs to be some kind of magic number in the struct header to validate its looking at the right memory. Best case, some kind of pointer accounting. Unfortunately, magic doesn't come for free.

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

#15
post #12

This is a bit on the unsafe side since it blindly trusts user input. At minimum, there needs to be some kind of magic number in the struct header to validate its looking at the right memory. Best case, some kind of pointer accounting. Unfortunately, magic doesn't come for free.

We can optimize that, just put a function pointer in the beginning that's called every time to validate the string... /s

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

#17

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 avoidin…

You can "accidentally" do a lot of things that any compiler in the world won't warn you about. This isn't an excuse to not use a good library/tool.

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

#18

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 avoidin…

It is not a real issue in modern days. You just enable address sanitizer in all your debug builds and these issues are obvious at runtime (still not perfect, but an improvement from dark days).

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

#20

UTF support?

Exactly. You should not name a buffer lib "string", when it does not support the basic unicode operations: case fold, normalize => compare, search. In utf-8 of course.

I'm also missing stack allocation support, needed for fast short strings. It should be even included in sdsnew, for len < 128.

Post reply on HN