commit 01921365fc084e3b592c00bdd090d0d457502904
parent e5399b363bcd8af06edc6bb4ba9edb034bcbe728
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date: Fri, 21 Mar 2025 19:21:44 +0100
libc/wchar: Add wcsncmp()
Diffstat:
5 files changed, 51 insertions(+), 0 deletions(-)
diff --git a/src/libc/objs/common-objs.mk b/src/libc/objs/common-objs.mk
@@ -128,6 +128,7 @@ COMMON_OBJS =\
wchar/wcrtomb.$O\
wchar/wcslen.$O\
wchar/wcscmp.$O\
+ wchar/wcsncmp.$O\
wchar/wcsrtombs.$O\
wchar/wcwidth.$O\
wchar/putwc.$O\
diff --git a/src/libc/wchar/wcsncmp.c b/src/libc/wchar/wcsncmp.c
@@ -0,0 +1,18 @@
+#include <wchar.h>
+
+#undef wcsncmp
+
+int
+wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n)
+{
+ unsigned long l1, l2;
+
+ for ( ; n > 0 && *s1 == *s2; --n)
+ ++s1, ++s2;
+
+ if (n == 0)
+ return 0;
+ l1 = *s1, l2 = *s2;
+
+ return l1 - l2;
+}
diff --git a/tests/libc/execute/.gitignore b/tests/libc/execute/.gitignore
@@ -41,4 +41,5 @@
0041-mbrlen
0044-wcslen
0045-wcscmp
+0046-wcsncmp
test.log
diff --git a/tests/libc/execute/0046-wcsncmp.c b/tests/libc/execute/0046-wcsncmp.c
@@ -0,0 +1,30 @@
+#include <assert.h>
+#include <stdio.h>
+#include <wchar.h>
+
+/*
+output:
+testing
+done
+end:
+*/
+
+int
+main(void)
+{
+ wchar_t t1[] = {0};
+ wchar_t t2[] = {0};
+ wchar_t t3[] = {0x31, 0};
+ wchar_t t4[] = {0x31, 0};
+ wchar_t t5[] = {0x32, 0x33, 0};
+
+ puts("testing");
+ assert(wcsncmp(t1, t1, 0) == 0);
+ assert(wcsncmp(t1, t2, 0) == 0);
+ assert(wcsncmp(t3, t4, 1) == 0);
+ assert(wcsncmp(t3, t5, 1) < 0);
+ assert(wcsncmp(t5, t3, 1) > 0);
+ puts("done");
+
+ return 0;
+}
diff --git a/tests/libc/execute/libc-tests.lst b/tests/libc/execute/libc-tests.lst
@@ -40,3 +40,4 @@
0041-mbrlen
0044-wcslen
0045-wcscmp
+0046-wcsncmp