MBRTOWC(3)

๐Ÿ“ฆ version 15.1, in freebsd 14.5, freebsd 15.1

๐Ÿ’… MBRTOWC(3) โ€” Library Functions Manual ๐Ÿ’…

okay bestie, gather round: this is the glow-up for mbrtowc(3), mbrtoc16(3), and mbrtoc32(3) โ€” the restartable wide-character conversion trio. theyโ€™re iconic, theyโ€™re useful, and yes, they have lore. โœจ

๐ŸŒธ NAME ๐ŸŒธ

mbrtowc, mbrtoc16, mbrtoc32 โ€” convert a character to a wide-character code (restartable)

๐ŸŽ€ LIBRARY ๐ŸŽ€

Standard C Library (libc, -lc)

โœจ SYNOPSIS โœจ

#include <wchar.h>

size_t
mbrtowc(wchar_t * restrict pc, const char * restrict s, size_t n,
    mbstate_t * restrict ps);

#include <uchar.h>

size_t
mbrtoc16(char16_t * restrict pc, const char * restrict s, size_t n,
    mbstate_t * restrict ps);

size_t
mbrtoc32(char32_t * restrict pc, const char * restrict s, size_t n,
    mbstate_t * restrict ps);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

okay so picture this: mbrtowc(), mbrtoc16(), and mbrtoc32() inspect at most n bytes pointed to by s to determine how many bytes are needed to complete the next multibyte character. ๐Ÿ”

If a character can be completed, and pc is not NULL, the wide character represented by s gets stored in the wchar_t, char16_t, or char32_t that pc points to. slay. ๐Ÿ’…

If s is NULL, these functions behave as if pc was NULL, s was an empty string (""), and n was 1. no cap.

The mbstate_t argument, ps, is used to keep track of the shift state. If it is NULL, these functions use an internal, static mbstate_t object, which is initialized to the initial conversion state at program startup. ๐ŸŽ€

And the way that a single char16_t is not large enough to represent certain multibyte characters means mbrtoc16() may need to be invoked multiple times to convert a single multibyte character sequence. itโ€™s giving โ€œone more time for the girlies in the back.โ€ ๐Ÿ˜ญ

๐Ÿ‘‘ RETURN VALUES ๐Ÿ‘‘

The mbrtowc(), mbrtoc16(), and mbrtoc32() functions return:

  • 0 โ€” the next n or fewer bytes represent the null wide character (L'\0'). ๐Ÿ’€
  • >0 โ€” the next n or fewer bytes represent a valid character, and these functions return the number of bytes used to complete the multibyte character. ๐Ÿ”ฅ
  • (size_t)-1 โ€” an encoding error has occurred. The next n or fewer bytes do not contribute to a valid multibyte character. ๐Ÿšจ
  • (size_t)-2 โ€” the next n contribute to, but do not complete, a valid multibyte character sequence, and all n bytes have been processed. ๐Ÿ˜ญ

The mbrtoc16() function also returns:

  • (size_t)-3 โ€” the next character resulting from a previous call has been stored. No bytes from the input have been consumed. โœจ

๐Ÿ™ˆ ERRORS ๐Ÿ™ˆ

The mbrtowc(), mbrtoc16(), and mbrtoc32() functions will fail if:

  • [EILSEQ] โ€” an invalid multibyte sequence was detected.
  • [EINVAL] โ€” the conversion state is invalid.

๐ŸŒˆ SEE ALSO ๐ŸŒˆ

mbtowc(3), multibyte(3), setlocale(3), wcrtomb(3)

โญ STANDARDS โญ

The mbrtowc(), mbrtoc16(), and mbrtoc32() functions conform to ISO/IEC 9899:2011 (โ€œISO C11โ€). ๐Ÿซถ

๐Ÿ’– xoxo, Debian โ€” May 21, 2013 โ€” MBRTOWC(3) ๐Ÿ’–

๐Ÿ“ฆ where this page comes from