Transforms strcoll to strcmp!

The jaw-breakingly-named string.h function (it doesn't appear to be ISO C) "strxfrm" transforms one string to another string, such that strxfrm'ed strings can be compared using strcmp instead of strcoll.

strcmp and strcoll both compare strings. The difference is that strcmp uses lexicographic ordering on bytes, while strcoll compares in a locale-sensitive manner. If string s contains "Être" and t contains "Est", for instance, (and if you are are writing a French dictionary, and accordingly have set a French locale...), you'd like s and t to appear quite close together. strcmp will put "Zoë" between them, but strcoll will not.

Unfortunately, you cannot always use strcoll. Maybe you have inherited some old code which uses strcmp. (Maybe you don't even have the source code any longer!). Maybe strcoll is too slow for you -- accents are relatively rare, and strcoll needs to take them into account every time it compares a string. Wouldn't it be nice if you could still use strcmp on localised strings?

That's just what

size_t strxfrm(char *s1, const char *s2, size_t n);
does! It transforms the locale-sensitive string in s2 into the array s1 of size n, such that if s1 is the strxfrmed version of s2 and t1 is the strxfrmed version of s2, then strcmp(s1,t1) == strcoll(s2,t2). Of course, the transformed version might require more space -- you cannot rely on strlen(s1)==strlen(s2). Instead, you tell strxfrm how big a buffer you have, and it copies up to that many characters into the buffer. It also returns how big a buffer you need. (So, like many similar functions, you can call it once with a NULL pointer and size 0, and use the return value +1 to allocate a buffer of the right size.)

strxfrm is one of those functions you never use...

...until you cannot do without it!

(I've never used it)

Log in or registerto write something here or to contact authors.