Compare commits

...

4 commits

Author SHA1 Message Date
shenleban tongying c04336fbfa
Merge daf2e81ae3 into fbec70e41a 2024-11-13 00:32:00 -05:00
shenleban tongying fbec70e41a opt: simplify Folding::apply
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run
2024-11-12 23:47:20 -05:00
shenleban tongying daf2e81ae3
Merge branch 'staged' into fix/dict-fix-for-real 2024-11-12 17:27:31 -05:00
shenleban tongying f59b04565a fix: dict.org crashes by incomplete response and str out of bound access 2024-11-12 16:32:32 -05:00
2 changed files with 26 additions and 36 deletions

View file

@ -20,41 +20,23 @@ bool isCombiningMark( wchar ch )
wstring apply( wstring const & in, bool preserveWildcards )
{
//remove space and accent;
auto withPunc = QString::fromStdU32String( in )
// remove diacritics (normalization), white space, punt,
auto temp = QString::fromStdU32String( in )
.normalized( QString::NormalizationForm_KD )
.remove( RX::markSpace )
.removeIf( [ preserveWildcards ]( const QChar & ch ) -> bool {
return ch.isPunct()
&& !( preserveWildcards && ( ch == '\\' || ch == '?' || ch == '*' || ch == '[' || ch == ']' ) );
} )
.toStdU32String();
//First, strip diacritics and apply ws/punctuation removal
wstring withoutDiacritics;
withoutDiacritics.reserve( withPunc.size() );
for ( auto const & ch : withPunc ) {
if ( !isPunct( ch )
|| ( preserveWildcards && ( ch == '\\' || ch == '?' || ch == '*' || ch == '[' || ch == ']' ) ) ) {
withoutDiacritics.push_back( ch );
}
}
// Now, fold the case
wstring caseFolded;
caseFolded.reserve( withoutDiacritics.size() * foldCaseMaxOut );
wchar const * nextChar = withoutDiacritics.data();
// case folding
std::u32string caseFolded;
caseFolded.reserve( temp.size() );
wchar buf[ foldCaseMaxOut ];
for ( size_t left = withoutDiacritics.size(); left--; ) {
caseFolded.append( buf, foldCase( *nextChar++, buf ) );
for ( const char32_t ch : temp ) {
auto n = foldCase( ch, buf );
caseFolded.append( buf, n );
}
return caseFolded;
}

View file

@ -538,14 +538,22 @@ void DictServerWordSearchRequest::readMatchData( QByteArray & reply )
if ( word.endsWith( '\"' ) ) {
word.chop( 1 );
}
if ( word[ 0 ] == '\"' ) {
if ( word.startsWith( '\"' ) ) {
word = word.mid( 1 );
}
if ( !word.isEmpty() ) {
this->addMatchedWord( word );
}
}
constexpr int halfSecond = 500;
if ( this->dictImpl->socket.waitForReadyRead( halfSecond ) ) {
reply = this->dictImpl->socket.readLine();
}
else {
return;
}
} while ( true );
}