12

I tried understanding both of them but I did not find any differences except for strcoll() this reference says that it

compares two null terminated strings according to current locale as defined by the LC_COLLATE category.

On the second thoughts and I know I am asking another question for detailed answer, what exactly is this locale, for both C and C++?

2
  • 10
    The example here seems pretty clear in my opinion.
    – Rapptz
    Dec 30, 2012 at 0:01
  • @Rapptz...Oops...should have seen that link....before posting...
    – Recker
    Dec 30, 2012 at 0:05

2 Answers 2

31

strcmp() takes the bytes of the string one by one and compare them as is whatever the bytes are.

strcoll() takes the bytes, transform them using the locale, then compares the result. The transformation re-orders depending on the language. In French, accentuated letters come after the non-accentuated ones. So é is after e. However, é is before f. strcoll() gets it right. strcmp() not so well.

However, in many cases strcmp() is enough because you don't need to show the result ordered in the language (locale) in use. For example, if you just need to quickly access a large number of data indexed by a string you'd use a map indexed by that string. It probably is totally useless to sort those using strcoll() which is generally very slow (in comparison to strcmp() at least.)

For details about characters you may also want to check out the Unicode website.

In regard to the locale, it's the language. By default it is set to "C" (more or less, no locale). Once you select a location the locale is set accordingly. You can also set the LC_LOCALE environment variable. There are actually many such variables. But in general you use predefined functions that automatically take those variables in account and do the right thing for you. (i.e. format dates / time, format numbers / measures, compute upper / lower case, etc.)

1
  • 1
    +1 The samples of character comparison in this answer are very nicely presented, particularly the "However, é is before f." example. Likewise, the notes about using strcmp() for internal sort management vs display are equally well done. There are some answers that are succinct and speak volumes, and I wish I could up-vote them more than once. This is one of them.
    – WhozCraig
    Dec 30, 2012 at 1:32
2

For some reason in all unicode locales I tested, on several different versions of glibc, strcoll() returns zero for any two hiraganas. This breaks sort, uniq, and everything that interacts with orders of strings in some way.

$ echo -e -n 'い\nろ\nは\nに\nほ\nへ\nと\n' | sort | uniq

which is simply broken beyond repair. People from different places of world might have different ideas on whether 'い' should be placed before or after 'ろ', but nobody sane would consider them the same.

And no, setting your locale to the Japanese one does not matter:

$ LC_ALL=ja_JP.utf8 LANG=ja_JP.utf8 LC_COLLATE=ja_JP.utf8 echo -e -n 'い\nろ\nは\nに\nほ\nへ\nと\n' | sort | uniq

There was discussion in some official mailing list, but guess what, it was in 2002 and it was never fixed because people don't care: https://www.mail-archive.com/linux-utf8@nl.linux.org/msg02658.html

That bug happened to us in some day and in the end our only way out was to set the collate locale to "C" and rely on the nice properties of utf-8 encoding. That's a horrible experience, since one shouldn't really work under "C" locale when processing all-Japanese data.

So for your sanity's sake, do NOT directly use strcoll. A safer variant might be:

int safe_strcoll(const char *a, const char *b)
{
  int ret = strcoll(a, b);
  if (ret != 0) return ret;
  return strcmp(a, b);
}

just in case strcoll() decides to screw you...

EDIT: I just repeated the experiment out of curiosity, and my current system (with glibc 2.29) works without problems now. Locale doesn't matter either.

2
  • 3
    I was lucky enough to spot the problem. Your change of environment only applies to echo. You should apply the change of language to sort and to uniq as well; the simplest is just to export LANG. It works perfectly with: (export LANG=ja_JP.UTF-8; echo -e -n 'い\nろ\nは\nに\nほ\nへ\nと\n' | sort | uniq).
    – qsantos
    Jun 26, 2017 at 18:43
  • 1
    Thank you for pointing that out. Except I was actually using export LANG=ja_JP.UTF-8 when first doing this experiment. I tried again on multiple machines and can still reproduce the problem in some, but not all, of them. I guess I need to dig a bit further as what caused the difference. glibc version seems not the only variable I need to control...
    – Yì Yáng
    Jul 1, 2017 at 2:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.