mbrlen ()- 判定多位元組字元的長度 (可重新啟動)

格式

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

語言層次

ANSI

安全執行緒

是,如果 ps 不是空值。

區分語言環境

此函數的行為可能受現行語言環境的 LC_CTYPE 種類影響。 如果在編譯指令上指定 LOCALETYPE (*LOCALEUCS2) 或 LOCALETYPE (*LOCALEUTF) ,則現行語言環境的 LC_UNI_CTYPE 種類也可能影響此函數。 在編譯指令上指定 LOCALETYPE (*CLD) 時,無法使用此函數。 如需相關資訊,請參閱 瞭解 CCSID 及語言環境

說明

此功能是可重新啟動的 mblen()版本。

mbrlen() 函數決定多位元組字元的長度。

n 是要檢查之多位元組字串的位元組數 (最多)。

此函數與其對應的內部狀態多位元組字元函數不同,因為它有一個額外參數 ps (類型為 mbstate_t 的指標) ,指向可完整說明相關聯多位元組字元序列之現行轉換狀態的物件。 如果 ps 是 NULL 指標,則 mbrlen() 行為類似於 mblen()

mbrlen() 是可重新啟動的 mblen()版本。 換句話說,移位狀態資訊作為其中一個引數傳遞 (ps 代表起始移位) ,並在結束時更新。 使用 mbrlen(),您可以從一個多位元組字串切換至另一個多位元組字串,前提是您已保留班次狀態資訊。

回覆值

如果 s 是空值指標,且作用中語言環境容許混合位元組字串,則 mbrlen() 函數會傳回非零。 如果 s 是空值指標,且作用中語言環境不容許混合位元組字串,則會傳回零。

如果 s 不是空值指標,則 mbrlen() 函數會傳回下列其中一項:

0
如果 s 是 NULL 字串 (s 指向 NULL 字元)。
正數
如果下一個 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
                                    */

相關資訊