liveBook · Manning

Mbstate_t

As explained in Modern C

mbstate_t is a C library type that stores the conversion state needed to interpret multibyte character sequences.

Counting multibyte characters

A call such as mbsrlen(start, 0) counts the characters represented by a multibyte string, rather than treating its bytes as individual characters. The zero state argument indicates that scanning needs no prior context. section 14.3

TAKEAWAY: Parsing a multibyte string may require a conversion state. The C standard type mbstate_t holds that state; it is provided by wchar.h and also by uchar.h. When a scan starts partway through a string, its state must be consistent with the multibyte character at that position. 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%@!

In this mbsrlen implementation, a local mbstate_t copy is passed to mbsrtowcs, so counting does not modify the caller’s state. section 14.3

How Modern C covers it

How deep it goes
Defines the type’s role and shows state copied and passed to conversion functions, without detailing its representation.
Examples
Uses mbsrlen to count characters and mbsrwc to retain the state at a search match.
What you can do after
Build it from scratch in multibyte string helpers that pass or copy a consistent conversion state.
How it connects
Connects multibyte strings with wchar_t, mbsrtowcs, and mbrtowc.

Also explained in

Compare how 2 books explain mbstate_t →

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.