Nothing. As it is, I got off my ass this weekend and eliminated the
rename and implemented the strcasecmp function within the application,
conditional on _WIN32 as I posted here earlier. A hack is a hack and
it was time to eliminate that one.
Since we're in comp.lang.c++, what do you think of this implementation
of a case-insensitive compare of C++ strings?
int stringcasecmp(const std::string s1, const std::string s2)
{
std::string us1 = s1;
std::string us2 = s2;
std::transform(us1.begin(), us1.end(), us1.begin(), ::tolower);
std::transform(us2.begin(), us2.end(), us2.begin(), ::tolower);
return us1.compare(us2);
}
This calls C function tolower(int), which has undefined behavior in case
of negative arguments. As the plain char type used by std::string is
often a signed type it can easily contain negative values. So this
implementation seems quite dangerous.
Even if the correct C++ std::tolower() function were used, it could not
properly support multibyte characters like UTF-8 encoding. I am even not
talking about comparing German ß and SS.
It also makes copies of the both strings (twice, as the parameters are
not references), then makes two passes through both strings, which may
become suboptimal if the strings do not fit in the cpu caches.
As the above function basically works only for ASCII (never seen EBCDIC
in real life), it would be best to acknowledge this; then calling the
locale-specific ::tolower would be wrong and also not necessary. Improved
version:
int strcasecmp_ascii(const std::string& s1, const std::string& s2)
{
const std::string::size_type n = std::min(s1.length(), s2.length());
for (std::string::size_type i = 0; i<n; ++i) {
char c1 = s1
, c2=s2;
if (c1>='A' && c1<='Z') {
c1+=32;
}
if (c2>='A' && c2<='Z') {
c2+=32;
}
if (c1<c2) {
return -1;
} else if (c1>c2) {
return 1;
}
}
return s1.length()>n? 1: (s2.length()>n? -1: 0);
}