unicode: Remove deprecated MB_PRECOMPOSED

Online documentation suggests it should be used as default (for the most
of code pages), SDK source code says it's deprecated. Let's remove it
and see how things work.

Reference: https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar
Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2025-03-28 14:15:41 +01:00
parent 832daf89d6
commit 50578dabaa

View File

@ -197,10 +197,9 @@ namespace stdex
stdex_assert(count_src < INT_MAX || count_src == SIZE_MAX); stdex_assert(count_src < INT_MAX || count_src == SIZE_MAX);
// Try to convert to stack buffer first. // Try to convert to stack buffer first.
DWORD dwFlagsMBWC = static_cast<UINT>(m_from_wincp) < CP_UTF7 ? MB_PRECOMPOSED : 0;
WCHAR szStackBuffer[1024 / sizeof(WCHAR)]; WCHAR szStackBuffer[1024 / sizeof(WCHAR)];
#pragma warning(suppress: 6387) // Testing indicates src may be NULL when count_src is also 0. Is SAL of the lpMultiByteStr parameter wrong? #pragma warning(suppress: 6387) // Testing indicates src may be NULL when count_src is also 0. Is SAL of the lpMultiByteStr parameter wrong?
int cch = MultiByteToWideChar(static_cast<UINT>(m_from_wincp), dwFlagsMBWC, reinterpret_cast<LPCCH>(src), static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer)); int cch = MultiByteToWideChar(static_cast<UINT>(m_from_wincp), 0, reinterpret_cast<LPCCH>(src), static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer));
if (cch) { if (cch) {
// Append from stack. // Append from stack.
dst.append(reinterpret_cast<const T_to*>(szStackBuffer), count_src != SIZE_MAX ? wcsnlen(szStackBuffer, cch) : static_cast<size_t>(cch) - 1); dst.append(reinterpret_cast<const T_to*>(szStackBuffer), count_src != SIZE_MAX ? wcsnlen(szStackBuffer, cch) : static_cast<size_t>(cch) - 1);
@ -209,10 +208,10 @@ namespace stdex
DWORD dwResult = GetLastError(); DWORD dwResult = GetLastError();
if (dwResult == ERROR_INSUFFICIENT_BUFFER) { if (dwResult == ERROR_INSUFFICIENT_BUFFER) {
// Query the required output size. Allocate buffer. Then convert again. // Query the required output size. Allocate buffer. Then convert again.
cch = MultiByteToWideChar(static_cast<UINT>(m_from_wincp), dwFlagsMBWC, reinterpret_cast<LPCCH>(src), static_cast<int>(count_src), NULL, 0); cch = MultiByteToWideChar(static_cast<UINT>(m_from_wincp), 0, reinterpret_cast<LPCCH>(src), static_cast<int>(count_src), NULL, 0);
size_t offset = dst.size(); size_t offset = dst.size();
dst.resize(offset + static_cast<size_t>(cch)); dst.resize(offset + static_cast<size_t>(cch));
cch = MultiByteToWideChar(static_cast<UINT>(m_from_wincp), dwFlagsMBWC, reinterpret_cast<LPCCH>(src), static_cast<int>(count_src), &dst[offset], cch); cch = MultiByteToWideChar(static_cast<UINT>(m_from_wincp), 0, reinterpret_cast<LPCCH>(src), static_cast<int>(count_src), &dst[offset], cch);
dst.resize(offset + (count_src != SIZE_MAX ? wcsnlen(&dst[offset], cch) : static_cast<size_t>(cch) - 1)); dst.resize(offset + (count_src != SIZE_MAX ? wcsnlen(&dst[offset], cch) : static_cast<size_t>(cch) - 1));
return; return;
} }
@ -250,10 +249,10 @@ namespace stdex
stdex_assert(count_src < INT_MAX || count_src == SIZE_MAX); stdex_assert(count_src < INT_MAX || count_src == SIZE_MAX);
// Try to convert to stack buffer first. // Try to convert to stack buffer first.
DWORD dwFlagsMBWC = static_cast<UINT>(m_from_wincp) < CP_UTF7 ? MB_PRECOMPOSED : 0, dwResult; DWORD dwResult;
WCHAR szStackBufferMBWC[512 / sizeof(WCHAR)]; WCHAR szStackBufferMBWC[512 / sizeof(WCHAR)];
#pragma warning(suppress: 6387) // Testing indicates src may be NULL when count_src is also 0. Is SAL of the lpMultiByteStr parameter wrong? #pragma warning(suppress: 6387) // Testing indicates src may be NULL when count_src is also 0. Is SAL of the lpMultiByteStr parameter wrong?
int cch = MultiByteToWideChar(static_cast<UINT>(m_from_wincp), dwFlagsMBWC, reinterpret_cast<LPCCH>(src), static_cast<int>(count_src), szStackBufferMBWC, _countof(szStackBufferMBWC)); int cch = MultiByteToWideChar(static_cast<UINT>(m_from_wincp), 0, reinterpret_cast<LPCCH>(src), static_cast<int>(count_src), szStackBufferMBWC, _countof(szStackBufferMBWC));
if (cch) { if (cch) {
// Append from stack. // Append from stack.
size_t count_inter = count_src != SIZE_MAX ? wcsnlen(szStackBufferMBWC, cch) : static_cast<size_t>(cch) - 1; size_t count_inter = count_src != SIZE_MAX ? wcsnlen(szStackBufferMBWC, cch) : static_cast<size_t>(cch) - 1;
@ -283,9 +282,9 @@ namespace stdex
dwResult = GetLastError(); dwResult = GetLastError();
if (dwResult == ERROR_INSUFFICIENT_BUFFER) { if (dwResult == ERROR_INSUFFICIENT_BUFFER) {
// Query the required output size. Allocate buffer. Then convert again. // Query the required output size. Allocate buffer. Then convert again.
cch = MultiByteToWideChar(static_cast<UINT>(m_from_wincp), dwFlagsMBWC, reinterpret_cast<LPCCH>(src), static_cast<int>(count_src), NULL, 0); cch = MultiByteToWideChar(static_cast<UINT>(m_from_wincp), 0, reinterpret_cast<LPCCH>(src), static_cast<int>(count_src), NULL, 0);
std::unique_ptr<WCHAR[]> szBufferMBWC(new WCHAR[cch]); std::unique_ptr<WCHAR[]> szBufferMBWC(new WCHAR[cch]);
cch = MultiByteToWideChar(static_cast<UINT>(m_from_wincp), dwFlagsMBWC, reinterpret_cast<LPCCH>(src), static_cast<int>(count_src), szBufferMBWC.get(), cch); cch = MultiByteToWideChar(static_cast<UINT>(m_from_wincp), 0, reinterpret_cast<LPCCH>(src), static_cast<int>(count_src), szBufferMBWC.get(), cch);
size_t count_inter = count_src != SIZE_MAX ? wcsnlen(szBufferMBWC.get(), cch) : static_cast<size_t>(cch) - 1; size_t count_inter = count_src != SIZE_MAX ? wcsnlen(szBufferMBWC.get(), cch) : static_cast<size_t>(cch) - 1;
// Query the required output size. Allocate buffer. Then convert again. // Query the required output size. Allocate buffer. Then convert again.