[RFC v1 1/1] string: Add streq(), memeq(), wcseq(), wmemeq()

Alejandro Colomar alx@kernel.org
Sat Aug 30 18:47:38 GMT 2025


Hi,

On Sat, Aug 30, 2025 at 08:46:10AM +0200, Alejandro Colomar wrote:
> These inline functions serve the most common use case of the comparison
> functions: test for equality.  It avoids the reversed return value that
> confuses programmers.  Using these APIs will result in more readable
> code, which in the end means safer code.

I've built a glibc with these inline functions, and one with these
implemented as macros.  The one with macros results in a smaller binary,
so I guess I should implement them as macros.  Also, that removes the
issue about bool/_Bool.


Have a lovely night!
Alex

> 
> Signed-off-by: Alejandro Colomar <alx@kernel.org>
> ---
>  string/string.h | 21 +++++++++++++++++++++
>  wcsmbs/wchar.h  | 31 +++++++++++++++++++++++++++++++
>  2 files changed, 52 insertions(+)
> 
> diff --git a/string/string.h b/string/string.h
> index df4d489556..7618c209dd 100644
> --- a/string/string.h
> +++ b/string/string.h
> @@ -80,6 +80,16 @@ extern int memcmp (const void *__s1, const void *__s2, size_t __n)
>  extern int __memcmpeq (const void *__s1, const void *__s2, size_t __n)
>       __THROW __attribute_pure__ __nonnull ((1, 2));
>  
> +#ifdef __USE_GNU
> +/* Compare N bytes of M1 and M2 for equality.  */
> +__attribute_pure__ __nonnull ((1, 2))
> +static inline _Bool
> +memeq (const void *__m1, const void *__m2, size_t __n)
> +{
> +  return __memcmpeq(__m1, __m2, __n) == 0;
> +}
> +#endif
> +
>  /* Search N bytes of S for C.  */
>  #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
>  extern "C++"
> @@ -155,6 +165,17 @@ extern char *strncat (char *__restrict __dest, const char *__restrict __src,
>  /* Compare S1 and S2.  */
>  extern int strcmp (const char *__s1, const char *__s2)
>       __THROW __attribute_pure__ __nonnull ((1, 2));
> +
> +#ifdef __USE_GNU
> +/* Compare S1 and S2 for equality.  */
> +__attribute_pure__ __nonnull ((1, 2))
> +static inline _Bool
> +streq (const char *__s1, const char *__s2)
> +{
> +  return strcmp(__s1, __s2) == 0;
> +}
> +#endif
> +
>  /* Compare N characters of S1 and S2.  */
>  extern int strncmp (const char *__s1, const char *__s2, size_t __n)
>       __THROW __attribute_pure__ __nonnull ((1, 2));
> diff --git a/wcsmbs/wchar.h b/wcsmbs/wchar.h
> index b31ca2d241..fe8899867f 100644
> --- a/wcsmbs/wchar.h
> +++ b/wcsmbs/wchar.h
> @@ -129,6 +129,22 @@ extern wchar_t *wcsncat (wchar_t *__restrict __dest,
>  /* Compare S1 and S2.  */
>  extern int wcscmp (const wchar_t *__s1, const wchar_t *__s2)
>       __THROW __attribute_pure__ __nonnull ((1, 2));
> +
> +#ifdef __USE_GNU
> +/* Compare S1 and S2 for equality.  */
> +__attribute_pure__ __nonnull ((1, 2))
> +static inline
> +# ifdef __cplusplus
> +bool
> +# else
> +_Bool
> +# endif
> +wcseq (const wchar_t *__s1, const wchar_t *__s2)
> +{
> +  return wcscmp(__s1, __s2) == 0;
> +}
> +#endif
> +
>  /* Compare N wide-characters of S1 and S2.  */
>  extern int wcsncmp (const wchar_t *__s1, const wchar_t *__s2, size_t __n)
>       __THROW __attribute_pure__ __nonnull ((1, 2));
> @@ -283,6 +299,21 @@ extern wchar_t *wmemchr (const wchar_t *__s, wchar_t __c, size_t __n)
>  extern int wmemcmp (const wchar_t *__s1, const wchar_t *__s2, size_t __n)
>       __THROW __attribute_pure__;
>  
> +#ifdef __USE_GNU
> +/* Compare N wide characters of M1 and M2 for equality.  */
> +__attribute_pure__ __nonnull ((1, 2))
> +static inline
> +# ifdef __cplusplus
> +bool
> +# else
> +_Bool
> +# endif
> +wmemeq (const wchar_t *__m1, const wchar_t *__m2, size_t __n)
> +{
> +  return wmemcmp(__m1, __m2, __n) == 0;
> +}
> +#endif
> +
>  /* Copy N wide characters of SRC to DEST.  */
>  extern wchar_t *wmemcpy (wchar_t *__restrict __s1,
>  			 const wchar_t *__restrict __s2, size_t __n) __THROW;
> -- 
> 2.50.1
> 

-- 
<https://www.alejandro-colomar.es>
Use port 80 (that is, <...:80/>).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20250830/dc703a5e/attachment.sig>


More information about the Libc-alpha mailing list