std::mbsrtowcs(3)
πΈ NAME πΈ
std::mbsrtowcs β okay so the name is literally std::mbsrtowcs, no plot twist. This is the std::mbsrtowcs(3) page from the C++ Standard Libary (their spelling, not mine, love that for them π
). We're converting multibyte strings to wide strings, and it's highkey iconic.
π¦ Synopsis π¦
The synopsis says Defined in header <cwchar>, bestie, and the signature is exactly this:
std::size_t mbsrtowcs( wchar_t* dst,
const char** src,
std::size_t len,
std::mbstate_t* ps );
So what does it do? It converts a null-terminated multibyte character sequence β one that begins in the conversion state described by *ps β from the array whose first element is pointed to by *src into its wide character representation. If dst is not null, the converted characters get stored in the successive elements of the wchar_t array pointed to by dst. No more than len wide characters are written to the destination array. Periodt.
Each multibyte character is converted as if by a call to std::mbrtowc. The conversion stops if:
- The multibyte null character was converted and stored.
srcis set to a null pointer and*psrepresents the initial shift state. - An invalid multibyte character (according to the current C locale) was encountered.
srcis set to point at the beginning of the first unconverted multibyte character. - The next wide character to be stored would exceed
len.srcis set to point at the beginning of the first unconverted multibyte character. This condition is not checked ifdstis a null pointer.
Vibe check: those stop conditions are literally the whole personality of this function, so don't skip them π
π Parameters π
dstβ pointer to wide character array where the results will be stored.srcβ pointer to pointer to the first element of a null-terminated multibyte string.lenβ number of wide characters available in the array pointed to bydst.psβ pointer to the conversion state object.
β¨ Return value β¨
On success, it returns the number of wide characters, excluding the terminating L'\0', written to the character array. If dst is a null pointer, it returns the number of wide characters that would have been written given unlimited length.
On conversion error (if an invalid multibyte character was encountered), it returns static_cast<std::size_t>(-1), stores EILSEQ in errno, and leaves *ps in unspecified state. Not cute, but that's the deal.
π Notes π
This function moves the src pointer to the end of the converted multibyte string. This doesn't happen if dst is a null pointer. iykyk β¨
π Example π
// Run this code
#include <clocale>
#include <cwchar>
#include <iostream>
#include <vector>
void print_as_wide(const char* mbstr)
{
std::mbstate_t state = std::mbstate_t();
std::size_t len = 1 + std::mbsrtowcs(nullptr, &mbstr, 0, &state);
std::vector<wchar_t> wstr(len);
std::mbsrtowcs(&wstr[0], &mbstr, wstr.size(), &state);
std::wcout << "Wide string: " << &wstr[0] << '\n'
<< "The length, including '\\0': " << wstr.size() <<
'\n';
}
int main()
{
std::setlocale(LC_ALL, "en_US.utf8");
const char* mbstr = "z\u00df\u6c34\U0001f34c"; // or u8"zΓζ°΄π"
print_as_wide(mbstr);
}
π Output π
Wide string: zΓζ°΄π
The length, including '\0': 5
π See also π
mbrtowcβ converts the next multibyte character to wide character, given state (function).wcsrtombsβ converts a wide string to narrow multibyte character string, given state (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
mbsrtowcs.
Okay bestie, that's the whole page π source: http://cppreference.com, version/date: 2024.06.10, page: std::mbsrtowcs(3). Go slay your multibyte conversions, no cap π«Άβ¨
π¦ 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