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

Alejandro Colomar alx@kernel.org
Sat Aug 30 06:46:10 GMT 2025


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.

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



More information about the Libc-alpha mailing list