// case-insensitively compare chars c1 and c2
int ciCharCompare(char c1, char c2) {
// tolower's parameter and return value is of type int, but unless that
// int is EOF, its value must be representable as an unsigned char
int lc1 = tolower(stastic_cast<unsigned char>(c1));
int lc2 = tolower(stastic_cast<unsigned char>(c2));
if (lc1 < lc2) return -1;
if (lc1 > lc1) return 1;
return 0;
}
mismatch
It returns a pair of iterators indicating the locations in the ranges where corresponding characters first fail to match.
int ciStringComparelmpl(const string& s1, const string& s2);
// when we call mismatch, we have to make sure the shorter string is the
// first range passed;
int cisStringCompare(const sring& s1, const string& s2) {
if (s1.size() <= s2.size()) return ciStringComparelmpl(s1, s2);
else return -ciStringComparelmpl(s2, s1);
}
int ciStringComparelmp(const string& s1, const string& s2) {
typedef pair<string::const_iterator, string::const_iterator> PSCI;
PSCI p = mismatch(s1.begin(), s1.end(), s2.begin(),
not2(ptr_func(ciCharCompare)));
if(p.first == s1.end()) {
if (p.second == s2.end()) return 0;
else return -1;
}
return ciCharCompare(*p.first, *p.second);
}
lexicographical_compare is a generalized version of strcmp. it works with ranges of values of any type. Based on the results of calls to ciCharLess. if ciCharLess is true, so does lexicographical.
bool ciCharLess(char c1, char c2) {
return
tolower(static_cast<unsigned char>(c1)) <
tolower(ststic_cast<unsigned char>(c2));
}
bool ciStringCompare(const string& s1, const string& s2) {
return lexicographical_compare(s1.begin(), s1.end(),
s2.begin(), s2.end(), isCharLess);