CommonFunctions.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #ifndef COMMON_FUNCTIONS_H
  2. #define COMMON_FUNCTIONS_H
  3. #include <string>
  4. #include <uchar.h>
  5. #include <codecvt>
  6. #include <locale>
  7. inline long long mstrlen(const char16_t *s) {
  8. return sizeof(s) / sizeof(*s);
  9. }
  10. inline std::string u16stringToBytes(const std::u16string& str)
  11. {
  12. #if defined(_MSC_VER)
  13. auto p = reinterpret_cast<unsigned short const*>(str.data());
  14. auto out = std::wstring_convert<std::codecvt_utf16<unsigned short>, unsigned short>().to_bytes(p, p + str.size());
  15. return out;
  16. #else
  17. std::wstring_convert<std::codecvt_utf16<char16_t>, char16_t> conv;
  18. return conv.to_bytes(str);
  19. #endif
  20. }
  21. inline std::u16string bytesToU16string(const std::string& str)
  22. {
  23. #if defined(_MSC_VER)
  24. std::u16string out;
  25. auto s = std::wstring_convert<std::codecvt_utf16<unsigned short>, unsigned short>().from_bytes(str);
  26. auto p = reinterpret_cast<wchar_t const*>(s.data());
  27. out.assign(p, p + s.size());
  28. return out;
  29. #else
  30. std::wstring_convert<std::codecvt_utf16<char16_t>, char16_t> conv;
  31. return conv.from_bytes(str);
  32. #endif
  33. }
  34. #endif