commit 32c148f21e31e4db0d6f05298c8f5b2d2ad7d9c1
parent f82bc6684d7c33065ce616b79528e7a0e439d1df
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date: Tue, 25 Mar 2025 19:15:00 +0100
libc/wchar: Add wmemcmp()
Diffstat:
5 files changed, 45 insertions(+), 0 deletions(-)
diff --git a/src/libc/objs/common-objs.mk b/src/libc/objs/common-objs.mk
@@ -135,5 +135,6 @@ COMMON_OBJS =\
wchar/wcwidth.$O\
wchar/wmemchr.$O\
wchar/wmemcpy.$O\
+ wchar/wmemcmp.$O\
wchar/putwc.$O\
wchar/_validutf8.$O\
diff --git a/src/libc/wchar/wmemcmp.c b/src/libc/wchar/wmemcmp.c
@@ -0,0 +1,17 @@
+#include <wchar.h>
+
+#undef wmemcmp
+
+int
+wmemcmp(const wchar_t *s, const wchar_t *t, size_t n)
+{
+ unsigned long ls, lt;
+
+ for (; n > 0 && *s == *t; --n)
+ ++s, ++t;
+ if (n == 0)
+ return 0;
+
+ ls = *s, lt = *t;
+ return ls - lt;
+}
diff --git a/tests/libc/execute/.gitignore b/tests/libc/execute/.gitignore
@@ -47,4 +47,5 @@
0048-wcscpy
0049-wmemchr
0050-wmemcpy
+0053-wmemcmp
test.log
diff --git a/tests/libc/execute/0053-wmemcmp.c b/tests/libc/execute/0053-wmemcmp.c
@@ -0,0 +1,25 @@
+#include <assert.h>
+#include <stdio.h>
+#include <wchar.h>
+
+/*
+output:
+testing
+done
+end:
+*/
+
+int
+main(void)
+{
+ wchar_t buf[40] = {1, 2, 3, 4, 5};
+
+ puts("testing");
+ assert(wmemcmp(buf, (wchar_t[]) {1, 2, 3, 4, 5}, 5) == 0);
+ assert(wmemcmp(buf, (wchar_t[]) {1, 1, 1, 1, 1}, 5) > 0);
+ assert(wmemcmp(buf, (wchar_t[]) {1, 3, 1, 1, 1}, 5) < 0);
+ assert(wmemcmp(buf, (wchar_t[]) {2, 3, 4, 5, 6}, 0) == 0);
+ puts("done");
+
+ return 0;
+}
diff --git a/tests/libc/execute/libc-tests.lst b/tests/libc/execute/libc-tests.lst
@@ -46,3 +46,4 @@
0048-wcscpy
0049-wmemchr
0050-wmemcpy
+0053-wmemcmp