Skip to content

Commit

Permalink
Fix unexpected result using QString::toStdWString() (fix goldendict#107
Browse files Browse the repository at this point in the history
… again, QTBUG-25536)

* QString::toStdWString() and QString::toUCS4() will have wrong size when
  QString instances contain non-BMP characters.

Close goldendict#320
  • Loading branch information
timonwong committed May 27, 2013
1 parent 427c0df commit 07113d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
2 changes: 1 addition & 1 deletion mdx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ using namespace Mdict;
enum
{
kSignature = 0x4349444d, // MDIC
kCurrentFormatVersion = 8 + BtreeIndexing::FormatVersion
kCurrentFormatVersion = 8 + BtreeIndexing::FormatVersion + Folding::Version
};

DEF_EX( exCorruptDictionary, "dictionary file was tampered or corrupted", std::exception )
Expand Down
28 changes: 13 additions & 15 deletions wstring_qt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,27 @@ namespace gd
return QString::fromUcs4( in.c_str() );
}

wstring toWString( QString const & in )
{
QVector< unsigned int > v = in.toUcs4();

// Fix for CJK Extension B characters
int n = v.size();
while( n > 0 && v[ n - 1 ] == 0 ) n--;
if( n != v.size() )
v.resize( n );

return wstring( v.constData(), v.size() );
}

#else

QString toQString( wstring const & in )
{
return QString::fromStdWString( in );
}

#endif

wstring toWString( QString const & in )
{
return in.toStdWString();
QVector< unsigned int > v = in.toUcs4();

// Fix for QString instance which contains non-BMP characters
// Qt will created unexpected null characters may confuse btree indexer.
// Related: https://bugreports.qt-project.org/browse/QTBUG-25536
int n = v.size();
while ( n > 0 && v[ n - 1 ] == 0 ) n--;
if ( n != v.size() )
v.resize( n );

return wstring( ( const wchar * ) v.constData(), v.size() );
}
#endif
}

0 comments on commit 07113d0

Please sign in to comment.