[RFC v3 2/4] string: Add streq,memeq,wcseq,wmemeq,strcaseeq[_l],wcscaseeq[_l] APIs
Alejandro Colomar
alx@kernel.org
Wed Sep 3 10:05:55 GMT 2025
These inline functions 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 | 21 +++++++++++++++++++++
string/strings.h | 21 +++++++++++++++++++++
wcsmbs/wchar.h | 42 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 84 insertions(+)
diff --git a/string/string.h b/string/string.h
index df4d489556..e9197883bb 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 __always_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 __always_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/string/strings.h b/string/strings.h
index c5f3aa7d83..9a256b7634 100644
--- a/string/strings.h
+++ b/string/strings.h
@@ -116,6 +116,16 @@ __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. */
+__attribute_pure__ __nonnull ((1, 2))
+static __always_inline __bool
+(strcaseeq) (const char *__s1, const char *__s2)
+{
+ return strcasecmp(__s1, __s2) == 0;
+}
+#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 +138,17 @@ 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. */
+__attribute_pure__ __nonnull ((1, 2, 3))
+static __always_inline __bool
+(strcaseeq_l) (const char *__s1, const char *__s2, locale_t __loc)
+{
+ return strcasecmp_l(__s1, __s2, __loc) == 0;
+}
+#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..4f28130e46 100644
--- a/wcsmbs/wchar.h
+++ b/wcsmbs/wchar.h
@@ -129,6 +129,17 @@ 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 __always_inline __bool
+(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));
@@ -137,6 +148,16 @@ 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. */
+__attribute_pure__ __nonnull ((1, 2))
+static __always_inline __bool
+(wcscaseeq) (const wchar_t *__s1, const wchar_t *__s2)
+{
+ return wcscasecmp(__s1, __s2) == 0;
+}
+#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 +167,17 @@ 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 LOC. */
+__attribute_pure__ __nonnull ((1, 2, 3))
+static __always_inline __bool
+(wcscaseeq_l) (const wchar_t *__s1, const wchar_t *__s2, locale_t __loc)
+{
+ return wcscasecmp_l(__s1, __s2, __loc) == 0;
+}
+#endif
+
extern int wcsncasecmp_l (const wchar_t *__s1, const wchar_t *__s2,
size_t __n, locale_t __loc) __THROW;
#endif
@@ -283,6 +315,16 @@ 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 __always_inline __bool
+(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