Hi Jonny, Segher,
On 7/9/21 3:54 PM, Jonny Grant wrote:
Yes, this could work. But it does rely on programmer typing it like that every time... Maybe an inline function better.
I agree on that.
inline size_t safestrlen(const char * s) {return s?strlen(s) : 0}
Perhaps there are too many email addresses on this cc list now.
I'd prefer a Annex K of C11 style function ISO/IEC TR 24731-1 for strlen() - but there isn't one such as strnlen_s.
Please, consider not calling some function safesomething() or similar,
as it isn't 100% safe. It's like calling some thing "the new X". How
will you call the next version? "the nova X"? And the next? "the
supernew X"?
As I said before, unsigned types are unsafe, you may want to accept it
or not, but they are.
Now, the day you realize that and develop an even safer function that
doesn't use unsigned size_t, what will you call it? supersafestrlen()?
Use names that define your functions as closely as possible.
On 09/07/2021 00:49, Segher Boessenkool wrote:
wherever you need it. If a function name isn't self-explanatory, and
Agree on this
even *cannot* be, your factoring is most likely not ideal. Code is
But not on this.
You could call it strlennull(), that is, a strlen() that special-cases
NULL. I find it a good enough name, as long as you document your function.
It saves the problem of repeating yourself every time.
primarily there for humans to read, it should be optimised for that.
Agree on this again, but I think the following is readable:
len = strlennull(maybenull);
Regards,
Alex
--
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/