You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

m_ConverterUtf8.h 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #pragma once
  2. #include <vector>
  3. namespace Common {
  4. namespace Converter {
  5. #if defined TARGET_OS_Windows
  6. // Returns the wstring (ie the utf16 formatted string) version of an utf8 string
  7. static std::wstring Utf8ToUtf16(const std::string& utf8)
  8. {
  9. std::vector<unsigned long> unicode;
  10. size_t i = 0;
  11. while (i < utf8.size())
  12. {
  13. unsigned long uni;
  14. size_t todo;
  15. unsigned char ch = utf8[i++];
  16. if (ch <= 0x7F)
  17. {
  18. uni = ch;
  19. todo = 0;
  20. }
  21. else if (ch <= 0xBF) { throw std::logic_error("not a UTF-8 string"); }
  22. else if (ch <= 0xDF)
  23. {
  24. uni = ch & 0x1F;
  25. todo = 1;
  26. }
  27. else if (ch <= 0xEF)
  28. {
  29. uni = ch & 0x0F;
  30. todo = 2;
  31. }
  32. else if (ch <= 0xF7)
  33. {
  34. uni = ch & 0x07;
  35. todo = 3;
  36. }
  37. else { throw std::logic_error("not a UTF-8 string"); }
  38. for (size_t j = 0; j < todo; ++j)
  39. {
  40. if (i == utf8.size()) { throw std::logic_error("not a UTF-8 string"); }
  41. ch = utf8[i++];
  42. if (ch < 0x80 || ch > 0xBF) { throw std::logic_error("not a UTF-8 string"); }
  43. uni <<= 6;
  44. uni += ch & 0x3F;
  45. }
  46. if (uni >= 0xD800 && uni <= 0xDFFF) { throw std::logic_error("not a UTF-8 string"); }
  47. if (uni > 0x10FFFF) { throw std::logic_error("not a UTF-8 string"); }
  48. unicode.push_back(uni);
  49. }
  50. std::wstring utf16;
  51. for (i = 0; i < unicode.size(); ++i)
  52. {
  53. unsigned long uni = unicode[i];
  54. if (uni <= 0xFFFF) { utf16 += wchar_t(uni); }
  55. else
  56. {
  57. uni -= 0x10000;
  58. utf16 += wchar_t((uni >> 10) + 0xD800);
  59. utf16 += wchar_t((uni & 0x3FF) + 0xDC00);
  60. }
  61. }
  62. return utf16;
  63. }
  64. #endif // TARGET_OS_Windows
  65. } // namespace Converter
  66. } // namespace Common