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

(cherry picked from commit c39dfb9f05)

CompoundFileReader: fix build error

(cherry picked from commit d40e34aebe)

UTF16ToWstring: stop at NUL input terminator.

(cherry picked from commit b784b44ed0)
This commit is contained in:
Alex Shvartzkop 2023-07-23 14:55:15 +05:00
parent 2eb6ca75a8
commit 582c9b7f08
1 changed files with 32 additions and 22 deletions

View File

@ -102,36 +102,46 @@ template <typename T>
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 <typename T>
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<char>(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<char>( c[i] );
}
}
return u8;
}
return u8;
#endif
}