Portable way to compare strings using case-insensitive

D

Diego Martins

Hi! The following code snippet:

namespace {
bool charCompare(char a, char b) {
return tolower(a) < tolower(b);
}
}

bool compareString(const std::string & s1, const std::string & s2)
{
return
std::lexicographical_compare(s1.begin(),s1.end(),s2.begin(),s2.end(),charCompare);
}

presents a compareString function to compare strings ignoring the case

compareString("yes","YES") --> true;

but

compareString("ahá","AHÁ") --> false;

how can I make it more portable to using accents, for example?

what are your background in this issue? what about using a Locale
class? (actually, I know nothing about locales in C++)

a possible idea is using classes to deal with character encode.
something like that:

compareString<ISO8859_1>("ahá","AHÁ")

and it will delegate to charCompare<T>

an alternative is to use an overload like that:
bool compareString(const std::string & s1, const std::string & s2,
const CharConv & conv)

and CharConv can be a conversion table or a class that implements
tolower() and toupper() for each desired character codification

can you comment the ideas above?

any better ideas?

Diego Martins
HP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top