Collating multi-byte strings

V

Victor Nazarov

Assuming my locale has enough info about codepage and multi-byte
charecters, how should I compare (collate) multi-byte strings (strings
of multi-byte charecters with zero-byte at the end) in ISO C99?

Thanks in advance
Vir
 
D

Dan Pop

In said:
Assuming my locale has enough info about codepage and multi-byte
charecters, how should I compare (collate) multi-byte strings (strings
of multi-byte charecters with zero-byte at the end) in ISO C99?

strcoll(), being declared in <string.h>, doesn't seem to be appropriate
for what you want. But I could be wrong on that, the standard being less
than clear on the issue.

In the absence of a mbscoll() function, it seems that your only
alternative is to use mbstowcs() to convert your strings to wide
character strings and then use wcscoll() to compare them.

Dan
 
V

Victor Nazarov

Dan Pop ÐÉÛÅÔ:
strcoll(), being declared in <string.h>, doesn't seem to be appropriate
for what you want. But I could be wrong on that, the standard being less
than clear on the issue.

In the absence of a mbscoll() function, it seems that your only
alternative is to use mbstowcs() to convert your strings to wide
character strings and then use wcscoll() to compare them.

Dan

what about:
#include <stdlib.h>
#include <wchar.h>

int my_mbscoll (const char *s, const char *t)
{
wchar_t ws[2], wt[2];
int res;

do {
s += mbtowc (&ws[0], s, MB_CUR_MAX);
t += mbtowc (&wt[0], t, MB_CUR_MAX);
ws[1] = wt[1] = 0;
res = wcscoll (ws, wt);
} while (ws[0] && res == 0);
return res;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,139
Messages
2,570,806
Members
47,352
Latest member
Maricruz09

Latest Threads

Top