Multibyte-character-string allocation
Multibyte characters have varying lengths. When you represent a multibyte-character string in an array, the number of array elements does not equal the number of multibyte characters in the string. Therefore, you cannot use the same allocation method for multibyte strings as for single-byte strings.
Instead, you can use the following macro and functions
to help you determine how much memory a multibyte character requires.
- IFX_GL_MB_MAX
- Indicates the maximum number of bytes that any multibyte character
in any locale can occupy.
Use this macro to allocate space in static buffers that are intended to contain one or more multibyte characters.
- ifx_gl_mb_loc_max()
- Returns the maximum number of bytes that any character in the current locale can occupy.
- ifx_gl_cv_outbuflen()
- ifx_gl_case_conv_outbuflen()
- Calculates one of the following values:
- Exactly the number of bytes that are required by a destination buffer of the converted multibyte characters
- A close over-approximation of the number
For example, the following declaration statically
allocates 20 multibyte characters for the mbs string:
gl_mchar_t mbs[20 * IFX_GL_MB_MAX]; /* static allocation */
The
following declarations dynamically allocate 20 multibyte characters
for the mb1 and mb2 strings:
gl_mchar_t *mbs1 = (gl_mchar_t *) malloc(20*IFX_GL_MB_MAX);
gl_mchar_t *mbs2 = (gl_mchar_t *) malloc(20*ifx_gl_mb_loc_max());
The declaration for mb1 uses the maximum multibyte-character size. The declaration for mb2 uses the ifx_gl_mb_loc_max() function to obtain a more precise estimate for the size of 20 multibyte characters. The ifx_gl_mb_loc_max() function returns the maximum size among all characters in the current processing locale.
If
your multibyte-character string is null terminated, allocate one additional
byte for the null terminator. The following declarations allocate
three null-terminated multibyte-character strings:
/* static allocation */
gl_mchar_t mbs[20*IFX_GL_MB_MAX+1];
/* dynamic allocation with IFX_GL_MB_MAX */
gl_mchar_t *mbs1 = (gl_mchar_t *) malloc(20*IFX_GL_MB_MAX+1);
/* dynamic allocation with ifx_gl_mb_loc_max() */
gl_mchar_t *mbs2 = (gl_mchar_t *)
malloc(20*ifx_gl_mb_loc_max()+1);