|
7 | 7 | #pragma once |
8 | 8 |
|
9 | 9 | // pybind11.h must be the first include |
| 10 | +#include <clocale> |
| 11 | +#include <cwchar> |
10 | 12 | #include <memory> |
11 | 13 | #include <pybind11/chrono.h> |
12 | 14 | #include <pybind11/complex.h> |
@@ -458,8 +460,28 @@ inline std::wstring Utf8ToWString(const std::string& str) { |
458 | 460 | return {}; |
459 | 461 | return result; |
460 | 462 | #else |
461 | | - std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; |
462 | | - return converter.from_bytes(str); |
| 463 | + // Use mbstowcs as a replacement for deprecated wstring_convert |
| 464 | + // Set locale to UTF-8 for proper conversion |
| 465 | + const char* old_locale = setlocale(LC_CTYPE, nullptr); |
| 466 | + setlocale(LC_CTYPE, "en_US.UTF-8"); |
| 467 | + |
| 468 | + size_t size_needed = mbstowcs(nullptr, str.c_str(), 0); |
| 469 | + if (size_needed == static_cast<size_t>(-1)) { |
| 470 | + LOG_ERROR("mbstowcs failed for UTF8 to wide string conversion"); |
| 471 | + setlocale(LC_CTYPE, old_locale); |
| 472 | + return {}; |
| 473 | + } |
| 474 | + |
| 475 | + std::wstring result(size_needed, 0); |
| 476 | + size_t converted = mbstowcs(&result[0], str.c_str(), size_needed); |
| 477 | + if (converted == static_cast<size_t>(-1)) { |
| 478 | + LOG_ERROR("mbstowcs failed for UTF8 to wide string conversion"); |
| 479 | + setlocale(LC_CTYPE, old_locale); |
| 480 | + return {}; |
| 481 | + } |
| 482 | + |
| 483 | + setlocale(LC_CTYPE, old_locale); |
| 484 | + return result; |
463 | 485 | #endif |
464 | 486 | } |
465 | 487 |
|
|
0 commit comments