[RFC v4 0/4] string: Addd equality APIs
Alejandro Colomar
alx@kernel.org
Sun Sep 14 06:00:55 GMT 2025
Hi!
I've changed them to be just macros. This avoids the issues with
static inline, which is not usable within extern inline. Another option
would be providing them as extern inline, but the benefit of macros is
that they're much simpler to implement, and we don't yet commit to any
ABI.
Have a lovely day!
Alex
Alejandro Colomar (4):
cdefs: Add __bool
string: Add streq,memeq,wcseq,wmemeq,strcaseeq[_l],wcscaseeq[_l] APIs
manual: Document streq,memeq,wcseq,wmemeq,strcaseeq,wcscaseeq APIs
tests-mbwc/tst_funcs.h: Fix typo
localedata/tests-mbwc/dat_wcscmp.c | 2 +-
manual/string.texi | 176 +++++++++++++++++++++++++++--
misc/sys/cdefs.h | 6 +
string/string.h | 11 ++
string/strings.h | 11 ++
wcsmbs/wchar.h | 22 ++++
6 files changed, 219 insertions(+), 9 deletions(-)
Range-diff against v3:
1: a921e4e1d2 = 1: a921e4e1d2 cdefs: Add __bool
2: c9f321b3d5 ! 2: c1c9b1d282 string: Add streq,memeq,wcseq,wmemeq,strcaseeq[_l],wcscaseeq[_l] APIs
@@ Metadata
## Commit message ##
string: Add streq,memeq,wcseq,wmemeq,strcaseeq[_l],wcscaseeq[_l] APIs
- 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.
+ 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: extern int memcmp (const void *__s1, const void *__s2, size_t _
+#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;
-+}
++#define memeq(m1, m2, n) ((__bool) !__memcmpeq(m1, m2, n))
+#endif
+
/* Search N bytes of S for C. */
@@ string/string.h: extern char *strncat (char *__restrict __dest, const char *__re
+
+#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;
-+}
++#define streq(s1, s2) ((__bool) !strcmp(s1, s2))
+#endif
+
/* Compare N characters of S1 and S2. */
@@ string/strings.h: __extension__ extern int ffsll (long long int __ll)
+#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;
-+}
++#define strcaseeq(s1, s2) ((__bool) !strcasecmp(s1, s2))
+#endif
+
/* Compare no more than N chars of S1 and S2, ignoring case. */
@@ string/strings.h: extern int strncasecmp (const char *__s1, const char *__s2, si
+#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;
-+}
++#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
@@ wcsmbs/wchar.h: extern wchar_t *wcsncat (wchar_t *__restrict __dest,
+
+#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;
-+}
++#define wcseq(s1, s2) ((__bool) !wcscmp(s1, s2))
+#endif
+
/* Compare N wide-characters of S1 and S2. */
@@ wcsmbs/wchar.h: extern int wcsncmp (const wchar_t *__s1, const wchar_t *__s2, si
+#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;
-+}
++#define wcscaseeq(s1, s2) ((__bool) !wcscasecmp(s1, s2))
+#endif
+
/* Compare no more than N chars of S1 and S2, ignoring case. */
@@ wcsmbs/wchar.h: extern int wcsncasecmp (const wchar_t *__s1, const wchar_t *__s2
+#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;
-+}
++ 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,
@@ wcsmbs/wchar.h: extern wchar_t *wmemchr (const wchar_t *__s, wchar_t __c, size_t
+#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;
-+}
++#define wmemeq(s1, s2, n) ((__bool) !wmemcmp(s1, s2, n))
+#endif
+
/* Copy N wide characters of SRC to DEST. */
3: ab46eb8684 = 3: 42712ccbd2 manual: Document streq,memeq,wcseq,wmemeq,strcaseeq,wcscaseeq APIs
4: 1d701575a0 = 4: 81720f64aa tests-mbwc/tst_funcs.h: Fix typo
--
2.50.1
More information about the Libc-alpha
mailing list