Namespaces
Variants
Views
Actions

Talk:cpp/string/wide/towupper

From cppreference.com

Recommended expansion of example

Example

#include <iostream>
 
int main() {
    std::locale::global(std::locale("en_US.utf8"));
 
    wchar_t x = L'\U0001044F'; // largest code-number of a lowercase unicode letter, that has a (different) uppercase representation (see http://www.open-std.org/JTC1/SC35/WG5/docs/30112d10.pdf )
    std::wcout << "lower:\t" << x                                      << std::endl;
    std::wcout << "upper:\t" << static_cast<wchar_t>(std::towupper(x)) << std::endl;
 
    std::wstring s = L\U0001044E\U0001044F";
    std::wcout << "lower:\t" << s << std::endl;
    auto& f = std::use_facet<std::ctype<wchar_t>>(std::locale());
    f.toupper(&s[0], &s[0]+s.size());
    std::wcout << "upper:\t" << s << std::endl;
    return 0;
}

Output:

lower:	𐑏
upper:	𐐧
lower:	ä𐑎𐑏
upper:	Ä𐐦𐐧

User706 (talk) 13:34, 25 November 2018 (PST)

if you are suggesting adding an example demonstrating the use of std::ctype::toupper, it already exists at that page. --Cubbi (talk) 06:41, 26 November 2018 (PST)

PS:

What I'd love, is an example which does exactly the same thing, but without wchar_t (and without std::wcout); instead using unicode and std::cout. Something like this https://wandbox.org/permlink/EkqJaVjaYd1CHcc9 But can it not be made shorter? (Does one really need the "long" routines from the github gist?)

Any tipps? Thanks.


User706 (talk) 13:43, 25 November 2018 (PST)

you could avoid those "long routines" by using std::wstring_convert::to_bytes and std::wstring_convert::from_bytes --Cubbi (talk) 06:41, 26 November 2018 (PST)