mbrlen
来自 cppreference.cn
定义于头文件 <wchar.h> |
||
(since C95) (until C99) |
||
(since C99) | ||
确定多字节字符表示形式的大小(以字节为单位)。
此函数等效于调用 mbrtowc(NULL, s, n, ps ? ps : &internal) 对于类型为 mbstate_t 的某个隐藏对象 internal,除了表达式 ps 只会被评估一次。
目录 |
[edit] 参数
s | - | 指向多字节字符串元素的指针 |
n | - | 可以检查的 s 中的字节数限制 |
ps | - | 指向保存转换状态的变量的指针 |
[edit] 返回值
以下适用项中的第一项
- 0 如果接下来的 n 个或更少字节完成空字符,或者如果 s 是一个空指针。两种情况都会重置转换状态。
- 完成有效多字节字符的字节数 [1...n]
- (size_t)-2 如果接下来的 n 个字节是可能有效的多字节字符的一部分,但在检查所有 n 个字节后仍然不完整
- (size_t)-1 如果发生编码错误。 errno 的值为 errno,为 EILSEQ;转换状态未指定。
[edit] 示例
运行此代码
#include <locale.h> #include <stdio.h> #include <string.h> #include <wchar.h> int main(void) { // allow mbrlen() to work with UTF-8 multibyte encoding setlocale(LC_ALL, "en_US.utf8"); // UTF-8 narrow multibyte encoding const char* str = "水"; size_t sz = strlen(str); mbstate_t mb; memset(&mb, 0, sizeof mb); int len1 = mbrlen(str, 1, &mb); if (len1 == -2) printf("The first 1 byte of %s is an incomplete multibyte char" " (mbrlen returns -2)\n", str); int len2 = mbrlen(str + 1, sz - 1, &mb); printf("The remaining %zu bytes of %s hold %d bytes of the multibyte" " character\n", sz - 1, str, len2); printf("Attempting to call mbrlen() in the middle of %s while in initial" " shift state returns %zd\n", str, mbrlen(str + 1, sz - 1, &mb)); }
输出
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] 参考文献
- C23 标准 (ISO/IEC 9899:2024)
- 7.29.6.3.1 mbrlen 函数 (p: 待定)
- C17 标准 (ISO/IEC 9899:2018)
- 7.29.6.3.1 mbrlen 函数 (p: 待定)
- C11 标准 (ISO/IEC 9899:2011)
- 7.29.6.3.1 mbrlen 函数 (p: 442)
- C99 标准 (ISO/IEC 9899:1999)
- 7.24.6.3.1 mbrlen 函数 (p: 388)
[edit] 参见
(C95) |
给定状态,将下一个多字节字符转换为宽字符 (函数) |
返回下一个多字节字符中的字节数 (函数) | |
C++ 文档 关于 mbrlen 的文档
|