Skip to content

Commit 1eaed2c

Browse files
committed
Increasing code coverage
1 parent f7fd125 commit 1eaed2c

File tree

1 file changed

+2
-29
lines changed

1 file changed

+2
-29
lines changed

mssql_python/pybind/ddbc_bindings.h

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#pragma once
88

99
// pybind11.h must be the first include
10-
#include <clocale>
11-
#include <cwchar>
1210
#include <memory>
1311
#include <pybind11/chrono.h>
1412
#include <pybind11/complex.h>
@@ -460,33 +458,8 @@ inline std::wstring Utf8ToWString(const std::string& str) {
460458
return {};
461459
return result;
462460
#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);
490463
#endif
491464
}
492465

0 commit comments

Comments
 (0)