17 #ifndef WIN32_LEAN_AND_MEAN
18 #define WIN32_LEAN_AND_MEAN
41 : std::runtime_error(message)
42 , m_conversion(conversion)
43 , m_error_code(error_code)
50 : std::runtime_error(message)
51 , m_conversion(conversion)
52 , m_error_code(error_code)
75 if (utf8.empty())
return std::wstring();
78 const DWORD conversionFlags = MB_ERR_INVALID_CHARS;
83 const int utf16Length = ::MultiByteToWideChar(
92 if (utf16Length == 0) {
94 DWORD error = ::GetLastError();
97 (error == ERROR_NO_UNICODE_TRANSLATION)
98 ?
"Invalid UTF-8 sequence found in input string."
99 :
"Can't get length of UTF-16 string (MultiByteToWideChar "
108 utf16.resize(utf16Length);
113 if (!::MultiByteToWideChar(
125 DWORD error = ::GetLastError();
127 "Can't convert string from UTF-8 to UTF-16 (MultiByteToWideChar "
143 if (utf8 ==
nullptr || *utf8 ==
'\0')
return std::wstring();
146 const int utf8Length =
static_cast<int>(strlen(utf8));
149 const DWORD conversionFlags = MB_ERR_INVALID_CHARS;
154 const int utf16Length = ::MultiByteToWideChar(
162 if (utf16Length == 0) {
164 DWORD error = ::GetLastError();
166 (error == ERROR_NO_UNICODE_TRANSLATION)
167 ?
"Invalid UTF-8 sequence found in input string."
168 :
"Can't get length of UTF-16 string (MultiByteToWideChar "
177 utf16.resize(utf16Length);
182 if (!::MultiByteToWideChar(
194 DWORD error = ::GetLastError();
196 "Can't convert string from UTF-8 to UTF-16 (MultiByteToWideChar "
212 if (utf16.empty())
return std::string();
217 const int utf8Length = ::WideCharToMultiByte(
221 static_cast<int>(utf16.length()),
226 if (utf8Length == 0) {
228 DWORD error = ::GetLastError();
230 "Can't get length of UTF-8 string (WideCharToMultiByte failed).",
238 utf8.resize(utf8Length);
243 if (!::WideCharToMultiByte(
247 static_cast<int>(utf16.length()),
250 static_cast<int>(utf8.length()),
255 DWORD error = ::GetLastError();
257 "Can't convert string from UTF-16 to UTF-8 (WideCharToMultiByte "
273 if (utf16 ==
nullptr || *utf16 == L
'\0')
return std::string();
276 const int utf16Length =
static_cast<int>(wcslen(utf16));
281 const int utf8Length = ::WideCharToMultiByte(
290 if (utf8Length == 0) {
292 DWORD error = ::GetLastError();
294 "Can't get length of UTF-8 string (WideCharToMultiByte failed).",
302 utf8.resize(utf8Length);
307 if (!::WideCharToMultiByte(
311 static_cast<int>(utf16Length),
314 static_cast<int>(utf8.length()),
319 DWORD error = ::GetLastError();
321 "Can't convert string from UTF-16 to UTF-8 (WideCharToMultiByte "
unsigned long error_code_type
conversion_type conversion() const
utf8_conversion_error(const char *message, conversion_type conversion, error_code_type error_code)
EXPORT std::string UTF8FromUTF16(const std::wstring &utf16)
EXPORT std::wstring UTF16FromUTF8(const std::string &utf8)
error_code_type error_code() const