[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: RFC: add a string-desc module
From: |
Bruno Haible |
Subject: |
Re: RFC: add a string-desc module |
Date: |
Sat, 25 Mar 2023 12:49:57 +0100 |
Jeffrey Walton wrote:
> A natural thing to want
> to do is print a string, and C-based routines usually expect a
> terminating NULL.
I'll add a comment regarding printf with the "%.*s" directive.
> Also, if you initialize the struct, then the allocated string will
> likely include a terminating NULL. I understand the size member will
> omit the NULL, but it will be present anyways in the string.
No; it depends where the 'char *' comes from. If it is a pointer into
a piece of memory read through read_file, for example, there will be
no NUL terminator.
Also, in C you can write
char buf[4] = "abcd";
which does not add a NUL.
> A length prefixed string may be a good idea.
https://github.com/antirez/sds does it like this. But again, this
does not allow for an allocation-free substring function.
> So if you are going to add the "string descriptor", then I hope you
> add some functions to make it easier for less experienced folks to
> write safer code.
I believe all these functions are already in the proposal.
> Also see libbsd's stringlist.h for some inspiration,
> https://cgit.freedesktop.org/libbsd/tree/include/bsd/stringlist.h .
This is unrelated, AFAICS. It's not about a string, but about an
extensible array of strings.
Bruno