Re: sorting std::vector<string> ignoring case
- From: "Doug Harrison [MVP]" <dsh@xxxxxxxx>
- Date: Wed, 09 May 2007 22:27:52 -0500
On Wed, 9 May 2007 00:18:22 -0300, "Cholo Lennon" <chololennon@xxxxxxxxxxx>
wrote:
I'm sorry for my wrong expression, with 'portable' I wanted to say ANSI compatible. Also I didn't say that the original names has
been removed. They exist yet. Still is valid to use them in VC8 but, like you appointed, they are deprecated.
This is extremely nitpicking, but I believe stricmp and _stricmp are
equally "ANSI compatible", and I gave the reason in the "P.S." part of my
last message. I think the important point is to realize that while declared
in <string.h>, neither are Standard C functions.
FWIW, I mentioned using the Windows function CompareString in my other
message largely because string comparison seems to get more and more
intricate every day, and Windows goes quite a bit beyond C locales, which
influence the behavior of functions like stricmp. For example, I used to
think that doing a setlocale(LC_ALL, "") at program startup was a necessary
first step without any ramifications, but then I looked at how to compare
English strings when searching a presorted data table. Naively, I thought
stricmp was good enough, but then I came across a case in which words were
separated by underscores. Not knowing how underscores collate with other
characters under all conditions, I came across CompareString, which says:
http://msdn2.microsoft.com/en-us/library/ms647476.aspx
<q>
When the results of the comparison should be consistent regardless of
locale, for example, when comparing retrieved data against a predefined
list or an internal value, use CompareString or CompareStringEx. Either of
the following calls will match even if mystr is "INLAP", whereas the
locale-sensitive call to lstrcmpi fails if the current locale is
Vietnamese.
</q>
So apparently even pure English doesn't sort the same in a case-insensitive
way across all locales when using functions like lstrcmpi and probably by
extension, stricmp. So to use stricmp in this scenario, I'd have to undo
and reset my C-level locale for every comparison. What a mess. I replaced
it with the "invariant" code suggested by the CompareString documentation.
I'd be interested to hear from anyone who knows how to approach this with
C++ locales.
--
Doug Harrison
Visual C++ MVP
.
- Follow-Ups:
- Re: sorting std::vector<string> ignoring case
- From: Jerry Coffin
- Re: sorting std::vector<string> ignoring case
- From: Darko Miletic
- Re: sorting std::vector<string> ignoring case
- From: Cholo Lennon
- Re: sorting std::vector<string> ignoring case
- References:
- sorting std::vector<string> ignoring case
- From: Ed
- Re: sorting std::vector<string> ignoring case
- From: Darko Miletic
- Re: sorting std::vector<string> ignoring case
- From: Cholo Lennon
- Re: sorting std::vector<string> ignoring case
- From: Doug Harrison [MVP]
- Re: sorting std::vector<string> ignoring case
- From: Cholo Lennon
- sorting std::vector<string> ignoring case
- Prev by Date: Re: sorting std::vector<string> ignoring case
- Next by Date: Re: sorting std::vector<string> ignoring case
- Previous by thread: Re: sorting std::vector<string> ignoring case
- Next by thread: Re: sorting std::vector<string> ignoring case
- Index(es):
Relevant Pages
|
Loading