Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
string.cc
Go to the documentation of this file.
1 
18 #include "ion/port/string.h"
19 
20 #if defined(ION_PLATFORM_WINDOWS)
21 #include <windows.h>
22 #endif
23 
24 namespace ion {
25 namespace port {
26 
27 #if defined(ION_PLATFORM_WINDOWS)
28 std::wstring Utf8ToWide(const std::string& utf8) {
31  std::wstring wide;
32  if (!utf8.empty()) {
33  wide.resize(utf8.size()); // Worst case: one wchar_t for each byte
34  const int size =
35  ::MultiByteToWideChar(CP_UTF8, 0,
36  &utf8[0], static_cast<int>(utf8.size()),
37  &wide[0], static_cast<int>(wide.size()));
38  wide.resize(size);
39  }
40  return wide;
41 }
42 
43 std::string WideToUtf8(const std::wstring& wide) {
44  std::string utf8;
45  if (!wide.empty()) {
46  utf8.resize(4 * wide.size()); // Worst case: 4 bytes for each wchar_t
47  const int size =
48  ::WideCharToMultiByte(CP_UTF8, 0,
49  &wide[0], static_cast<int>(wide.size()),
50  &utf8[0], static_cast<int>(utf8.size()),
51  NULL, NULL);
52  utf8.resize(size);
53  }
54  return utf8;
55 }
56 #endif
57 
58 } // namespace port
59 } // namespace ion
60