std::mbrtoc32(3)
π
std::mbrtoc32(3) π
okay so this page comes from the C++ Standard Libary manual (yes bestie, that spelling, love that for us) and it's all about std::mbrtoc32(3). no cap, we're getting into multibyte-to-UTF-32 conversion and it's lowkey iconic.
π NAME π
std::mbrtoc32 β literally std::mbrtoc32, bestie. that's the name. periodt.
π¦ Synopsis π¦
Defined in header <cuchar>:
std::size_t mbrtoc32( char32_t* pc32,
const char* s, (since C++11)
std::size_t n,
std::mbstate_t* ps );
Converts a narrow multibyte character to its UTF-32 character representation. slay.
If s is not a null pointer, it inspects at most n bytes of the multibyte character string, beginning with the byte pointed to by s, to figure out the number of bytes necessary to complete the next multibyte character (including any shift sequences). If the function determines that the next multibyte character in s is complete and valid, it converts it to the corresponding 32-bit character and stores it in *pc32 (if pc32 is not null). cute.
If the multibyte character in *s corresponds to a multi-char32_t sequence (not possible with UTF-32), then after the first call to this function, *ps is updated in such a way that the next calls to mbrtoc32 will write out the additional char32_t, without considering *s. yes, the state object is doing main character energy.
If s is a null pointer, the values of n and pc32 are ignored and the call is equivalent to std::mbrtoc32(nullptr, "", 1, ps).
If the wide character produced is the null character, the conversion state *ps represents the initial shift state.
The multibyte encoding used by this function is specified by the currently active C locale. iykyk.
π Parameters π
pc32β pointer to the location where the resulting 32-bit character will be written π«Άsβ pointer to the multibyte character string used as input β¨nβ limit on the number of bytes insthat can be examined πΈpsβ pointer to the conversion state object used when interpreting the multibyte string π¦
π₯ Return value π₯
the first of the following that applies, bestie:
0β if the character converted froms(and stored in*pc32if non-null) was the null character.- the number of bytes
[1...n]of the multibyte character successfully converted froms. -3β if the nextchar32_tfrom a multi-char32_tcharacter has now been written to*pc32. no bytes are processed from the input in this case.-2β if the nextnbytes constitute an incomplete, but so far valid, multibyte character. nothing is written to*pc32.-1β if encoding error occurs. nothing is written to*pc32, the valueEILSEQis stored inerrnoand the value of*psis unspecified. π
β¨ Examples β¨
run this code, bestie:
// Run this code
#include <cassert>
#include <clocale>
#include <cstring>
#include <cuchar>
#include <cwchar>
#include <iomanip>
#include <iostream>
int main()
{
std::setlocale(LC_ALL, "en_US.utf8");
std::string str = "z\u00df\u6c34\U0001F34C"; // or u8"zΓζ°΄π"
std::cout << "Processing " << str.size() << " bytes: [ " <<
std::showbase;
for (unsigned char c : str)
std::cout << std::hex << +c << ' ';
std::cout << "]\n";
std::mbstate_t state{}; // zero-initialized to initial state
char32_t c32;
const char* ptr = str.c_str(), *end = str.c_str() + str.size() + 1;
while (std::size_t rc = std::mbrtoc32(&c32, ptr, end - ptr,
&state))
{
std::cout << "Next UTF-32 char: " << std::hex
<< static_cast<int>(c32) << " obtained from ";
assert(rc != (std::size_t) - 3); // no surrogates in UTF-32
if (rc == (std::size_t) - 1)
break;
if (rc == (std::size_t) - 2)
break;
std::cout << std::dec << rc << " bytes [ ";
for (std::size_t n = 0; n < rc; ++n)
std::cout << std::hex << +static_cast<unsigned
char>(ptr[n]) << ' ';
std::cout << "]\n";
ptr += rc;
}
}
Output:
Processing 10 bytes: [ 0x7a 0xc3 0x9f 0xe6 0xb0 0xb4 0xf0 0x9f 0x8d
0x8c ]
Next UTF-32 char: 0x7a obtained from 1 bytes [ 0x7a ]
Next UTF-32 char: 0xdf obtained from 2 bytes [ 0xc3 0x9f ]
Next UTF-32 char: 0x6c34 obtained from 3 bytes [ 0xe6 0xb0 0xb4 ]
Next UTF-32 char: 0x1f34c obtained from 4 bytes [ 0xf0 0x9f 0x8d 0x8c ]
π See also π
c32rtombβ convert a 32-bit wide character to narrow multibyte string (C++11) (function) πdo_inβ converts a string fromExternTtoInternT, such as when reading from file [virtual] (virtual protected member function ofstd::codecvt<InternT,ExternT,StateT>) π§- C documentation for
mbrtoc32π
π· source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::mbrtoc32(3) β mwah, bye bestie π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms