From db91035b421e40cb332038ffd10d9e9e5124588c Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Wed, 11 Jun 2025 09:57:30 +0200 Subject: [PATCH] unicode: fix and cleanup Windows support Signed-off-by: Simon Rozman --- include/stdex/unicode.hpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/include/stdex/unicode.hpp b/include/stdex/unicode.hpp index 7b22f3dcf..6bb91033e 100644 --- a/include/stdex/unicode.hpp +++ b/include/stdex/unicode.hpp @@ -214,7 +214,7 @@ namespace stdex // Try to convert to stack buffer first. 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? - int cch = MultiByteToWideChar(static_cast(m_from_wincp), 0, reinterpret_cast(src), static_cast(count_src), szStackBuffer, _countof(szStackBuffer)); + int cch = MultiByteToWideChar(m_from_wincp, 0, reinterpret_cast(src), static_cast(count_src), szStackBuffer, _countof(szStackBuffer)); if (cch) { // Append from stack. dst.append(reinterpret_cast(szStackBuffer), count_src != SIZE_MAX ? wcsnlen(szStackBuffer, cch) : static_cast(cch) - 1); @@ -223,10 +223,10 @@ namespace stdex DWORD dwResult = GetLastError(); if (dwResult == ERROR_INSUFFICIENT_BUFFER) { // Query the required output size. Allocate buffer. Then convert again. - cch = MultiByteToWideChar(static_cast(m_from_wincp), 0, reinterpret_cast(src), static_cast(count_src), NULL, 0); + cch = MultiByteToWideChar(m_from_wincp, 0, reinterpret_cast(src), static_cast(count_src), NULL, 0); size_t offset = dst.size(); dst.resize(offset + static_cast(cch)); - cch = MultiByteToWideChar(static_cast(m_from_wincp), 0, reinterpret_cast(src), static_cast(count_src), &dst[offset], cch); + cch = MultiByteToWideChar(m_from_wincp, 0, reinterpret_cast(src), static_cast(count_src), &dst[offset], cch); dst.resize(offset + (count_src != SIZE_MAX ? wcsnlen(&dst[offset], cch) : static_cast(cch) - 1)); return; } @@ -236,12 +236,12 @@ namespace stdex #pragma warning(suppress: 4127) // Can't use precompiler #if on template arguments, using "if" makes MSVC warnings. if constexpr (sizeof(T_from) == sizeof(wchar_t) && sizeof(T_to) == sizeof(char)) { stdex_assert(count_src < INT_MAX || count_src == SIZE_MAX); - LPCCH lpDefaultChar = m_to_wincp == charset_id::utf8 || m_to_wincp == charset_id::utf7 ? NULL : &m_invalid; + LPCCH lpDefaultChar = m_to_wincp == CP_UTF8 || m_to_wincp == CP_UTF7 ? NULL : &m_invalid; // Try to convert to stack buffer first. CHAR szStackBuffer[1024 / sizeof(CHAR)]; #pragma warning(suppress: 6387) // Testing indicates src may be NULL when count_src is also 0. Is SAL of the lpWideCharStr parameter wrong? - int cch = WideCharToMultiByte(static_cast(m_to_wincp), dwFlagsWCMB, reinterpret_cast(src), static_cast(count_src), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, NULL); + int cch = WideCharToMultiByte(m_to_wincp, dwFlagsWCMB, reinterpret_cast(src), static_cast(count_src), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, NULL); if (cch) { // Copy from stack. Be careful not to include zero terminator. dst.append(reinterpret_cast(szStackBuffer), count_src != SIZE_MAX ? strnlen(szStackBuffer, cch) : static_cast(cch) - 1); @@ -250,10 +250,10 @@ namespace stdex DWORD dwResult = GetLastError(); if (dwResult == ERROR_INSUFFICIENT_BUFFER) { // Query the required output size. Allocate buffer. Then convert again. - cch = WideCharToMultiByte(static_cast(m_to_wincp), dwFlagsWCMB, reinterpret_cast(src), static_cast(count_src), NULL, 0, lpDefaultChar, NULL); + cch = WideCharToMultiByte(m_to_wincp, dwFlagsWCMB, reinterpret_cast(src), static_cast(count_src), NULL, 0, lpDefaultChar, NULL); size_t offset = dst.size(); dst.resize(offset + static_cast(cch)); - cch = WideCharToMultiByte(static_cast(m_to_wincp), dwFlagsWCMB, reinterpret_cast(src), static_cast(count_src), &dst[offset], cch, lpDefaultChar, NULL); + cch = WideCharToMultiByte(m_to_wincp, dwFlagsWCMB, reinterpret_cast(src), static_cast(count_src), &dst[offset], cch, lpDefaultChar, NULL); dst.resize(offset + (count_src != SIZE_MAX ? strnlen(&dst[offset], cch) : static_cast(cch) - 1)); return; } @@ -263,13 +263,13 @@ namespace stdex #pragma warning(suppress: 4127) // Can't use precompiler #if on template arguments, using "if" makes MSVC warnings. if constexpr (sizeof(T_from) == sizeof(char) && sizeof(T_to) == sizeof(char)) { stdex_assert(count_src < INT_MAX || count_src == SIZE_MAX); - LPCCH lpDefaultChar = m_to_wincp == charset_id::utf8 || m_to_wincp == charset_id::utf7 ? NULL : &m_invalid; + LPCCH lpDefaultChar = m_to_wincp == CP_UTF8 || m_to_wincp == CP_UTF7 ? NULL : &m_invalid; // Try to convert to stack buffer first. DWORD dwResult; 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? - int cch = MultiByteToWideChar(static_cast(m_from_wincp), 0, reinterpret_cast(src), static_cast(count_src), szStackBufferMBWC, _countof(szStackBufferMBWC)); + int cch = MultiByteToWideChar(m_from_wincp, 0, reinterpret_cast(src), static_cast(count_src), szStackBufferMBWC, _countof(szStackBufferMBWC)); if (cch) { // Append from stack. size_t count_inter = count_src != SIZE_MAX ? wcsnlen(szStackBufferMBWC, cch) : static_cast(cch) - 1; @@ -278,7 +278,7 @@ namespace stdex // Try to convert to stack buffer first. CHAR szStackBufferWCMB[512 / sizeof(CHAR)]; #pragma warning(suppress: 6387) // Testing indicates szStackBufferMBWC may be NULL when count_inter is also 0. Is SAL of the lpWideCharStr parameter wrong? - cch = WideCharToMultiByte(static_cast(m_to_wincp), dwFlagsWCMB, szStackBufferMBWC, static_cast(count_inter), szStackBufferWCMB, _countof(szStackBufferWCMB), lpDefaultChar, NULL); + cch = WideCharToMultiByte(m_to_wincp, dwFlagsWCMB, szStackBufferMBWC, static_cast(count_inter), szStackBufferWCMB, _countof(szStackBufferWCMB), lpDefaultChar, NULL); if (cch) { // Copy from stack. Be careful not to include zero terminator. dst.append(reinterpret_cast(szStackBufferWCMB), strnlen(szStackBufferWCMB, cch)); @@ -287,10 +287,10 @@ namespace stdex dwResult = GetLastError(); if (dwResult == ERROR_INSUFFICIENT_BUFFER) { // Query the required output size. Allocate buffer. Then convert again. - cch = WideCharToMultiByte(static_cast(m_to_wincp), dwFlagsWCMB, szStackBufferMBWC, static_cast(count_inter), NULL, 0, lpDefaultChar, NULL); + cch = WideCharToMultiByte(m_to_wincp, dwFlagsWCMB, szStackBufferMBWC, static_cast(count_inter), NULL, 0, lpDefaultChar, NULL); size_t offset = dst.size(); dst.resize(offset + cch); - cch = WideCharToMultiByte(static_cast(m_to_wincp), dwFlagsWCMB, szStackBufferMBWC, static_cast(count_inter), &dst[offset], cch, lpDefaultChar, NULL); + cch = WideCharToMultiByte(m_to_wincp, dwFlagsWCMB, szStackBufferMBWC, static_cast(count_inter), &dst[offset], cch, lpDefaultChar, NULL); dst.resize(offset + strnlen(&dst[offset], cch)); return; } @@ -299,16 +299,16 @@ namespace stdex dwResult = GetLastError(); if (dwResult == ERROR_INSUFFICIENT_BUFFER) { // Query the required output size. Allocate buffer. Then convert again. - cch = MultiByteToWideChar(static_cast(m_from_wincp), 0, reinterpret_cast(src), static_cast(count_src), NULL, 0); + cch = MultiByteToWideChar(m_from_wincp, 0, reinterpret_cast(src), static_cast(count_src), NULL, 0); std::unique_ptr szBufferMBWC(new WCHAR[cch]); - cch = MultiByteToWideChar(static_cast(m_from_wincp), 0, reinterpret_cast(src), static_cast(count_src), szBufferMBWC.get(), cch); + cch = MultiByteToWideChar(m_from_wincp, 0, reinterpret_cast(src), static_cast(count_src), szBufferMBWC.get(), cch); size_t count_inter = count_src != SIZE_MAX ? wcsnlen(szBufferMBWC.get(), cch) : static_cast(cch) - 1; // Query the required output size. Allocate buffer. Then convert again. - cch = WideCharToMultiByte(static_cast(m_to_wincp), dwFlagsWCMB, szBufferMBWC.get(), static_cast(count_inter), NULL, 0, lpDefaultChar, NULL); + cch = WideCharToMultiByte(m_to_wincp, dwFlagsWCMB, szBufferMBWC.get(), static_cast(count_inter), NULL, 0, lpDefaultChar, NULL); size_t offset = dst.size(); dst.resize(offset + cch); - cch = WideCharToMultiByte(static_cast(m_to_wincp), dwFlagsWCMB, szBufferMBWC.get(), static_cast(count_inter), &dst[offset], cch, lpDefaultChar, NULL); + cch = WideCharToMultiByte(m_to_wincp, dwFlagsWCMB, szBufferMBWC.get(), static_cast(count_inter), &dst[offset], cch, lpDefaultChar, NULL); dst.resize(offset + strnlen(&dst[offset], cch)); return; }