Alejandro Colomar <[email protected]> writes:

> Hi,
>
> I think it would be interesting to add a new strnul() macro.

+1

> I've been using it for some time in shadow-utils, and it helps simplify
> quite some code here in gnulib.  It's the obvious s+strlen(s), without
> repeating 's', which is error-prone.

I've often used the strchr(s, '\0') pattern.

Some packages (e.g., systemd) uses 'strnull'.  I find that worse since
\0 is ASCII NUL and not NULL.

I think this is an important enough case to warrant a readable idiom,
and I think its naming fits nicely between the existing 'strchr' and
'strchrnul'.

/Simon



> I've implemented it as
>
>       #define strnul(s)  strchr(s, '\0')
>
> which allows me to implement it as a trivial macro.  gcc -O1 optimizes
> this as if it were a simple strlen(3) call.
>
> This is just a draft, to discuss the idea.  I think the simplification
> is similar to what we gained with streq(3) recently, so I expect you'll
> like it.  I haven't written the build-system code yet.
>
> What do you think?
>
> BTW, this change has allowed me to see some code that could get help
> from some other simplifications.  I'll work on those after we finish
> with this.
>
>
> Have a lovely night!
> Alex
>
>
> Alejandro Colomar (1):
>   lib/: Implement strnul()
>
>  lib/argz.c               |  2 +-
>  lib/cpu-supports.c       |  2 +-
>  lib/csharpcomp.c         | 17 +++++------------
>  lib/fnmatch.c            |  2 +-
>  lib/glob.c               |  2 +-
>  lib/link.c               |  2 +-
>  lib/localename-unsafe.c  |  2 +-
>  lib/mbspcasecmp.c        |  2 +-
>  lib/opendir.c            |  2 +-
>  lib/parse-duration.c     |  6 +++---
>  lib/relocatable.c        |  6 +++---
>  lib/setlocale.c          |  2 +-
>  lib/strftime.c           |  2 +-
>  lib/striconv.c           |  2 +-
>  lib/string.in.h          |  2 ++
>  lib/strncat.c            |  2 +-
>  lib/term-style-control.c |  6 +++---
>  lib/vasnprintf.c         |  2 +-
>  lib/vc-mtime.c           |  4 ++--
>  19 files changed, 31 insertions(+), 36 deletions(-)

Attachment: signature.asc
Description: PGP signature

Reply via email to