Examples
The following example uses the wcscoll subroutine to compare two wide character strings based on their collation weights:
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <stdlib.h>
extern int errno;
main()
{
wchar_t *pwcs1, *pwcs2;
size_t n;
(void)setlocale(LC_ALL, "");
/* set it to zero for checking errors on wcscoll */
errno = 0;
/*
** Let pwcs1 and pwcs2 be two wide character strings to
** compare.
*/
n = wcscoll(pwcs1, pwcs2);
/*
** If errno is set then it indicates some
** collation error.
*/
if(errno != 0){
/* error has occurred... handle error ...*/
}
}
The following example uses the wcsxfrm subroutine to compare two wide character strings based on collation weights:
Note: Determining the size n (where n is a number) of
the transformed string, when using the wcsxfrm subroutine,
can be accomplished in one of the following ways:
- For each character in the wide character string, the number of bytes for possible collation values cannot exceed the COLL_WEIGHTS_MAX * sizeof(wchar_t) value. This value, multiplied by the number of wide character codes, gives the buffer length needed. To the buffer length add 1 for the terminating wide character null. This strategy may slow down performance.
- Estimate the byte-length needed. If the previously obtained value is not enough, increase it. This may not satisfy all strings but gives maximum performance.
- Call the wcsxfrm subroutine twice: first to find the value of n, and a second time to transform the string using this n value. This strategy slows down performance because the wcsxfrm subroutine is called twice. However, it yields a precise value for the buffer size needed to store the transformed string.
The method you choose depends on the characteristics of the strings used in the program and the performance objectives of the program.
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <stdlib.h>
main()
{
wchar_t *pwcs1, *pwcs2, *pwcs3, *pwcs4;
size_t n, retval;
(void)setlocale(LC_ALL, "");
/*
** Let the string pointed to by pwcs1 and pwcs3 be the
** wide character arrays to store the transformed wide
** character strings. Let the strings pointed to by pwcs2
** and pwcs4 be the wide character strings to compare based
** on the collation values of the wide characters in these
** strings.
** Let n be large enough (say,BUFSIZ) to transform the two
** wide character strings specified by pwcs2 and pwcs4.
**
** Note:
** In practice, it is best to call wcsxfrm if the wide
** character string is to be compared several times to
** different wide character strings.
*/
do {
retval = wcsxfrm(pwcs1, pwcs2, n);
if(retval == (size_t)-1){
/* error has occurred. */
/* Process the error if needed */
break;
}
if(retval >= n ){
/*
** Increase the value of n and use a bigger buffer pwcs1.
*/
}
}while (retval >= n);
do {
retval = wcsxfrm(pwcs3, pwcs4, n);
if (retval == (size_t)-1){
/* error has occurred. */
/* Process the error if needed */
break;
if(retval >= n){
/*Increase the value of n and use a bigger buffer pwcs3.*/
}
}while (retval >= n);
retval = wcscmp(pwcs1, pwcs3);
/* retval has the result */
}