mbrlen
From cppreference.net
|
ヘッダーで定義
<wchar.h>
|
||
|
(C95以降)
(C99まで) |
||
| (C99以降) | ||
マルチバイト文字の表現におけるサイズをバイト単位で決定します。
この関数は、型 mbstate_t の隠れオブジェクト internal に対して mbrtowc ( NULL , s, n, ps ? ps : & internal ) を呼び出すことと等価ですが、式 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++ documentation
for
mbrlen
|
|