A little refactoring of wildcards in the input line handling

This commit is contained in:
Abs62 2014-04-26 15:46:26 +04:00
parent e67bab15aa
commit a4ea28c121
6 changed files with 21 additions and 36 deletions

View file

@ -77,15 +77,12 @@ void BtreeIndex::openIndex( IndexInfo const & indexInfo,
rootNode.clear();
}
vector< WordArticleLink > BtreeIndex::findArticles( wstring const & str )
vector< WordArticleLink > BtreeIndex::findArticles( wstring const & word )
{
vector< WordArticleLink > result;
try
{
// Exast search - unescape all wildcard symbols
wstring word = Folding::unescapeWildcardSymbols( str );
wstring folded = Folding::apply( word );
if( folded.empty() )
folded = Folding::applyWhitespaceOnly( word );
@ -216,11 +213,6 @@ void BtreeWordSearchRequest::run()
str.find( '?' ) != wstring::npos ||
str.find( '[' ) != wstring::npos ||
str.find( ']' ) != wstring::npos );
else
{
// Exast search - unescape all wildcard symbols
str = Folding::unescapeWildcardSymbols( str );
}
wstring folded = Folding::apply( str );

View file

@ -204,7 +204,6 @@ void DictHeadwords::itemClicked( const QModelIndex & index )
if ( value.canConvert< QString >() )
{
QString headword = value.toString();
headword.replace( QRegExp( "([\\*\\?\\[\\]])" ), "\\\\1" );
emit headwordSelected( headword );
}
}

View file

@ -2,6 +2,7 @@
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "folding.hh"
#include <QRegExp>
namespace Folding {
@ -664,28 +665,18 @@ void normalizeWhitespace( wstring & str )
}
}
wstring unescapeWildcardSymbols( wstring const & in )
QString escapeWildcardSymbols( const QString & str )
{
wstring tmp;
tmp.reserve( in.size() );
QString escaped( str );
escaped.replace( QRegExp( "([\\[\\]\\?\\*])", Qt::CaseInsensitive ), "\\\\1" );
return escaped;
}
wchar const * wordBegin = in.c_str();
for( ; *wordBegin; ++wordBegin )
{
if( *wordBegin == '*' || *wordBegin == '?'
|| *wordBegin == '[' || *wordBegin == ']' )
{
wstring::size_type n = tmp.size();
if( n && tmp[ n - 1 ] == '\\' )
{
tmp[ n - 1 ] = *wordBegin;
continue;
}
}
tmp.push_back( *wordBegin );
}
return tmp;
QString unescapeWildcardSymbols( const QString & str )
{
QString unescaped( str );
unescaped.replace( QRegExp( "\\\\([\\[\\]\\?\\*])", Qt::CaseInsensitive ), "\\1" );
return unescaped;
}
}

View file

@ -5,6 +5,7 @@
#define __FOLDING_HH_INCLUDED__
#include "wstring.hh"
#include <QString>
/// Folding provides means to translate several possible ways to write a
/// symbol into one. This facilitates searching. Here we currently perform
@ -79,7 +80,10 @@ void normalizeWhitespace( wstring & );
//ssize_t apply( wchar const * in, wchar * out, size_t outSize );
/// Unescape all wildcard symbols (for exast search)
wstring unescapeWildcardSymbols( wstring const & );
QString unescapeWildcardSymbols( QString const & );
/// Unescape all wildcard symbols (for place word to input line)
QString escapeWildcardSymbols( QString const & );
}

View file

@ -403,7 +403,6 @@ void FullTextSearchDialog::itemClicked( const QModelIndex & idx )
if( idx.isValid() && idx.row() < results.size() )
{
QString headword = results[ idx.row() ].headword;
headword.replace( QRegExp( "([\\*\\?\\[\\]])" ), "\\\\1" );
emit showTranslationFor( headword, results[ idx.row() ].dictIDs, searchRegExp );
}
}

View file

@ -2091,7 +2091,7 @@ void MainWindow::translateInputChanged( QString const & newValue )
void MainWindow::translateInputFinished( bool checkModifiers )
{
QString word = translateLine->text();
QString word = Folding::unescapeWildcardSymbols( translateLine->text() );
if ( word.size() )
{
@ -2513,9 +2513,9 @@ void MainWindow::showHistoryItem( QString const & word )
history.enableAdd( false );
if ( cfg.preferences.searchInDock )
translateLine->setText( word );
translateLine->setText( Folding::escapeWildcardSymbols( word ) );
else
translateBox->setText( word, false );
translateBox->setText( Folding::escapeWildcardSymbols( word ), false );
showTranslationFor( word );
@ -3487,7 +3487,7 @@ ArticleView * MainWindow::getCurrentArticleView()
void MainWindow::wordReceived( const QString & word)
{
toggleMainWindow( true );
translateLine->setText( word );
translateLine->setText( Folding::escapeWildcardSymbols( word ) );
translateInputFinished( false );
}