fork(2) download
  1. /*
  2. The MIT License (MIT)
  3.  
  4. Copyright (c) 2014 Johann Gerell
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7.   of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9.   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10.   copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13.   The above copyright notice and this permission notice shall be included in all
  14.   copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19.   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. SOFTWARE.
  23. */
  24. #include <locale>
  25. #include <codecvt>
  26. #include <string>
  27. #include <iostream>
  28. #include <iomanip>
  29. #include <cstdint>
  30. #include <vector>
  31. #include <algorithm>
  32. #include <iterator>
  33.  
  34. template <typename T>
  35. struct code_unit_t
  36. {
  37. T value;
  38.  
  39. code_unit_t(T value)
  40. : value(value)
  41. {
  42. }
  43. };
  44.  
  45. template <typename T>
  46. std::ostream& operator<<(std::ostream& stream, const code_unit_t<T>& code_unit)
  47. {
  48. stream
  49. << "0x"
  50. << std::hex
  51. << std::uppercase
  52. << std::setw(2 * sizeof(code_unit.value))
  53. << std::setfill('0')
  54. << static_cast<int64_t>(code_unit.value);
  55.  
  56. return stream;
  57. }
  58.  
  59. template <typename T>
  60. std::ostream& operator<<(std::ostream& stream, const std::vector<code_unit_t<T>>& code_units)
  61. {
  62. std::copy(std::begin(code_units),
  63. std::end(code_units),
  64. std::ostream_iterator<code_unit_t<T>>(stream, " "));
  65.  
  66. return stream;
  67. }
  68.  
  69. struct raw_t
  70. {
  71. std::string utf8;
  72. std::wstring utf16;
  73.  
  74. raw_t(std::string utf8, std::wstring utf16)
  75. : utf8(utf8)
  76. , utf16(utf16)
  77. {
  78. }
  79. };
  80.  
  81. struct converted_t
  82. {
  83. std::string utf8;
  84. std::wstring utf16;
  85.  
  86. converted_t(raw_t raw)
  87. {
  88. std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
  89. utf8 = converter.to_bytes(raw.utf16);
  90. utf16 = converter.from_bytes(raw.utf8);
  91. }
  92. };
  93.  
  94. struct code_units_t
  95. {
  96. std::vector<code_unit_t<uint8_t>> utf8;
  97. std::vector<code_unit_t<uint16_t>> utf16;
  98.  
  99. code_units_t(converted_t converted)
  100. {
  101. std::copy(std::begin(converted.utf8),
  102. std::end(converted.utf8),
  103. std::back_inserter(utf8));
  104.  
  105. std::copy(std::begin(converted.utf16),
  106. std::end(converted.utf16),
  107. std::back_inserter(utf16));
  108. }
  109. };
  110.  
  111. template <typename T>
  112. struct result_t
  113. {
  114. bool succeeded;
  115. std::vector<code_unit_t<T>> code_units;
  116.  
  117. result_t(bool succeeded, std::vector<code_unit_t<T>> code_units)
  118. : succeeded(succeeded)
  119. , code_units(code_units)
  120. {
  121. }
  122. };
  123.  
  124. template <typename T>
  125. std::ostream& operator<<(std::ostream& stream, const result_t<T>& result)
  126. {
  127. stream
  128. << std::dec
  129. << std::boolalpha
  130. << "\tsucceeded : " << result.succeeded << "\n"
  131. << "\tcode unit count : " << result.code_units.size() << "\n"
  132. << "\tcode units : " << result.code_units << "\n";
  133.  
  134. return stream;
  135. }
  136.  
  137. int main()
  138. {
  139. // 1. Go to http://r...content-available-to-author-only...b.io/apps/conversion/
  140. // 2. Paste おはよう into the topmost "Convert" text input field.
  141. // 3. Use the values from the "UTF-8/16 code units" text output fields
  142. const char* raw_utf8 { "\xE3\x81\x8A\xE3\x81\xAF\xE3\x82\x88\xE3\x81\x86" };
  143. const wchar_t* raw_utf16 { L"\x304A\x306F\x3088\x3046" };
  144. const raw_t raw { raw_utf8, raw_utf16 };
  145. const converted_t converted { raw };
  146. const code_units_t code_units { converted };
  147.  
  148. std::cout
  149. << "UTF-16 => UTF-8" << "\n"
  150. << result_t<uint8_t>(raw.utf8 == converted.utf8, code_units.utf8) << "\n"
  151. << "UTF-8 => UTF-16" << "\n"
  152. << result_t<uint16_t>(raw.utf16 == converted.utf16, code_units.utf16) << "\n";
  153.  
  154. return 0;
  155. }
Success #stdin #stdout 0s 16072KB
stdin
Standard input is empty
stdout
UTF-16 => UTF-8
	succeeded       : true
	code unit count : 12
	code units      : 0xE3 0x81 0x8A 0xE3 0x81 0xAF 0xE3 0x82 0x88 0xE3 0x81 0x86 

UTF-8 => UTF-16
	succeeded       : false
	code unit count : 4
	code units      : 0x4A30 0x6F30 0x8830 0x4630