mbrlen
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <wchar.h> で定義
|
||
size_t mbrlen( const char *s, size_t n, mbstate_t *ps ); |
(C95以上) (C99未満) |
|
size_t mbrlen( const char *restrict s, size_t n, mbstate_t *restrict ps ); |
(C99以上) | |
マルチバイト文字表現のバイト単位のサイズを調べます。
この関数は、式 ps が一度しか評価されないことを除き、 mbstate_t 型の何らかの隠れたオブジェクト internal に対する呼び出し mbrtowc(nullptr, s, n, ps?ps:&internal) と同等です。
引数
| s | - | マルチバイト文字列の要素を指すポインタ |
| n | - | 調べる s のバイト数の制限 |
| ps | - | 変換状態を保持する変数を指すポインタ |
戻り値
以下のいずれか最初に適用されるものが返されます。
- 次の
nバイトまたはそれより少ないバイトが完全なヌル文字を構成する場合、またはsがヌルポインタの場合は0。 どちらの場合も変換状態をリセットします。 - 有効なマルチバイト文字を構成するバイト数
[1...n]。 - 次の
nバイトが有効なマルチバイト文字となり得る一部だけれども、nバイトをすべて調べた後未だ不完全な場合は(size_t)-2。 - エンコーディングエラーが発生した場合は
(size_t)-1。 errno の値はEILSEQになり、変換状態は未規定になります。
例
Run this code
#include <locale.h>
#include <string.h>
#include <stdio.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 = u8"水";
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