对码当歌,猿生几何?

std::mblen

From cppreference.com
< cpp‎ | string‎ | multibyte
Defined in header <cstdlib>
int mblen( const char* s, std::size_t n );

Determines the size, in bytes, of the multibyte character whose first byte is pointed to by s.

If s is a null pointer, resets the global conversion state and determined whether shift sequences are used.

This function is equivalent to the call std::mbtowc((wchar_t*)0, s, n), except that conversion state of std::mbtowc is unaffected.

Notes

Each call to mblen updates the internal global conversion state (a static object of type std::mbstate_t, only known to this function). If the multibyte encoding uses shift states, care must be taken to avoid backtracking or multiple scans. In any case, multiple threads should not call mblen without synchronization: std::mbrlen may be used instead.

Parameters

s - pointer to the multibyte character
n - limit on the number of bytes in s that can be examined

Return value

If s is not a null pointer, returns the number of bytes that are contained in the multibyte character or -1 if the first bytes pointed to by s do not form a valid multibyte character or 0 if s is pointing at the null charcter '\0'.

If s is a null pointer, resets its internal conversion state to represent the initial shift state and returns 0 if the current multibyte encoding is not state-dependent (does not use shift sequences) or a non-zero value if the current multibyte encoding is state-dependent (uses shift sequences).

Example

#include <clocale>
#include <string>
#include <iostream>
#include <cstdlib>
#include <stdexcept>
 
// the number of characters in a multibyte string is the sum of mblen()'s
// note: the simpler approach is std::mbstowcs(NULL, s.c_str(), s.size())
std::size_t strlen_mb(const std::string& s)
{
    std::size_t result = 0;
    const char* ptr = s.data();
    const char* end = ptr + s.size();
    std::mblen(NULL, 0); // reset the conversion state
    while (ptr < end) {
        int next = std::mblen(ptr, end-ptr);
        if (next == -1) {
            throw std::runtime_error("strlen_mb(): conversion error");
        }
        ptr += next;
        ++result;
    }
    return result;
}
 
int main()
{
    // allow mblen() to work with UTF-8 multibyte encoding
    std::setlocale(LC_ALL, "en_US.utf8");
    // UTF-8 narrow multibyte encoding
    std::string str = u8"z\u00df\u6c34\U0001f34c"; // or u8"zß水🍌"
    std::cout << str << " is " << str.size() << " bytes, but only "
              << strlen_mb(str) << " characters\n";
}

Output:

zß水🍌 is 10 bytes, but only 4 characters

See also

converts the next multibyte character to wide character
(function)
returns the number of bytes in the next multibyte character, given state
(function)
C documentation for mblen