Fix unexpected result using QString::toStdWString() (fix #107 again, QTBUG-25536)

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

Close #320
This commit is contained in:
Timon Wong 2013-05-27 14:56:16 +08:00
parent 427c0df90e
commit 07113d0bf6
2 changed files with 14 additions and 16 deletions

2
mdx.cc
View file

@ -56,7 +56,7 @@ using namespace Mdict;
enum enum
{ {
kSignature = 0x4349444d, // MDIC kSignature = 0x4349444d, // MDIC
kCurrentFormatVersion = 8 + BtreeIndexing::FormatVersion kCurrentFormatVersion = 8 + BtreeIndexing::FormatVersion + Folding::Version
}; };
DEF_EX( exCorruptDictionary, "dictionary file was tampered or corrupted", std::exception ) DEF_EX( exCorruptDictionary, "dictionary file was tampered or corrupted", std::exception )

View file

@ -10,19 +10,6 @@ namespace gd
return QString::fromUcs4( in.c_str() ); 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 #else
QString toQString( wstring const & in ) QString toQString( wstring const & in )
@ -30,9 +17,20 @@ namespace gd
return QString::fromStdWString( in ); return QString::fromStdWString( in );
} }
#endif
wstring toWString( QString const & in ) 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
} }