Hi Paul,
On 2026-02-20T15:47:36-0800, Paul Eggert wrote:
> On 2026-02-20 15:34, Alejandro Colomar wrote:
> > So, you mean something like this?
> >
> > INLINE char *
> > (strnul)(const char *s)
> > {
> > return strchr(s, '\0');
> > }
> >
> > #define strnul(s) strchr(s, '\0')
>
> Sort of, but not exactly; that implementation would have multiple problems.
>
> I suggest looking how glibc implements the qualifier-generic strchr macro
> for C, and doing something similar. See glibc commit
> cd748a63ab1a7ae846175c532a3daab341c62690 dated 2025-11-20.Ahh, you want to implement it as a const-generic API. We could do what I did for musl. <https://www.openwall.com/lists/musl/2026/01/08/3> #define __QVoidptrof(p) typeof(1?(p):(void*){0}) #define __QCharptrof(s) typeof \ ( \ _Generic((__QVoidptrof(s)){0}, \ const void *: (const char *) 0, \ void *: (char *) 0 \ ) \ ) #if __STDC_VERSION__ >= 202311L # define memchr(p, chr) ((__QVoidptrof(p)) memchr(p, chr)) # define strchr(s, chr) ((__QCharptrof(s)) strchr(s, chr)) # define strrchr(s, chr) ((__QCharptrof(s)) strrchr(s, chr)) # define strpbrk(s, chrs) ((__QCharptrof(s)) strpbrk(s, chrs)) # define strstr(s, str) ((__QCharptrof(s)) strstr(s, str)) #endif I find it quite more readable than the glibc implementation. BTW, the it doesn't really need __STDC_VERSION__ >= 202311L. This works all the way back to C11, AFAIK. However, shouldn't that be done in a separate patch? I think it would be more reasonable to transform all string APIs to be const-generic at once, in a patch that doesn't introduce any new APIs. Feel free to take (and adapt as necessary) my code above and include Co-authored-by: Alejandro Colomar <[email protected]> in the commit message. Cheers, Alex -- <https://www.alejandro-colomar.es>
signature.asc
Description: PGP signature
