Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toupper/tolower + locale (german)

Tags:

c++

how to convert a string (wstring) from lowercase to uppercase characters and vice versa? I searched the net and found there is a STL-function std::transform.
But until now I hav'nt figured out how to give the right locale-object for example "Germany_german" to the function. Who can help please? my code looks like:

wstring strin = L"ABCÄÖÜabcäöü";
wstring str = strin;
locale loc( "Germany_german" ); // ??? how to apply this ???
std::transform( str.begin(), str.end(), str.begin(), (int(*)(int)tolower );
//result: "abcäöüabcäöü"

The characters ÄÖÜ and äöü (it's like Ae, Oe, Ue) will not be converted correctly.

P.S.: I don't prefer a big switch sweat and also I know BOOST is capable of everything, i would prefer a STL solution.

thanks in advance
Oops

like image 417
Oops Avatar asked Dec 09 '22 16:12

Oops


2 Answers

You need to apply it in different way:

for(unsigned i=0;i<str.size();i++)
   str[i]=std::toupper(str[i],loc);

Or set global locale

std::locale::global(std::locale("Germany_german"));

and then

std::transform( str.begin(), str.end(), str.begin(), std::toupper );

See: http://www.cplusplus.com/reference/std/locale/tolower/

Note 1 C toupper is different from std::toupper which receives std::locale as parameter it uses char only as parameter while std::toupper works on both char and wchar_t.

Note 2 std::locale is quite broken for German because it works on character basis and can't convert "ß" to "SS" but it would work for most of other characters...

Note 3: If you need correct case conversion including handling characters like "ß", you need use good localization library like ICU, or Boost.Locale

like image 158
Artyom Avatar answered Dec 24 '22 13:12

Artyom


The problem is that tolower()/toupper() are fundamentally broken for many locales. How do you propose toupper() should work with words containing an 'ß'? What would be the all-upper-case of "Maße"?[1] There are similar examples in French and other languages.

So the first thing to ask yourself is why you need this and what you want to achieve.

If you're willing to ignore all these problems, here is the first treatment on the topic google found me. (If you're going to google for more of this, James Kanze has over the years posted many many useful insights into this problem in quite a few of the C++ newsgroups.)

[1] A hint for the few non-Germans hanging out here :): In German, the "ß", literally a ligature of "sz", is not available as a capital letter. It can be replaced by "ss", but not always. For example, "Masse" is a completely different word than "Maße".

like image 42
sbi Avatar answered Dec 24 '22 14:12

sbi