[RFC v4 2/4] string: Add streq,memeq,wcseq,wmemeq,strcaseeq[_l],wcscaseeq[_l] APIs

Alejandro Colomar alx@kernel.org
Sun Sep 14 06:01:09 GMT 2025


These APIs serve the most common use case of the comparison functions:
test for equality.  They avoid 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  | 11 +++++++++++
 string/strings.h | 11 +++++++++++
 wcsmbs/wchar.h   | 22 ++++++++++++++++++++++
 3 files changed, 44 insertions(+)

diff --git a/string/string.h b/string/string.h
index df4d489556..2a41679b6d 100644
--- a/string/string.h
+++ b/string/string.h
@@ -80,6 +80,11 @@ 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.  */
+#define memeq(m1, m2, n)  ((__bool) !__memcmpeq(m1, m2, n))
+#endif
+
 /* Search N bytes of S for C.  */
 #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
 extern "C++"
@@ -155,6 +160,12 @@ 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.  */
+#define streq(s1, s2)  ((__bool) !strcmp(s1, s2))
+#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/string/strings.h b/string/strings.h
index c5f3aa7d83..91196ce541 100644
--- a/string/strings.h
+++ b/string/strings.h
@@ -116,6 +116,11 @@ __extension__ extern int ffsll (long long int __ll)
 extern int strcasecmp (const char *__s1, const char *__s2)
      __THROW __attribute_pure__ __nonnull ((1, 2));
 
+#ifdef _USE_GNU
+/* Compare S1 and S2 for equality, ignoring case.  */
+#define strcaseeq(s1, s2)  ((__bool) !strcasecmp(s1, s2))
+#endif
+
 /* Compare no more than N chars of S1 and S2, ignoring case.  */
 extern int strncasecmp (const char *__s1, const char *__s2, size_t __n)
      __THROW __attribute_pure__ __nonnull ((1, 2));
@@ -128,6 +133,12 @@ extern int strncasecmp (const char *__s1, const char *__s2, size_t __n)
 extern int strcasecmp_l (const char *__s1, const char *__s2, locale_t __loc)
      __THROW __attribute_pure__ __nonnull ((1, 2, 3));
 
+#ifdef _USE_GNU
+/* Compare S1 and S2 for equality, ignoring case,
+   using collation rules from LOC.  */
+#define strcaseeq_l(s1, s2, l)  ((__bool) !strcasecmp_l(s1, s2, l))
+#endif
+
 /* Compare no more than N chars of S1 and S2, ignoring case, using
    collation rules from LOC.  */
 extern int strncasecmp_l (const char *__s1, const char *__s2,
diff --git a/wcsmbs/wchar.h b/wcsmbs/wchar.h
index b31ca2d241..da095b4a85 100644
--- a/wcsmbs/wchar.h
+++ b/wcsmbs/wchar.h
@@ -129,6 +129,12 @@ 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.  */
+#define wcseq(s1, s2)  ((__bool) !wcscmp(s1, s2))
+#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));
@@ -137,6 +143,11 @@ extern int wcsncmp (const wchar_t *__s1, const wchar_t *__s2, size_t __n)
 /* Compare S1 and S2, ignoring case.  */
 extern int wcscasecmp (const wchar_t *__s1, const wchar_t *__s2) __THROW;
 
+#ifdef _USE_GNU
+/* Compare S1 and S2 for equality, ignoring case.  */
+#define wcscaseeq(s1, s2)  ((__bool) !wcscasecmp(s1, s2))
+#endif
+
 /* Compare no more than N chars of S1 and S2, ignoring case.  */
 extern int wcsncasecmp (const wchar_t *__s1, const wchar_t *__s2,
 			size_t __n) __THROW;
@@ -146,6 +157,12 @@ extern int wcsncasecmp (const wchar_t *__s1, const wchar_t *__s2,
 extern int wcscasecmp_l (const wchar_t *__s1, const wchar_t *__s2,
 			 locale_t __loc) __THROW;
 
+#ifdef _USE_GNU
+/* Compare S1 and S2 for equality, ignoring case,
+   using collation rules from L.  */
+#define wcscaseeq_l(s1, s2, l)  ((__bool) !wcscasecmp_l(s1, s2, l))
+#endif
+
 extern int wcsncasecmp_l (const wchar_t *__s1, const wchar_t *__s2,
 			  size_t __n, locale_t __loc) __THROW;
 #endif
@@ -283,6 +300,11 @@ 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.  */
+#define wmemeq(s1, s2, n)  ((__bool) !wmemcmp(s1, s2, n))
+#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