libc/wchar: Add wcsrchr() - 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 1b9b9b8ada90d264546916bc65e2867ba3a0fca3
 (DIR) parent 8a6918d48bb8ecbc1c2af2c72b2e89a3df3a999b
 (HTM) Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
       Date:   Tue, 25 Mar 2025 21:32:52 +0100
       
       libc/wchar: Add wcsrchr()
       
       Diffstat:
         M src/libc/objs/common-objs.mk        |       1 +
         A src/libc/wchar/wcsrchr.c            |      16 ++++++++++++++++
         M tests/libc/execute/.gitignore       |       1 +
         A tests/libc/execute/0058-wcsrchr.c   |      27 +++++++++++++++++++++++++++
         M tests/libc/execute/libc-tests.lst   |       1 +
       
       5 files changed, 46 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/src/libc/objs/common-objs.mk b/src/libc/objs/common-objs.mk
       @@ -134,6 +134,7 @@ COMMON_OBJS =\
                wchar/wcscat.$O\
                wchar/wcsncat.$O\
                wchar/wcschr.$O\
       +        wchar/wcsrchr.$O\
                wchar/wcsrtombs.$O\
                wchar/wcwidth.$O\
                wchar/wmemchr.$O\
 (DIR) diff --git a/src/libc/wchar/wcsrchr.c b/src/libc/wchar/wcsrchr.c
       @@ -0,0 +1,16 @@
       +#include <wchar.h>
       +
       +#undef wcsrchr
       +
       +wchar_t *
       +wcsrchr(const wchar_t *s, wchar_t c)
       +{
       +        wchar_t *t;
       +
       +        for (t = (wchar_t *) s; *t; ++t)
       +                ;
       +        while (t > s && *t != c)
       +                --t;
       +
       +        return (*t == c) ? t : NULL;
       +}
 (DIR) diff --git a/tests/libc/execute/.gitignore b/tests/libc/execute/.gitignore
       @@ -54,4 +54,5 @@
        0055-wcscat
        0056-wcsncat
        0057-wcschr
       +0058-wcsrchr
        test.log
 (DIR) diff --git a/tests/libc/execute/0058-wcsrchr.c b/tests/libc/execute/0058-wcsrchr.c
       @@ -0,0 +1,27 @@
       +#include <assert.h>
       +#include <stdio.h>
       +#include <wchar.h>
       +
       +/*
       +output:
       +testing
       +done
       +end:
       +*/
       +
       +int
       +main()
       +{
       +        wchar_t buf[] = L"012321";
       +
       +        puts("testing");
       +        assert(wcsrchr(buf, '1') == buf+5);
       +        assert(wcsrchr(buf, '0') == buf);
       +        assert(wcsrchr(buf, '3') == buf+3);
       +        assert(wcsrchr("",  '0') == NULL);
       +        assert(wcsrchr(buf, 'a')  == NULL);
       +        assert(wcsrchr(buf, 0) == buf+6);
       +        puts("done");
       +
       +        return 0;
       +}
 (DIR) diff --git a/tests/libc/execute/libc-tests.lst b/tests/libc/execute/libc-tests.lst
       @@ -53,3 +53,4 @@
        0055-wcscat
        0056-wcsncat
        0057-wcschr
       +0058-wcsrchr