In Objective-C, it's dead simple:
NSLog(@"%@", [@"BAÑO" lowercaseString]); // Outputs "baño".
In C++, what's the equivalent? Can anyone provide valid code for this that produces the same output? Is there a nice STL way to do this without relying on ICU, Boost, or any other 3rd party libs?
My current non-solution is:
using namespace std;
string s = "BAÑO";
wstring w(s.begin(), s.end());
transform(w.begin(), w.end(), w.begin(), towlower);
// w contains "baÑo"
The problem turns out to be incredibly complicated in C++. There's only one library I know of that gets it absolutely right taking into consideration unicode normalization and other non-lower-128-ASCII character point issues.
IBM's ICU
It's massive but it does everything correctly. toupper and tolower fall short in this issue unfortunately and there's no other C++ construct available.
There is tolower, which is locale specific, but I don't think it'll work with UTF-8 strings.
The correct solution will always be locale specific, because the case rules depend on the language. For example, the lowercase version of 'I' is not always 'i'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With