Internationalization and localization tools


Locale-Sensitive C/C++ Collation Functions

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

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

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

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

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

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

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

Internationalization (I18n) Function Overview

The strncmp function compares 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.

wcsncmp is the wide character version of strncmp that will compare at most count wide characters of the two wide-character string arguments.

Supported on Windows platforms only, _mbsncmp and _mbsnbcmp are multibyte versions of strncmp. _mbsncmp will compare at most count multibyte characters and _mbsnbcmp will compare at most count bytes. They both use the current multibyte code page.

_tcsnccmp and _tcsncmp are the corresponding Generic functions for _mbsncmp and _mbsnbcmp, respectively. _tccmp is equivalent to _tcsnccmp.

I18n Issues

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

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.

On ANSI UTF-8 platforms, strncmp will work correctly for an equality comparison. However, on Windows platforms, use _mbsncmp or wcsncmp for a multibyte or wide character comparisons.

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

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 strncmp 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