mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-23 20:14:05 +00:00
opt: simplify Folding::apply
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run
This commit is contained in:
parent
20fcea33e1
commit
fbec70e41a
|
@ -20,41 +20,23 @@ bool isCombiningMark( wchar ch )
|
||||||
|
|
||||||
wstring apply( wstring const & in, bool preserveWildcards )
|
wstring apply( wstring const & in, bool preserveWildcards )
|
||||||
{
|
{
|
||||||
//remove space and accent;
|
// remove diacritics (normalization), white space, punt,
|
||||||
auto withPunc = QString::fromStdU32String( in )
|
auto temp = QString::fromStdU32String( in )
|
||||||
.normalized( QString::NormalizationForm_KD )
|
.normalized( QString::NormalizationForm_KD )
|
||||||
.remove( RX::markSpace )
|
.remove( RX::markSpace )
|
||||||
|
.removeIf( [ preserveWildcards ]( const QChar & ch ) -> bool {
|
||||||
|
return ch.isPunct()
|
||||||
|
&& !( preserveWildcards && ( ch == '\\' || ch == '?' || ch == '*' || ch == '[' || ch == ']' ) );
|
||||||
|
} )
|
||||||
.toStdU32String();
|
.toStdU32String();
|
||||||
|
// case folding
|
||||||
//First, strip diacritics and apply ws/punctuation removal
|
std::u32string caseFolded;
|
||||||
wstring withoutDiacritics;
|
caseFolded.reserve( temp.size() );
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
wchar buf[ foldCaseMaxOut ];
|
wchar buf[ foldCaseMaxOut ];
|
||||||
|
for ( const char32_t ch : temp ) {
|
||||||
for ( size_t left = withoutDiacritics.size(); left--; ) {
|
auto n = foldCase( ch, buf );
|
||||||
caseFolded.append( buf, foldCase( *nextChar++, buf ) );
|
caseFolded.append( buf, n );
|
||||||
}
|
}
|
||||||
|
|
||||||
return caseFolded;
|
return caseFolded;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue