libc/wchar: Use hidden state in mblen and mbrlen - 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 6f13ed360e15b5daa1df2c2de39dc93422db4255
 (DIR) parent abc7bb9f69d648be0457170d1f491594cbb2f7e7
 (HTM) Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
       Date:   Thu, 20 Mar 2025 20:07:17 +0100
       
       libc/wchar: Use hidden state in mblen and mbrlen
       
       These functions have a hidden state and they should
       behave as if they don't call mbrtowc (not using the
       internal hidden state of mbrtowc) and the return value
       of mblen() has to be adjusted because it cannot
       return -2 in the same way that mbrtowc() does.
       
       Diffstat:
         M src/libc/stdlib/mblen.c             |       9 ++++++++-
         M src/libc/wchar/mbrlen.c             |       4 ++++
       
       2 files changed, 12 insertions(+), 1 deletion(-)
       ---
 (DIR) diff --git a/src/libc/stdlib/mblen.c b/src/libc/stdlib/mblen.c
       @@ -1,9 +1,16 @@
        #include <stdlib.h>
       +#include <wchar.h>
        
        #undef mblen
        
        int
        mblen(const char *s, size_t n)
        {
       -        return mbtowc(NULL, s, n);
       +        int ret;
       +        static mbstate_t st;
       +
       +        ret = mbrtowc(NULL, s, n, &st);
       +        if (ret < 0)
       +                ret = -1;
       +        return ret;
        }
 (DIR) diff --git a/src/libc/wchar/mbrlen.c b/src/libc/wchar/mbrlen.c
       @@ -5,5 +5,9 @@
        size_t
        mbrlen(const char *restrict s, size_t n, mbstate_t *restrict ps)
        {
       +        static mbstate_t st;
       +
       +        if (!ps)
       +                ps = &st;
                return mbrtowc(NULL, s, n, ps);
        }