Hi Bruno,

On 2026-02-21T10:38:08+0100, Bruno Haible wrote:
> Paul Eggert wrote:
> > Although the function should be there, it cannot be 
> > const-generic in C.
> 
> Hmm. How useful would a function pointer to a function be,
> if it has a different signature than if the function was invoked
> directly? In this case, I'd prefer to not define the function
> pointer at all.

Functions can not be const-generic in C.  The glibc implementation
similarly only works for function calls but not function pointers.

> 
> > Although in some cases this wouldn't be quite as efficient as what I 
> > suggested earlier, it would be qualifier-generic if strchr is, and it 
> > would be simpler to implement.
> 
> strchr is a red herring, because this way to define strnul is compiled
> less efficiently than (s + strlen (s)). See attached example code.

Not really.  At -O1, both GCC and Clang produce the same exact code:

        alx@devuan:~/tmp$ diff -u strnul?.c
        --- strnul1.c   2026-02-21 14:12:35.465033923 +0100
        +++ strnul2.c   2026-02-21 14:17:36.099834548 +0100
        @@ -2,5 +2,5 @@
         char *
         strnul(const char *s)
         {
        -       return strchr(s, '\0');
        +       return (char *) s + strlen(s);
         }
        alx@devuan:~/tmp$ gcc -S -O1 strnul?.c
        alx@devuan:~/tmp$ diff -u strnul?.s
        --- strnul1.s   2026-02-21 14:17:43.715899893 +0100
        +++ strnul2.s   2026-02-21 14:17:43.727899996 +0100
        @@ -1,4 +1,4 @@
        -       .file   "strnul1.c"
        +       .file   "strnul2.c"
                .text
                .globl  strnul
                .type   strnul, @function
        alx@devuan:~/tmp$ clang -S -O1 strnul?.c
        alx@devuan:~/tmp$ diff -u strnul?.s
        --- strnul1.s   2026-02-21 14:17:49.955953457 +0100
        +++ strnul2.s   2026-02-21 14:17:49.975953628 +0100
        @@ -1,4 +1,4 @@
        -       .file   "strnul1.c"
        +       .file   "strnul2.c"
                .text
                .globl  strnul                          # -- Begin function 
strnul
                .p2align        4

> The better way is thus (s + strlen (s)), and since it evaluates s twice,
> it means we need an inline function, not a macro. But we have the machinery
> for inline functions in Gnulib.
> 
> I'm committing this implementation:

Please reconsider.


Have a lovely day!
Alex

-- 
<https://www.alejandro-colomar.es>

Attachment: signature.asc
Description: PGP signature

Reply via email to