mbrlen() — マルチバイト文字の長さの計算 (再始動可能)

形式

#include <wchar.h>
size_t mbrlen (const char *s, size_t n, mbstate_t *ps);

言語レベル

ANSI

スレッド・セーフ

はい (ps が NULL 以外の場合)。

ロケール依存

この関数の振る舞いは、現行ロケールの LC_CTYPE カテゴリーの影響を受ける可能性があります。 この関数は、LOCALETYPE(*LOCALEUCS2) または LOCALETYPE(*LOCALEUTF) をコンパイル・コマンドで指定した場合も、現行ロケールの LC_UNI_CTYPE カテゴリーによって影響を受ける可能性があります。 この関数は、コンパイル・コマンドに対して LOCALETYPE(*CLD) が指定されている場合には使用できません。 詳しくは、 CCSID とロケールについてを参照してください。

説明

この関数は、 mblen()の再始動可能バージョンです。

mbrlen() 関数は、マルチバイト文字の長さを判別します。

n は、検証するマルチバイト・ストリングのバイト数 (最大) です。

この関数は、対応する内部状態のマルチバイト文字関数とは以下の点で異なります。この関数には追加のパラメーターとして mbstate_t の型 を指す ps ポインターがあり、このポインターは、関連するマルチバイト文字シーケンスの現在の変換状態を完全に表すことができるオブジェクトを指します。 ps が NULL ポインターの場合、 mbrlen()mblen()のように動作します。

mbrlen() は、再始動可能なバージョンの mblen()です。 つまり、シフト状態情報は引数の 1 つとして渡され (ps は初期シフトを表します)、終了時に更新されます。 mbrlen()では、シフト状態情報を保持していれば、あるマルチバイト・ストリングから別のマルチバイト・ストリングに切り替えることができます。

戻り値

s が NULL ポインターであり、アクティブなロケールで混合バイトのストリングが許可されている場合は、mbrlen() 関数は非ゼロを戻します。 s が NULL ポインターの場合であり、アクティブなロケールで混合バイトのストリングが許可されていない場合は、ゼロを戻します。

s が NULL ポインターでない場合、mbrlen() 関数は、以下のいずれかを戻します。

0
s が NULL ストリングの場合、s はヌル文字を指します。
正の値
続く n 以下のバイトで有効なマルチバイト文字が構成される場合。 戻り値は、マルチバイト文字を構成するバイト数です。
(size_t)-1
s が有効なマルチバイト文字を指さない場合。
(size_t)-2
続く n 以下のバイト数によって、バイトが不完全な (ただし有効である可能性もある) マルチバイト文字を形成し、すべての n バイトが処理された場合。

 /* This program is compiled with LOCALETYPE(*LOCALE) and   */
 /* SYSIFCOPT(*IFSIO)                                       */

 #include <stdio.h>
 #include <stdlib.h>
 #include <locale.h>
 #include <wchar.h>
 #include <errno.h>

 #define  LOCNAME     "/qsys.lib/JA_JP.locale"
 #define  LOCNAME_EN  "/qsys.lib/EN_US.locale"

 int main(void)
 {
     int length, sl = 0;
     char  string[10];
     mbstate_t ps = 0;
     memset(string, '\0', 10);
     string[0] = 0xC1;
     string[1] = 0x0E;
     string[2] = 0x41;
     string[3] = 0x71;
     string[4] = 0x41;
     string[5] = 0x72;
     string[6] = 0x0F;
     string[7] = 0xC2;
     /* In this first example we will find the length of       */
     /* of a multibyte character when the CCSID of locale      */
     /* associated with LC_CTYPE is 37.                        */
     /* For single byte cases the state will always            */
     /* remain in the initial state  0                         */

     if (setlocale(LC_ALL, LOCNAME_EN) == NULL)
         printf("setlocale failed.\n");

     length = mbrlen(string, MB_CUR_MAX, &ps);

     /* In this case length is 1, which is always the case for */
     /* single byte CCSID  */

     printf("length = %d, state = %d\n\n", length, ps);
     printf("MB_CUR_MAX: %d\n\n", MB_CUR_MAX);

     /* Now let's try a multibyte example.  We first must set the */
     /* locale to a multibyte locale.  We choose a locale with     */
     /* CCSID 5026  */

     if (setlocale(LC_ALL, LOCNAME) == NULL)
         printf("setlocale failed.\n");

     length = mbrlen(string, MB_CUR_MAX, &ps);

     /* The first is single byte so length is 1 and             */
     /* the state is still the initial state 0                  */

     printf("length = %d, state = %d\n\n", length, ps);
     printf("MB_CUR_MAX: %d\n\n", MB_CUR_MAX);

     sl += length;

     length = mbrlen(&string[sl], MB_CUR_MAX, &ps);

     /* The next character is a mixed byte.  Length is 3 to     */
     /* account for the shiftout 0x0e.  State is                */
     /* changed to double byte state.                           */

     printf("length = %d, state = %d\n\n", length, ps);

     sl += length;


    length = mbrlen(&string[sl], MB_CUR_MAX, &ps);

    /* The next character is also a double byte character.     */
    /* The state is changed to initial state since this was    */
    /* the last double byte character.  Length is 3 to         */
    /* account for the ending 0x0f shiftin.                    */

    printf("length = %d, state = %d\n\n", length, ps);

    sl += length;

    length = mbrlen(&string[sl], MB_CUR_MAX, &ps);

    /* The next character is single byte so length is 1 and    */
    /* state remains in initial state.                         */

    printf("length = %d, state = %d\n\n", length, ps);


 }
 /*  The output should look like this:

 length = 1, state = 0

 MB_CUR_MAX: 1

 length = 1, state = 0

 MB_CUR_MAX: 4

 length = 3, state = 2

 length = 3, state = 0

 length = 1, state = 0
                                    */

関連情報