Translate curly apostrophe to ASCII apostrophe (#3395) (#3401)

* Translate curly apostrophe to ASCII apostrophe (#3395)

Treats the curly apostrophe as a straight apostrophe when searching in
the deck editor search bar.

* Moved logic for handeling translation to CardDatabaseDisplayModel.

This implementation was done with strings instead of characters because
the curly apostrophes and quotes are considered multi-characters.
Thus, the method of iterating through the string and replacing
the characters with the proper translations was difficult to cleanly
implement.

* clang-tidy modifications

* Implemented faster algorithm for string translation.

Optimized the algorithm for string translation, before it would compute
in O(N*M) time where N is the size of the string and M is the size of
the translation table.  Now it will compute in O(N) time where N is the
length of the string.

* Renaming variables and methods.

* Fixed character literal type, was unicode, is now wide.
This commit is contained in:
Henry Lancelle 2018-10-03 00:15:02 -07:00 committed by ctrlaltca
parent eb4b1e74f1
commit cf9fdcd09e
2 changed files with 22 additions and 1 deletions

View file

@ -1,5 +1,6 @@
#include "carddatabasemodel.h"
#include "filtertree.h"
#include <QMap>
#define CARDDBMODEL_COLUMNS 6
@ -17,6 +18,11 @@ CardDatabaseModel::~CardDatabaseModel()
{
}
QMap<wchar_t, wchar_t> CardDatabaseDisplayModel::characterTranslation = {{L'', L'\"'},
{L'', L'\"'},
{L'', L'\''},
{L'', L'\''}};
int CardDatabaseModel::rowCount(const QModelIndex & /*parent*/) const
{
return cardList.size();
@ -324,6 +330,16 @@ void CardDatabaseDisplayModel::filterTreeChanged()
invalidate();
}
const QString CardDatabaseDisplayModel::sanitizeCardName(const QString &dirtyName, const QMap<wchar_t, wchar_t> &table)
{
std::wstring toReturn = dirtyName.toStdWString();
for (wchar_t &ch : toReturn) {
if (table.contains(ch)) {
ch = table.value(ch);
}
}
return QString::fromStdWString(toReturn);
}
TokenDisplayModel::TokenDisplayModel(QObject *parent) : CardDatabaseDisplayModel(parent)
{
}

View file

@ -73,6 +73,9 @@ private:
FilterTree *filterTree;
int loadedRowCount;
/** The translation table that will be used for sanitizeCardName. */
static QMap<wchar_t, wchar_t> characterTranslation;
public:
CardDatabaseDisplayModel(QObject *parent = 0);
void setFilterTree(FilterTree *filterTree);
@ -88,7 +91,7 @@ public:
}
void setCardName(const QString &_cardName)
{
cardName = _cardName;
cardName = sanitizeCardName(_cardName, characterTranslation);
invalidate();
}
void setCardNameSet(const QSet<QString> &_cardNameSet)
@ -127,6 +130,8 @@ protected:
void fetchMore(const QModelIndex &parent);
private slots:
void filterTreeChanged();
/** Will translate all undesirable characters in DIRTYNAME according to the TABLE. */
const QString sanitizeCardName(const QString &dirtyName, const QMap<wchar_t, wchar_t> &table);
};
class TokenDisplayModel : public CardDatabaseDisplayModel