Locale-Sensitive Length Function
size_t strnlen(const char *str, size_t maxSize);
size_t wcsnlen(const wchar_t *str, size_t maxSize);
size_t _mbsnlen(const unsigned char *str, size_t maxSize);
size_t _mbstrnlen(const char *str, size_t maxSize);
size_t _tcsnlen(const TCHAR *str, size_t maxSize);
size_t _tcscnlen(const _TXCHAR *str, size_t maxSize);
Internationalization (I18n) Function Overview
The strnlen function returns the length of the string str
in bytes if the length is smaller than maxSize bytes.
Otherwise it returns maxSize . Therefore, this function is equivalent to
(strlen(str) < maxSize ? strlen(str) : maxSize) , but it is more efficient
and works even if the string str is not null-terminated.
wcsnlen is the wide character equivalent of strnlen .
The maxSize argument specifies the maximum number of wide characters.
_mbsnlen and _mbstrnlen are supported on Windows platforms only
and return the number of multibyte characters in a multibyte-character string.
_mbsnlen recognizes multibyte-character sequences according to the multibyte code
page currently in use, but it does not test for multibyte-character validity. _mbstrnlen
tests for multibyte-character validity and recognizes multibyte-character sequences;
it returns -1 in the event of an invalid multibyte character.
_tcsnlen and _tcscnlen are the Generic versions of the function;
when the Windows _MBCS compiler flag is set, _tcsnlen maps to
strnlen and _tcscnlen maps to _mbsnlen .
When the Windows _UNICODE flag is set, the Generic functions
both map to wcsnlen . Note that there is no generic version that maps to _mbstrnlen .
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.
On ANSI UTF-8 platforms, there is no multibyte version of strnlen ; the return will be the number of bytes
in the multibyte string, rather than the number of multibyte characters. To calculate the number of multibyte characters,
convert the multibyte string to a wide-character string, and then call wcsnlen .
On Windows MBCS platforms, ensure that the current locale and the
multibyte code page are set properly as they are used in the validation and calculation of the
string's length.
On Windows Generic platforms, choose _tcscnlen rather than _tcsnlen when desiring the number of multibyte characters, since _tcscnlen will map to _mbsnlen when the _MBCS compiler flag is set,
whereas _tcsnlen will map to strnlen .
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
|