Internationalization and localization tools


Locale-Sensitive Length Function

size_t strnlen_l(const char *str, size_t maxSize, _locale_t locale);

size_t wcsnlen_l(const wchar_t *str, size_t maxSize, _locale_t locale);

size_t _mbsnlen(const unsigned char *str, size_t maxSize, _locale_t locale);

size_t _mbstrnlen_l(const char *str, size_t maxSize, _locale_t locale);

size_t _tcscnlen_l(const _TXCHAR *str, size_t maxSize, _locale_t locale);

Internationalization (I18n) Function Overview

The strnlen_l function is similar to strnlen in that it returns the length of the string str in bytes if the length is smaller than maxSize bytes; otherwise it returns maxSize. However, these set of functions also pass in the locale to be used by the multibyte versions to identify and validate the multibyte characters.

wcsnlen_l is the wide character equivalent of strnlen_l. The maxSize argument specifies the maximum number of wide characters.

_mbsnlen_l and _mbstrnlen_l return the number of multibyte characters in a multibyte-character string. _mbsnlen_l recognizes multibyte-character sequences according to the multibyte code page currently in use, but it does not test for multibyte-character validity. _mbstrnlen_l tests for multibyte-character validity and recognizes multibyte-character sequences; it returns -1 in the event of an invalid multibyte character.

_tcscnlen_l is the Generic versions of the function; mapping to _mbsnlen_l when the Windows _MBCS compiler flag is set, and to wcsnlen_l when the Windows _UNICODE flag is set. Note that there is no generic version that maps to _mbstrlen_l.

I18n Issues

Use the appropriate version of the function as required for internationalization support, noting the following:

Ensure that the value of the maxSize argument is correct for the chosen i18n platform; the maximum number of single-byte, multibyte, or wide characters.

Ensure that the correct locale is passed in.

In addition, set the multibyte code page prior to calling the MBCS functions _mbsnlen_l or _mbstrnlen_l.

Recommended Replacements*

*If you're already using the recommended function, see I18n Issues for other reasons why Globalyzer is detecting the function.

Locale-Sensitive Length Functions

 

Lingoport internationalization and localization services and software