std::mbrlen
Defined in header <cwchar> | | |
|---|---|---|
std::size_t mbrlen( const char* s, std::size_t n, std::mbstate_t* ps); | | |
确定其第一个字节指向的多字节字符的剩余部分的大小(以字节为单位)。s,给定当前的转换状态。ps...
此函数等效于调用。std::mbrtowc(nullptr, s, n, ps?ps:&internal)对于某个隐藏的对象internal类型std::mbstate_t,除了这句话ps只计算一次。
参数
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 |
返回值
0如果下一个n或者更少的字节完成空字符。
之间的字节数%281和n%29完成有效的多字节字符。
(size_t)-1如果出现编码错误。
(size_t)-2如果下一个n字节是可能有效的多字节字符的一部分,在检查完所有字符后,该字符仍然是不完整的。n字节。
例
二次
#include <clocale>
#include <string>
#include <iostream>
#include <cwchar>
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 = u8"水"; // 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';
}二次
产出:
二次
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二次
另见
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 virtual | calculates the length of the externT string that would be consumed by conversion into given internT buffer (virtual protected member function of std::codecvt) |
c为mbrlen编写的文件
© cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

