A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../error/error_code/../../string/multibyte/mbrlen.html below:

std::mbrlen - cppreference.com

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.

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.

[edit] Parameters s - pointer to an element of a multibyte character string n - limit on the number of bytes in s that can be examined ps - pointer to the variable holding the conversion state [edit] Return value [edit] Example
#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();
 
    // simple use: length of a complete multibyte character
    const std::size_t len = std::mbrlen(&str[0], str.size(), &mb);
    std::cout << "The length of " << str << " is " << len << " bytes\n";
 
    // advanced use: restarting in the middle of a multibyte character
    const std::size_t len1 = std::mbrlen(&str[0], 1, &mb);
    if (len1 == std::size_t(-2))
        std::cout << "The first 1 byte of " << str
                  << " is an incomplete multibyte char (mbrlen returns -2)\n";
 
    const std::size_t 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";
 
    // error case:
    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 length of æ°´ is 3 bytes.
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
[edit] See also converts the next multibyte character to wide character, given state
(function) [edit] returns the number of bytes in the next multibyte character
(function) [edit] calculates the length of the ExternT string that would be consumed by conversion into given InternT buffer
(virtual protected member function of std::codecvt<InternT,ExternT,StateT>) [edit]

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4