libc/wchar: Add wcsstr() - scc - simple c99 compiler
 (HTM) git clone git://git.simple-cc.org/scc
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 09245abd272928c778c739e2402517a464b1a678
 (DIR) parent 1b9b9b8ada90d264546916bc65e2867ba3a0fca3
 (HTM) Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
       Date:   Tue, 25 Mar 2025 21:44:30 +0100
       
       libc/wchar: Add wcsstr()
       
       Diffstat:
         M src/libc/objs/common-objs.mk        |       1 +
         A src/libc/wchar/wcsstr.c             |      22 ++++++++++++++++++++++
         M tests/libc/execute/.gitignore       |       1 +
         A tests/libc/execute/0059-wcsstr.c    |      32 +++++++++++++++++++++++++++++++
         M tests/libc/execute/libc-tests.lst   |       1 +
       
       5 files changed, 57 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/src/libc/objs/common-objs.mk b/src/libc/objs/common-objs.mk
       @@ -135,6 +135,7 @@ COMMON_OBJS =\
                wchar/wcsncat.$O\
                wchar/wcschr.$O\
                wchar/wcsrchr.$O\
       +        wchar/wcsstr.$O\
                wchar/wcsrtombs.$O\
                wchar/wcwidth.$O\
                wchar/wmemchr.$O\
 (DIR) diff --git a/src/libc/wchar/wcsstr.c b/src/libc/wchar/wcsstr.c
       @@ -0,0 +1,22 @@
       +#include <wchar.h>
       +
       +#undef wcsstr
       +
       +wchar_t *
       +wcsstr(const wchar_t *s1, const wchar_t *s2)
       +{
       +        wchar_t *p = (wchar_t *) s1;
       +        wchar_t c = *s2;
       +        size_t len;
       +
       +        if ((len = wcslen(s2)) == 0)
       +                return p;
       +
       +        for ( ; p = wcschr(p, c); ++p) {
       +                if (!wcsncmp(p, s2, len))
       +                        return p;
       +        }
       +
       +        return NULL;
       +}
       +
 (DIR) diff --git a/tests/libc/execute/.gitignore b/tests/libc/execute/.gitignore
       @@ -55,4 +55,5 @@
        0056-wcsncat
        0057-wcschr
        0058-wcsrchr
       +0059-wcsstr
        test.log
 (DIR) diff --git a/tests/libc/execute/0059-wcsstr.c b/tests/libc/execute/0059-wcsstr.c
       @@ -0,0 +1,32 @@
       +#include <assert.h>
       +#include <stdio.h>
       +#include <wchar.h>
       +
       +/*
       +output:
       +testing
       +done
       +end:
       +*/
       +
       +int
       +main()
       +{
       +        wchar_t buf[30] = L"abc";
       +
       +        puts("testing");
       +        assert(wcsstr(buf, L"abc") == buf);
       +        assert(wcsstr(buf, L"bc") == buf + 1);
       +        assert(wcsstr(buf, L"c") == buf + 2);
       +        assert(wcsstr(buf, L"d") == NULL);
       +        wcscpy(buf, L"ababc");
       +        assert(wcsstr(buf, L"abc") == buf+2);
       +        assert(wcsstr(L"", L"abc") == NULL);
       +        assert(wcsstr(buf, L"") == buf);
       +        buf[0] = '\0';
       +        assert(wcsstr(buf, L"") == buf);
       +        puts("done");
       +
       +        return 0;
       +}
       +
 (DIR) diff --git a/tests/libc/execute/libc-tests.lst b/tests/libc/execute/libc-tests.lst
       @@ -54,3 +54,4 @@
        0056-wcsncat
        0057-wcschr
        0058-wcsrchr
       +0059-wcsstr