Internationalization and localization tools


Locale-Sensitive C/C++ Collation Function

int strnicmp(const char* string1, const char* string2, size_t count);

int _strnicmp(const char* string1, const char* string2, size_t count);

int _wcsnicmp(const unsigned wchar_t* string1, const unsigned wchar_t* string2, size_t count);

int _mbsnicmp(const unsigned char* string1, const unsigned char* string2, size_t count);

int _tcsncicmp(const _TXCHAR* string1, const _TXCHAR* string2, size_t count);

int _mbsnbicmp(const unsigned char* string1, const unsigned char* string2, size_t count);

int _tcsnicmp(const _TXCHAR* string1, const _TXCHAR* string2, size_t count);

Internationalization (I18n) Function Overview

The strnicmp function does a case-insensitive compare of at most count characters of two null-terminated strings. It returns zero if the strings are equal, a negative number if string1 is less than string2 and a positive number if string1 is greater than string2. If one string is a prefix of the other, the longer string is greater than the shorter string.

All of these functions, with the exception of strnicmp, are only supported on Windows platforms.

_wcsnicmp is the wide character version of _strnicmp that performs a case-insensitive compare of at most count wide characters of the two wide-character string arguments.

_mbsnicmp and _mbsnbicmp are multibyte versions of _strnicmp. _mbsnicmp will compare at most count multibyte characters and _mbsnbicmp will compare at most count bytes. They both use the current multibyte code page.

_tcsncicmp and _tcsnicmp are the corresponding Generic functions for _mbsnicmp and _mbsnbicmp, respectively.

I18n Issues

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

The single-byte versions of these functions, strnicmp and _strnicmp, should not be used for equality comparison because they will only work with single-byte characters. On Windows platforms, call one of the _mbs functions or _wcsnicmp. On ANSI UTF-8 platforms, convert the UTF-8 strings to wide character strings and then call wcscasecmp on the wide strings. On ANSI UTF-16 platforms, call wcscasecmp. Although wcscasecmp does not support a partial string compare, it is case-insensitive.

In addition, these functions should not be used for character collation, i.e. alphabetization, because outside of the ASCII subset of characters, they will not sort according to the character set sort order rules of a specific locale. Instead, use one of the collation functions listed below.

Ensure that the count argument value is correct for the chosen platform and function call; the number of bytes for _mbsnbicmp, the number of multibyte characters for _mbsnicmp, and the number of wide characters for _wcsnicmp. See Locale-Sensitive Length Functions for a discussion on multibyte and wide character sizes.

Prior to calling one of these functions, ensure that the current locale is set properly by calling setlocale, as string comparison is dependent on the LC_CTYPE locale category.

In the case of the Windows-only multibyte and generic versions of these functions, ensure that the multibyte code page is set correctly. See _setmbcp for more information on setting up the multibyte code page.

Recommended Replacements*

For equality comparisons use

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

For purposes of character collation, there are multibyte and wide functions for handling international sorting. These functions differ from the traditional strnicmp in that they use locale-specific sorting rather than collating by a character's encoded value. The value assigned to LC_COLLATE within the machine's environment when the program runs dictates the locale-specific sorting rules drawn from by the collation functions.

For collation use

Collation Functions

 

Lingoport internationalization and localization services and software