|
7 | 7 | #pragma once |
8 | 8 |
|
9 | 9 | // pybind11.h must be the first include |
10 | | -#include <clocale> |
11 | | -#include <cwchar> |
12 | 10 | #include <memory> |
13 | 11 | #include <pybind11/chrono.h> |
14 | 12 | #include <pybind11/complex.h> |
@@ -460,33 +458,8 @@ inline std::wstring Utf8ToWString(const std::string& str) { |
460 | 458 | return {}; |
461 | 459 | return result; |
462 | 460 | #else |
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 | | - // Get the required buffer size (excluding null terminator) |
469 | | - size_t size_needed = mbstowcs(nullptr, str.c_str(), 0); |
470 | | - if (size_needed == static_cast<size_t>(-1)) { |
471 | | - LOG_ERROR("mbstowcs failed for UTF8 to wide string conversion"); |
472 | | - setlocale(LC_CTYPE, old_locale); |
473 | | - return {}; |
474 | | - } |
475 | | - |
476 | | - // Allocate buffer with space for null terminator |
477 | | - std::wstring result(size_needed + 1, 0); |
478 | | - // Convert with proper buffer size to prevent overflow |
479 | | - size_t converted = mbstowcs(&result[0], str.c_str(), result.size()); |
480 | | - if (converted == static_cast<size_t>(-1)) { |
481 | | - LOG_ERROR("mbstowcs failed for UTF8 to wide string conversion"); |
482 | | - setlocale(LC_CTYPE, old_locale); |
483 | | - return {}; |
484 | | - } |
485 | | - |
486 | | - // Resize to actual content length (excluding null terminator) |
487 | | - result.resize(converted); |
488 | | - setlocale(LC_CTYPE, old_locale); |
489 | | - return result; |
| 461 | + std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; |
| 462 | + return converter.from_bytes(str); |
490 | 463 | #endif |
491 | 464 | } |
492 | 465 |
|
|
0 commit comments