Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: refactor the escape(unescap)Amps #1300

Merged
merged 2 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/common/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ bool endsWithIgnoreCase( const string & str1, string str2 )
return ( str1.size() >= (unsigned)str2.size() )
&& ( strcasecmp( str1.c_str() + ( str1.size() - str2.size() ), str2.data() ) == 0 );
}

QString escapeAmps( QString const & str )
{
QString result( str );
result.replace( "&", "&&" );
return result;
}

QString unescapeAmps( QString const & str )
{
QString result( str );
result.replace( "&&", "&" );
return result;
}
} // namespace Utils

QString Utils::Path::combine( const QString & path1, const QString & path2 )
Expand Down
4 changes: 4 additions & 0 deletions src/common/utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ void removeDirectory( QString const & directory );
void removeDirectory( string const & directory );
} // namespace Fs

QString escapeAmps( QString const & str );

QString unescapeAmps( QString const & str );

} // namespace Utils

#endif // UTILS_HH
28 changes: 6 additions & 22 deletions src/ui/groups_widgets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "langcoder.hh"
#include "language.hh"
#include "metadata.hh"
#include "utils.hh"

#include <QDir>
#include <QFileDialog>
Expand Down Expand Up @@ -517,23 +518,6 @@ DictGroupsWidget::DictGroupsWidget( QWidget * parent ):
setUsesScrollButtons( true );
}

namespace {

QString escapeAmps( QString const & str )
{
QString result( str );
result.replace( "&", "&&" );
return result;
}

QString unescapeAmps( QString const & str )
{
QString result( str );
result.replace( "&&", "&" );
return result;
}

} // namespace

void DictGroupsWidget::populate( Config::Groups const & groups,
vector< sptr< Dictionary::Class > > const & allDicts_,
Expand All @@ -546,7 +530,7 @@ void DictGroupsWidget::populate( Config::Groups const & groups,

for ( int x = 0; x < groups.size(); ++x ) {
const auto gr = new DictGroupWidget( this, *allDicts, groups[ x ] );
addTab( gr, escapeAmps( groups[ x ].name ) );
addTab( gr, Utils::escapeAmps( groups[ x ].name ) );
connect( gr, &DictGroupWidget::showDictionaryInfo, this, &DictGroupsWidget::showDictionaryInfo );
connect( gr->getModel(), &DictListModel::contentChanged, this, &DictGroupsWidget::tabDataChanged );

Expand All @@ -569,7 +553,7 @@ Config::Groups DictGroupsWidget::makeGroups() const

for ( int x = 0; x < count(); ++x ) {
result.push_back( dynamic_cast< DictGroupWidget & >( *widget( x ) ).makeGroup() );
result.back().name = unescapeAmps( tabText( x ) );
result.back().name = Utils::unescapeAmps( tabText( x ) );
}

return result;
Expand Down Expand Up @@ -638,7 +622,7 @@ int DictGroupsWidget::addNewGroup( QString const & name )
newGroup.id = nextId++;

const auto gr = new DictGroupWidget( this, *allDicts, newGroup );
const int idx = insertTab( currentIndex() + 1, gr, escapeAmps( name ) );
const int idx = insertTab( currentIndex() + 1, gr, Utils::escapeAmps( name ) );
connect( gr, &DictGroupWidget::showDictionaryInfo, this, &DictGroupsWidget::showDictionaryInfo );

connect( gr->getModel(), &DictListModel::contentChanged, this, &DictGroupsWidget::tabDataChanged );
Expand Down Expand Up @@ -897,7 +881,7 @@ QString DictGroupsWidget::getCurrentGroupName() const
const int current = currentIndex();

if ( current >= 0 )
return unescapeAmps( tabText( current ) );
return Utils::unescapeAmps( tabText( current ) );

return QString();
}
Expand All @@ -907,7 +891,7 @@ void DictGroupsWidget::renameCurrentGroup( QString const & name )
const int current = currentIndex();

if ( current >= 0 )
setTabText( current, escapeAmps( name ) );
setTabText( current, Utils::escapeAmps( name ) );
}

void DictGroupsWidget::removeCurrentGroup()
Expand Down
11 changes: 3 additions & 8 deletions src/ui/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1814,8 +1814,7 @@ ArticleView * MainWindow::createNewTab( bool switchToIt, QString const & name )

int index = cfg.preferences.newTabsOpenAfterCurrentOne ? ui.tabWidget->currentIndex() + 1 : ui.tabWidget->count();

QString escaped = name;
escaped.replace( "&", "&&" );
QString escaped = Utils::escapeAmps( name );

ui.tabWidget->insertTab( index, view, escaped );
mruList.append( dynamic_cast< QWidget * >( view ) );
Expand Down Expand Up @@ -1943,7 +1942,7 @@ void MainWindow::titleChanged( ArticleView * view, QString const & title )
else {
escaped = title;
}
escaped.replace( "&", "&&" );
escaped = Utils::escapeAmps( escaped );

int index = ui.tabWidget->indexOf( view );
if ( !escaped.isEmpty() )
Expand Down Expand Up @@ -4264,11 +4263,7 @@ void MainWindow::showFTSIndexingName( QString const & name )
QString MainWindow::unescapeTabHeader( QString const & header )
{
// Reset table header to original headword

QString escaped = header;
escaped.replace( "&&", "&" );

return escaped;
return Utils::unescapeAmps( header );
}

void MainWindow::addCurrentTabToFavorites()
Expand Down
Loading