CompoundFileReader: fix UTF16~wstring conversion on 16b wchar compilers

This commit is contained in:
Alex Shvartzkop 2023-07-23 14:55:15 +05:00
parent 430da67222
commit c39dfb9f05
1 changed files with 26 additions and 18 deletions

View File

@ -102,34 +102,42 @@ template <typename T>
std::wstring UTF16ToWstring(const T* u16, size_t len = 0) std::wstring UTF16ToWstring(const T* u16, size_t len = 0)
{ {
std::wstring ret; std::wstring ret;
#ifdef _MSC_VER if( sizeof( wchar_t ) == 2 )
while (*u16) ret += *u16++;
#else
uint32_t cp;
size_t pos = 0;
while (GetNextCodePointFromUTF16(u16, len, &pos, &cp))
{ {
ret += cp; while( *u16 )
ret += *u16++;
}
else
{
uint32_t cp;
size_t pos = 0;
while( GetNextCodePointFromUTF16( u16, len, &pos, &cp ) )
{
ret += cp;
}
} }
#endif
return ret; return ret;
} }
template <typename T> template <typename T>
std::string WstringToUTF8(const T* wstr) std::string WstringToUTF8(const T* wstr)
{ {
#ifdef _MSC_VER if( sizeof( wchar_t ) == 2 )
return UTF16ToUTF8(wstr);
#else
std::string u8;
uint32_t cp;
while ((cp = *wstr++) != 0)
{ {
uint32_t c[4]; return UTF16ToUTF8( wstr );
int count = CodePointToUTF8(cp, c, c+1, c+2, c+3); }
for (int i = 0; i < count; i++) else
{
std::string u8;
uint32_t cp;
while( ( cp = *wstr++ ) != 0 )
{ {
u8 += static_cast<char>(c[i]); 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<char>( c[i] );
}
} }
} }
return u8; return u8;