scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit d21aa5a5a185cacd7a63439788287ef6564ad5e2
parent aef615310c0729e97e4ad5a2e4206266660679a2
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date:   Wed, 26 Mar 2025 14:26:21 +0100

libc/wchar: Add wcsspn()

Diffstat:
Msrc/libc/objs/common-objs.mk | 1+
Asrc/libc/wchar/wcsspn.c | 14++++++++++++++
Mtests/libc/execute/.gitignore | 1+
Atests/libc/execute/0061-wcsspn.c | 25+++++++++++++++++++++++++
Mtests/libc/execute/libc-tests.lst | 1+
5 files changed, 42 insertions(+), 0 deletions(-)

diff --git a/src/libc/objs/common-objs.mk b/src/libc/objs/common-objs.mk @@ -137,6 +137,7 @@ COMMON_OBJS =\ wchar/wcsrchr.$O\ wchar/wcsstr.$O\ wchar/wcsxfrm.$O\ + wchar/wcsspn.$O\ wchar/wcsrtombs.$O\ wchar/wcwidth.$O\ wchar/wmemchr.$O\ diff --git a/src/libc/wchar/wcsspn.c b/src/libc/wchar/wcsspn.c @@ -0,0 +1,14 @@ +#include <wchar.h> + +#undef wcsspn + +size_t +wcsspn(const wchar_t *s, const wchar_t *accept) +{ + wchar_t wc; + const wchar_t *p; + + for (p = s; (wc = *p) && wcschr(accept, wc); p++) + ; + return p - s; +} diff --git a/tests/libc/execute/.gitignore b/tests/libc/execute/.gitignore @@ -57,4 +57,5 @@ 0058-wcsrchr 0059-wcsstr 0060-wcsxfrm +0061-wcsspn test.log diff --git a/tests/libc/execute/0061-wcsspn.c b/tests/libc/execute/0061-wcsspn.c @@ -0,0 +1,25 @@ +#include <assert.h> +#include <stdio.h> +#include <wchar.h> + +/* +output: +testing +done +end: +*/ + +int +main() +{ + puts("testing"); + assert(wcsspn(L"abccdef", L"cba") == 4); + assert(wcsspn(L"abcg", L"cba0") == 3); + assert(wcsspn(L"", L"abc") == 0); + assert(wcsspn(L"abc", L"") == 0); + assert(wcsspn(L"", L"") == 0); + assert(wcsspn(L"abc", L"def") == 0); + puts("done"); + + return 0; +} diff --git a/tests/libc/execute/libc-tests.lst b/tests/libc/execute/libc-tests.lst @@ -56,3 +56,4 @@ 0058-wcsrchr 0059-wcsstr 0060-wcsxfrm +0061-wcsspn