mbrlen
來自 cppreference.com
在標頭檔案 <wchar.h> 中定義 |
||
(自 C95 起) (直到 C99) |
||
(C99 起) | ||
確定多位元組字元表示的位元組大小。
此函式等同於呼叫 mbrtowc(NULL, s, n, ps ? ps : &internal),其中 internal 是型別為 mbstate_t 的某個隱藏物件,只是表示式 ps 只會被評估一次。
目錄 |
[編輯] 引數
s | - | 指向多位元組字串中一個元素的指標 |
n | - | s 中可檢查的位元組數的限制 |
ps | - | 指向儲存轉換狀態的變數的指標 |
[編輯] 返回值
以下適用情況中的第一個
- 0 如果接下來的 n 或更少位元組完成空字元,或者如果 s 是空指標。兩種情況都重置轉換狀態。
- 完成一個有效多位元組字元的位元組數 [1...n]
- (size_t)-2 如果接下來的 n 位元組是可能有效的多位元組字元的一部分,但在檢查所有 n 位元組後仍不完整
- (size_t)-1 如果發生編碼錯誤。 errno 的值為 EILSEQ;轉換狀態不確定。
[編輯] 示例
執行此程式碼
#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
[編輯] 參考
- C23 標準 (ISO/IEC 9899:2024)
- 7.29.6.3.1 mbrlen 函式 (p: TBD)
- C17 標準 (ISO/IEC 9899:2018)
- 7.29.6.3.1 mbrlen 函式 (p: TBD)
- 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)
[編輯] 另請參閱
(C95) |
將下一個多位元組字元轉換為寬字元,給定狀態 (函式) |
返回下一個多位元組字元的位元組數 (函式) | |
C++ 文件 關於 mbrlen
|