std::mbstowcs(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

okay bestie, meet std::mbstowcs β€” literally std::mbstowcs, no notes. It's from the C++ Standard Library (std::mbstowcs(3) if you're keeping score), and the original header spells it C++ Standard Libary, which is honestly iconic of it. This function is giving "turn my multibyte string into wide characters" energy, and I'm obsessed. 🫢


✨ Synopsis ✨

Defined in header <cstdlib>:

std::size_t mbstowcs( wchar_t* dst, const char* src, std::size_t len );

okay so here's the vibe: it converts a multibyte character string from the array whose first element is pointed to by src into its wide character representation. The converted characters are stored in the successive elements of the array pointed to by dst. No more than len wide characters are written to the destination array. That's the cap, bestie. πŸ’…

Each character is converted as if by a call to std::mbtowc, except that the mbtowc conversion state is unaffected. iykyk 🧚 The conversion stops if:

  • The multibyte null character was converted and stored.
  • An invalid (in the current C locale) multibyte character was encountered.
  • The next wide character to be stored would exceed len.

🌸 Notes 🌸

In most implementations, this function updates a global static object of type std::mbstate_t as it processes through the string, and it cannot be called simultaneously by two threads. Not me being a shared-state main character 😭 β€” std::mbsrtowcs should be used in such cases.

POSIX specifies a common extension: if dst is a null pointer, this function returns the number of wide characters that would be written to dst, if converted. Similar behavior is standard for std::mbsrtowcs. slay for consistency ✨


πŸŽ€ Parameters πŸŽ€

  • dst β€” pointer to wide character array where the wide string will be stored.
  • src β€” pointer to the first element of a null-terminated multibyte string to convert.
  • len β€” number of wide characters available in the array pointed to by dst.

πŸ¦‹ Return value πŸ¦‹

On success, it returns the number of wide characters, excluding the terminating L'\0', written to the destination array. periodt. πŸ’–

On conversion error (if an invalid multibyte character was encountered), it returns static_cast<std::size_t> (-1). yeah, that's the sad little error vibe check πŸ’€


🌈 Example 🌈

okay, run this code πŸƒβ€β™€οΈπŸ’¨ (the source even says // Run this code):

#include <clocale>
#include <cstdlib>
#include <iostream>

int main()
{
    std::setlocale(LC_ALL, "en_US.utf8");
    std::wcout.imbue(std::locale("en_US.utf8"));
    const char* mbstr = "z\u00df\u6c34\U0001f34c"; // or u8"zß水🍌"
                        // or
"\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9f\x8d\x8c";
    wchar_t wstr[5];
    std::mbstowcs(wstr, mbstr, 5);
    std::wcout << "wide string: " << wstr << '\n';
}

Output:

wide string: zß水🍌

πŸ‘‘ See also πŸ‘‘

  • mbsrtowcs β€” converts a narrow multibyte character string to wide string, given state (function).
  • wcstombs β€” converts a wide string to narrow multibyte character string (function).
  • do_in β€” converts a string from ExternT to InternT, such as when reading from file [virtual] (virtual protected member function of std::codecvt<InternT,ExternT,StateT>).
  • C documentation for mbstowcs.

kisses, your Unix bestie πŸ’‹ β€” this page came from cppreference.com, version 2024.06.10, and it's the std::mbstowcs(3) manual page. periodt. πŸŽ€

πŸ“¦ where this page comes from