Guest User

Untitled

a guest
May 31st, 2015
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <locale>
  5. #include <iomanip>
  6. #include <string>
  7. #include <codecvt>
  8.  
  9. // utility wrapper to adapt locale-bound facets for wstring/wbuffer convert
  10. template<class Facet>
  11. struct deletable_facet : Facet
  12. {
  13.     template<class ...Args>
  14.     deletable_facet(Args&& ...args) : Facet(std::forward<Args>(args)...) {}
  15.     ~deletable_facet() {}
  16. };
  17.  
  18. int main()
  19. {
  20.     std::cout << "sizeof(char32_t) = " << sizeof(char32_t) << std::endl;
  21.     std::cout << "sizeof(wchar_t)  = " << sizeof(wchar_t)  << std::endl;
  22.  
  23.     // UTF-8 narrow multibyte encoding
  24.     std::string data = u8"z\u00df\u6c34\U0001f34c";
  25.     std::ofstream("text.txt") << data;
  26.  
  27.     // using system-supplied locale's codecvt facet
  28.     std::wifstream fin("text.txt");
  29.     // reading from wifstream will use codecvt<wchar_t, char, mbstate_t>
  30.     fin.imbue(std::locale(""));
  31.     std::cout << "The UTF-8 file contains the following UCS4 code points: \n";
  32.     for (wchar_t c; fin >> c; )
  33.         std::cout << "U+" << std::hex << std::setw(4) << std::setfill('0') << c << '\n';
  34.  
  35.     // using standard (locale-independent) codecvt facet
  36.     std::wstring_convert<
  37.         deletable_facet<std::codecvt<char32_t, char, std::mbstate_t>>, char32_t> conv32;
  38.     std::u32string str32 = conv32.from_bytes(data);
  39.  
  40.     std::cout << "The UTF-8 string contains the following UCS4 code points: \n";
  41.     for (char32_t c : str32)
  42.         std::cout << "U+" << std::hex << std::setw(4) << std::setfill('0') << c << '\n';
  43.  
  44.     std::locale::global(std::locale(""));
  45.     std::wstring_convert<
  46.         deletable_facet<std::codecvt<wchar_t, char, std::mbstate_t>>> wconv;
  47.     std::wstring wstr = wconv.from_bytes(data);
  48.     std::cout << "The UTF-8 string contains the following UCS4 code points: \n";
  49.     for (wchar_t c : wstr)
  50.         std::cout << "U+" << std::hex << std::setw(4) << std::setfill('0') << c << '\n';
  51. }
Advertisement
Add Comment
Please, Sign In to add comment