converting std::basic_string to upper or lower case.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Apologies in advance on this one - I realise this is probably a common
question - but I can't seem to find a solution on the news groups.

How would you convert a basic_string to upper-case in-place? In the past
I've always done it like so:

std::string str1 = "My-/$%^Mixed_Case_String";
std::transform(str1.begin(), str1.end(), str1.begin(), ::toupper);

However, a colleague of mine has just piped up to say he's had issues with
this where he was passing a mixed case string in and some of the upper case
characters were coming back messed up. He then quoted the MSDN spiel on the
ascii version of toupper:

In order for toupper to give the expected results, __isascii and islower
must both return nonzero

This suggests that if a character is already upper (i.e. islower returned
false) then the results are undefined. Is this right or is the
documentation in error? Seems mighty strange to me...

One alternative I found was to use:
std::use_facet<std::ctype<char> >(std::locale()).toupper(&*str1.begin(),
&*str1.end());

but this is hardly pleasing on the eye - and I'm not even sure it's
guaranteed to work as I can't remember off the top of my head if the memory
behind a basic_string is guaranteed to be continuous - I have a nagging
suspicions it's not and that only the memory returned from c_str() is.

Any help on this matter would be appreciated.

Thanks

Andy




.



Relevant Pages

  • Re: how to convert characters to upper case in utf8 env.
    ... I need to convert to upper case only those characters ... It would seem improbable that toupper(), on Solaris or elsewhere, could ... UTF-32 or any other Unicode encoding variant. ...
    (comp.lang.c)
  • Re: proper case
    ... > characters from the origional are converted to ToUpper. ... Strings in .NET ... Yes, but the point is that he wanted PROPER case, not UPPER. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Function based index not used with like operator
    ... search for is starting with the characters the user search for. ... SQL> set echo on linesize 132 trimspool on ... col2 from uppertest1 where upper ... 438 bytes received via SQL*Net from client ...
    (comp.databases.oracle.server)
  • Re: The case for lower case
    ... Lower case letters contain more visual "information" than upper case ... and cover up the top half of a row of all upper case characters. ... achieve by putting white letters on a black background ... readability study in their lives ("Hey, I grew up on a greenscreen terminal ...
    (alt.lang.asm)
  • Re: The case for lower case
    ... both pure lower and upper case tend to slow down ... > Lower case letters contain more visual "information" than upper case ... > and cover up the top half of a row of all upper case characters. ... black background more pleasing than other combinations. ...
    (alt.lang.asm)