Keep conversion state across input buffers
A restartable conversion passes the same state to each call: `mbstate_t st = { };` followed by `mbrtoc8(ob, p, n, &st)`. If the available bytes end inside a multibyte character, the next input buffer can continue that conversion using `st`. Set `LC_CTYPE` before scanning to use the platform’s multibyte encoding rather than treating input one byte at a time under the `"C"` locale. section 14.5
`mbstate_t` is the C standard-library type for a multibyte conversion’s parsing state. It is provided by `<wchar.h>`, alongside multibyte and wide-character utilities. A multibyte string remains null-terminated, but its byte length does not directly give its number of characters; parsing character boundaries may require a state. section 14.3
size_t const r = mbrtoc8(ob, p, n, &st);
switch (r) { // Handle the special cases.
case mbincomplete: p += n; in += n; continue;
case 0: case mbstored: case mbinvalid: goto INVAL;
}
p += r; in += r;
char8_t* cont = ob+1; // first character is already stored
while (mbrtoc8(cont, "", 1, &st) == mbstored) {
cont++;
}
The repeated `mbrtoc8` calls show why state belongs to the conversion, not to one input call: the code handles incomplete input, then obtains remaining UTF-8 output bytes with the same `st`. section 14.5
TAKEAWAY — preserve the state appropriate to the conversion you are continuing.
- Keep it across input-buffer boundaries when a multibyte character may arrive piecewise. A fixed-size input buffer need not contain every complete character. section 14.5
- Copy it when a conversion must inspect text without advancing the caller’s state: `mbsrlen` copies `*state` into a local `mbstate_t st` before calling `mbsrtowcs`. section 14.3
- Save a prior state when a search must return to a character boundary: `mbsrwc` backs up its state before decoding a character and can retain that backup at a match. section 14.3
State also matters on the output side. In the UTF-8 conversion example, one `mbrtoc8` call consumes input for a code point; further calls using the same state collect its remaining UTF-8 bytes. The code handles incomplete input and invalid input separately rather than treating every return value as a byte count. section 14.5

