Is the strcasecmp algorithm flawed?
Other links, http://man7.org/linux/man-pages/man3/strcasecmp.3p.html for strcasecmp say that converting to lower-case is the correct behavior (at least in POSIX locale).
The reason for that behavior is that if you use strcasecmp to sort an array of strings it is needed to get reasonable results.
Otherwise if you try to sort "A", "C", "_", "b" using e.g., qsort the result would depend on the order of comparisons.
The behavior is correct.
Per the POSIX str\[n\]casecmp()
specification:
When the
LC_CTYPE
category of the locale being used is from the POSIX locale, these functions shall behave as if the strings had been converted to lowercase and then a byte comparison performed. Otherwise, the results are unspecified.
That is also part of the NOTES section of the Linux man page:
The POSIX.1-2008 standard says of these functions:
When the LC_CTYPE category of the locale being used is from the POSIX locale, these functions shall behave as if the strings had been converted to lowercase and then a byte comparison performed. Otherwise, the results are unspecified.
Why?
As @HansOlsson pointed out in his answer, doing case-insensitive comparisons between only letters and allowing all other comparisons to have their "natural" results as done in strcmp()
would break sorting.
If 'A' == 'a'
(the definition of a case-insensitive comparison) then '_' > 'A'
and '_' < 'a'
(the "natural" results in the ASCII character set) can not both be true.