Count characters without converting into an array
A character-counting helper calls `mbsrtowcs(0, &s, 0, &st)`, where `st` is a copy of the conversion state. Unlike `strlen`, this counts wide characters rather than bytes in the multibyte string. section 14.3
`mbsrtowcs` means “multibyte string, restartable, to wide character string.” Its `src` argument identifies the multibyte string; `dst` receives wide characters of type `wchar_t`. A wide-character string is a null-terminated array, much like an ordinary `char` string. section 14.3
TAKEAWAY: Use a null destination to obtain the length before allocating storage. A conversion helper then allocates room for that many wide characters plus the terminator and calls `mbsrtowcs` again with the allocated destination. The counting helper checks for a return value of `-1` before using the length. section 14.3
30 size_t mbsrlen(char const*s, mbstate_t const*restrict state) {
31 if (!state) state = MBSTATE;
32 mbstate_t st = *state;
33 size_t mblen = mbsrtowcs(0, &s, 0, &st);
34 if (mblen == -1) errno = 0;
35 return mblen;
36 }
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":11}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":13}],[{\"line\":0,\"ch\":20},{\"line\":0,\"ch\":30}],[{\"line\":0,\"ch\":34},{\"line\":0,\"ch\":43}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":16}],[{\"line\":0,\"ch\":25},{\"line\":0,\"ch\":30}],[{\"line\":0,\"ch\":50},{\"line\":0,\"ch\":58}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":2}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":9}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":9}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":0,\"ch\":34},{\"line\":0,\"ch\":43}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":16}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":11}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":13}],[{\"line\":3,\"ch\":14},{\"line\":3,\"ch\":19}],[{\"line\":4,\"ch\":11},{\"line\":4,\"ch\":16}],[{\"line\":5,\"ch\":14},{\"line\":5,\"ch\":19}],[{\"line\":3,\"ch\":22},{\"line\":3,\"ch\":31}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":9}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":9}],[{\"line\":3,\"ch\":14},{\"line\":3,\"ch\":19}],[{\"line\":4,\"ch\":11},{\"line\":4,\"ch\":16}],[{\"line\":5,\"ch\":14},{\"line\":5,\"ch\":19}],[{\"line\":4,\"ch\":24},{\"line\":4,\"ch\":29}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}],[{\"line\":5,\"ch\":7},{\"line\":5,\"ch\":13}],[{\"line\":3,\"ch\":14},{\"line\":3,\"ch\":19}],[{\"line\":4,\"ch\":11},{\"line\":4,\"ch\":16}],[{\"line\":5,\"ch\":14},{\"line\":5,\"ch\":19}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":2}]]"}
!@%STYLE%@!
Inspect the counting call and its copied conversion state. section 14.3

