fork download
  1. #include <cassert>
  2. #include <cwctype>
  3. #include <cstdlib>
  4. #include <iomanip>
  5. #include <iostream>
  6. #include <locale>
  7. #include <string>
  8.  
  9. using std::size_t;
  10.  
  11. std::u16string make_u16string( const std::wstring& ws )
  12. /* Creates a UTF-16 string from a wide-character string. Any wide characters
  13.  * outside the allowed range of UTF-16 are mapped to the sentinel value U+FFFD,
  14.  * per the Unicode documentation. (http://w...content-available-to-author-only...e.org/faq/private_use.html
  15.  * retrieved 12 March 2017.) Unpaired surrogates in ws are also converted to
  16.  * sentinel values. Noncharacters, however, are left intact. As a fallback,
  17.  * if wide characters are the same size as char16_t, this does a more trivial
  18.  * construction using that implicit conversion.
  19.  */
  20. {
  21. /* We assume that, if this test passes, a wide-character string is already
  22.   * UTF-16, or at least converts to it implicitly without needing surrogate
  23.   * pairs.
  24.   */
  25. if ( sizeof(wchar_t) == sizeof(char16_t) ) {
  26. return std::u16string( ws.begin(), ws.end() );
  27. } else {
  28. /* The conversion from UTF-32 to UTF-16 might possibly require surrogates.
  29.   * A surrogate pair suffices to represent all wide characters, because all
  30.   * characters outside the range will be mapped to the sentinel value
  31.   * U+FFFD. Add one character for the terminating NUL.
  32.   */
  33. const size_t max_len = 2 * ws.length() + 1;
  34. // Our temporary UTF-16 string.
  35. std::u16string result;
  36.  
  37. result.reserve(max_len);
  38.  
  39. for ( const wchar_t& wc : ws ) {
  40. const std::wint_t chr = wc;
  41.  
  42. if ( chr < 0 || chr > 0x10FFFF || (chr >= 0xD800 && chr <= 0xDFFF) ) {
  43. // Invalid code point. Replace with sentinel, per Unicode standard:
  44. constexpr char16_t sentinel = u'\uFFFD';
  45. result.push_back(sentinel);
  46. } else if ( chr < 0x10000UL ) { // In the BMP.
  47. result.push_back(static_cast<char16_t>(wc));
  48. } else {
  49. assert( chr <= 0x10FFFFUL ); // Quick and dirty error-checking.
  50. const char16_t leading = static_cast<char16_t>(
  51. ((chr-0x10000UL) / 0x400U) + 0xD800U );
  52. const char16_t trailing = static_cast<char16_t>(
  53. ((chr-0x10000UL) % 0x400U) + 0xDC00U );
  54.  
  55. result.append({leading, trailing});
  56. } // end if
  57. } // end for
  58.  
  59. /* The returned string is shrunken to fit, which might not be the Right
  60.   * Thing if there is more to be added to the string.
  61.   */
  62. result.shrink_to_fit();
  63.  
  64. // We depend here on the compiler to optimize the move constructor.
  65. return result;
  66. } // end if
  67. // Not reached.
  68. }
  69.  
  70. int main(void)
  71. {
  72. static const std::wstring wtest(L"☪☮∈✡℩☯✝ \U0001F644");
  73. static const std::u16string u16test(u"☪☮∈✡℩☯✝ \U0001F644");
  74. const std::u16string converted = make_u16string(wtest);
  75.  
  76. // The correct locale name may vary by OS, e.g., "en_US.utf8".
  77. constexpr char locale_name[] = "";
  78. // Some online compilers need the next line commented out:
  79. std::locale::global(std::locale(locale_name));
  80. std::wcout.imbue(std::locale());
  81.  
  82. std::wcout << L"sizeof(wchar_t) == " << sizeof(wchar_t) << L".\n";
  83.  
  84. for( size_t i = 0; i <= u16test.length(); ++i ) {
  85. if ( u16test[i] != converted[i] ) {
  86. std::wcout << std::hex << std::showbase
  87. << std::right << std::setfill(L'0')
  88. << std::setw(4) << (unsigned)converted[i] << L" ≠ "
  89. << std::setw(4) << (unsigned)u16test[i] << L" at "
  90. << i << L'.' << std::endl;
  91. return EXIT_FAILURE;
  92. } // end if
  93. } // end for
  94.  
  95. std::wcout << wtest << std::endl;
  96.  
  97. return EXIT_SUCCESS;
  98. }
Success #stdin #stdout 0s 16920KB
stdin
Standard input is empty
stdout
sizeof(wchar_t) == 4.
☪☮∈✡℩☯✝ 🙄