fork download
  1. std::wstring widen(std::string const& src)
  2. {
  3. typedef std::codecvt<wchar_t, char, std::mbstate_t> convert;
  4.  
  5. const convert& cvt = std::use_facet<convert>(std::locale(""));
  6. convert::result res;
  7. std::mbstate_t state;
  8.  
  9. const char *src_next = 0;
  10. wchar_t *wcs = new wchar_t[src.length() + 1];
  11. wchar_t *wcs_next = 0;
  12. std::wstring ret;
  13.  
  14. res = cvt.in(state, src.data(), src.data() + src.size(), src_next,
  15. wcs, wcs + sizeof(wchar_t) * (src.length() + 1), wcs_next
  16. );
  17.  
  18. // big endian
  19. if (res == convert::ok) {
  20. std::wstring ret(wcs, wcs_next);
  21.  
  22. for (auto it = ret.begin(), end = ret.end(); it != end; ++it) {
  23. std::reverse(reinterpret_cast<unsigned wchar_t>(*it), sizeof(unsigned wchar_t));
  24. }
  25. return ret;
  26. }
  27.  
  28.  
  29. // fail
  30. return std::wstring();
  31. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty