liveBook · Manning

Mbsrtowcs

As explained in Modern C

mbsrtowcs is a C standard library function that converts a restartable multibyte string to a wide-character string.

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

How Modern C covers it

How deep it goes
Shows counting and allocation calls, including conversion-state copies and error checks, without implementing the library function itself.
Examples
Uses mbsrlen to count characters and mbsrdup to allocate and fill a wide-character string.
What you can do after
Apply it via the C standard library to count multibyte-string characters and populate an allocated wide-character string.
How it connects
Links multibyte strings to wchar_t, wide-character strings, mbstate_t, strlen, and wcs-prefixed string functions.

Also explained in

Compare how 2 books explain mbsrtowcs →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.