From 07113d0bf6b059ef1c2ea68e9b415be5e5ae6f26 Mon Sep 17 00:00:00 2001 From: Timon Wong Date: Mon, 27 May 2013 14:56:16 +0800 Subject: [PATCH] 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 --- mdx.cc | 2 +- wstring_qt.cc | 28 +++++++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/mdx.cc b/mdx.cc index a861c1c6..b4815e2f 100644 --- a/mdx.cc +++ b/mdx.cc @@ -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 ) diff --git a/wstring_qt.cc b/wstring_qt.cc index 110989d2..75daf08d 100644 --- a/wstring_qt.cc +++ b/wstring_qt.cc @@ -10,19 +10,6 @@ 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 ) @@ -30,9 +17,20 @@ namespace gd 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 }