From 582c9b7f0800117e5f5a7033f173e685b0e9c2d1 Mon Sep 17 00:00:00 2001 From: Alex Shvartzkop Date: Sun, 23 Jul 2023 14:55:15 +0500 Subject: [PATCH] CompoundFileReader: fix UTF16~wstring conversion on 16b wchar compilers (cherry picked from commit c39dfb9f05a5a8b8cc63878cd7d896cfe4eda43b) CompoundFileReader: fix build error (cherry picked from commit d40e34aebe28fad69e0607d282c1eb794f3a7ea9) UTF16ToWstring: stop at NUL input terminator. (cherry picked from commit b784b44ed0034eb63598eaaef78a7d2c984745db) --- thirdparty/compoundfilereader/utf.h | 54 +++++++++++++++++------------ 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/thirdparty/compoundfilereader/utf.h b/thirdparty/compoundfilereader/utf.h index 5b253a80f7..dc92f1a693 100644 --- a/thirdparty/compoundfilereader/utf.h +++ b/thirdparty/compoundfilereader/utf.h @@ -102,36 +102,46 @@ template std::wstring UTF16ToWstring(const T* u16, size_t len = 0) { std::wstring ret; -#ifdef _MSC_VER - while (*u16) ret += *u16++; -#else - uint32_t cp; - size_t pos = 0; - while (GetNextCodePointFromUTF16(u16, len, &pos, &cp)) + if( sizeof( wchar_t ) == 2 ) { - ret += cp; + while( *u16 ) + ret += *u16++; + } + else + { + uint32_t cp; + size_t pos = 0; + while( GetNextCodePointFromUTF16( u16, len, &pos, &cp ) ) + { + if( !cp ) + break; + + ret += cp; + } } -#endif return ret; } template std::string WstringToUTF8(const T* wstr) { -#ifdef _MSC_VER - return UTF16ToUTF8(wstr); -#else - std::string u8; - uint32_t cp; - while ((cp = *wstr++) != 0) + if( sizeof( wchar_t ) == 2 ) { - uint32_t c[4]; - int count = CodePointToUTF8(cp, c, c+1, c+2, c+3); - for (int i = 0; i < count; i++) - { - u8 += static_cast(c[i]); - } + return UTF16ToUTF8( wstr ); + } + else + { + std::string u8; + uint32_t cp; + while( ( cp = *wstr++ ) != 0 ) + { + uint32_t c[4]; + int count = CodePointToUTF8( cp, c, c + 1, c + 2, c + 3 ); + for( int i = 0; i < count; i++ ) + { + u8 += static_cast( c[i] ); + } + } + return u8; } - return u8; -#endif }