std::mbrlen(3)
π std::mbrlen
okay so this page lives in the C++ Standard Library manual (section 3, bestie), and it's about one very specific little function. buckle up π
β¨ NAME β¨
std::mbrlen β yes the name is literally just std::mbrlen, that's the whole vibe, no notes π
β¨ Synopsis β¨
so first things first, this bad boy is Defined in header <cwchar> π«Ά
and here's the signature, exactly as written:
std::size_t mbrlen( const char* s, std::size_t n, std::mbstate_t* ps);
what does it do? it Determines the size, in bytes, of the remainder of the multibyte character whose first byte is pointed to by s, given the current conversion state ps. β¨
and here's the tea: this function is equivalent to the call std::mbrtowc(nullptr, s, n, ps ? ps : &internal) for some hidden object internal of type std::mbstate_t, except that the expression ps is evaluated only once. (yes, once. the compiler is not doing extra homework, and honestly? respect. π)
πΈ Parameters πΈ
sβ pointer to an element of a multibyte character stringnβ limit on the number of bytes insthat can be examinedpsβ pointer to the variable holding the conversion state
π₯ Return value π₯
bestie, there are four possible moods this function can wake up in:
0if the nextnor fewer bytes complete the null character.- the number of bytes (between
1andn) that complete a valid multibyte character. (size_t) - 1if encoding error occurs. π(size_t) - 2if the nextnbytes are part of a possibly valid multibyte character, which is still incomplete after examining allnbytes. (it's giving "to be continued..." energy π¦)
π Example π
// Run this code
#include <clocale>
#include <cwchar>
#include <iostream>
#include <string>
int main()
{
// allow mbrlen() to work with UTF-8 multibyte encoding
std::setlocale(LC_ALL, "en_US.utf8");
// UTF-8 narrow multibyte encoding
std::string str = "ζ°΄"; // or u8"\u6c34" or "\xe6\xb0\xb4"
std::mbstate_t mb = std::mbstate_t();
int len1 = std::mbrlen(&str[0], 1, &mb);
if (len1 == -2)
std::cout << "The first 1 byte of " << str
<< " is an incomplete multibyte char (mbrlen returns -2)\n";
int len2 = std::mbrlen(&str[1], str.size() - 1, &mb);
std::cout << "The remaining " << str.size() - 1 << " bytes of " << str
<< " hold " << len2 << " bytes of the multibyte character\n";
std::cout << "Attempting to call mbrlen() in the middle of " << str
<< " while in initial shift state returns "
<< (int)mbrlen(&str[1], str.size(), &mb) << '\n';
}
β¨ Output β¨
The first 1 byte of ζ°΄ is an incomplete multibyte char (mbrlen returns -2)
The remaining 2 bytes of ζ°΄ hold 2 bytes of the multibyte character
Attempting to call mbrlen() in the middle of ζ°΄ while in initial shift state returns -1
like, look at that output. it ate. π
π¦ See also π¦
mbrtowcβ converts the next multibyte character to wide character, given state (function)mblenβ returns the number of bytes in the next multibyte character (function)do_lengthβ calculates the length of theExternTstring that would be consumed by conversion into givenInternTbuffer β [virtual] (virtual protected member function ofstd::codecvt<InternT,ExternT,StateT>)- C documentation for
mbrlenβ the og crossover episode π§
okay that's the whole page, no cap, every single deet is in there π this one's brought to you by http://cppreference.com, version 2024.06.10, dated 2024.06.10 β mwah, go write some multibyte-safe code bestie, i'm obsessed with you ππβ¨
π¦ 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