|
10 | 10 | #include <sstream> |
11 | 11 | #include <iomanip> |
12 | 12 | #include <mutex> |
| 13 | +#include <utf8.h> |
13 | 14 | #include "Error.h" |
14 | 15 |
|
15 | 16 | static STATIC InitFunctionBase* g_initFunctions; |
@@ -349,12 +350,34 @@ bool UrlDecode(const std::string& in, std::string& out) |
349 | 350 |
|
350 | 351 | std::string ToNarrow(const std::wstring& wide) |
351 | 352 | { |
352 | | - static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> g_converter; |
353 | | - return g_converter.to_bytes(wide); |
| 353 | + // TODO: replace with something faster if needed |
| 354 | + std::vector<char> outVec; |
| 355 | + outVec.reserve(wide.size()); |
| 356 | + |
| 357 | +#ifdef _WIN32 |
| 358 | + utf8::utf16to8(wide.begin(), wide.end(), std::back_inserter(outVec)); |
| 359 | +#else |
| 360 | + utf8::utf32to8(wide.begin(), wide.end(), std::back_inserter(outVec)); |
| 361 | +#endif |
| 362 | + |
| 363 | + return std::string(outVec.begin(), outVec.end()); |
354 | 364 | } |
355 | 365 |
|
356 | 366 | std::wstring ToWide(const std::string& narrow) |
357 | 367 | { |
358 | | - static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> g_converter; |
359 | | - return g_converter.from_bytes(narrow); |
| 368 | + std::vector<uint8_t> cleanVec; |
| 369 | + cleanVec.reserve(narrow.size()); |
| 370 | + |
| 371 | + utf8::replace_invalid(narrow.begin(), narrow.end(), std::back_inserter(cleanVec)); |
| 372 | + |
| 373 | + std::vector<wchar_t> outVec; |
| 374 | + outVec.reserve(cleanVec.size()); |
| 375 | + |
| 376 | +#ifdef _WIN32 |
| 377 | + utf8::utf8to16(cleanVec.begin(), cleanVec.end(), std::back_inserter(outVec)); |
| 378 | +#else |
| 379 | + utf8::utf8to32(cleanVec.begin(), cleanVec.end(), std::back_inserter(outVec)); |
| 380 | +#endif |
| 381 | + |
| 382 | + return std::wstring(outVec.begin(), outVec.end()); |
360 | 383 | } |
0 commit comments