converting std::basic_string to upper or lower case.

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC 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




.


Quantcast