From cf9fdcd09e9855d3cde168eab9e5449e59c14ec4 Mon Sep 17 00:00:00 2001 From: Henry Lancelle Date: Wed, 3 Oct 2018 00:15:02 -0700 Subject: [PATCH] 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. --- cockatrice/src/carddatabasemodel.cpp | 16 ++++++++++++++++ cockatrice/src/carddatabasemodel.h | 7 ++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cockatrice/src/carddatabasemodel.cpp b/cockatrice/src/carddatabasemodel.cpp index bba3722f..faa8c3a8 100644 --- a/cockatrice/src/carddatabasemodel.cpp +++ b/cockatrice/src/carddatabasemodel.cpp @@ -1,5 +1,6 @@ #include "carddatabasemodel.h" #include "filtertree.h" +#include #define CARDDBMODEL_COLUMNS 6 @@ -17,6 +18,11 @@ CardDatabaseModel::~CardDatabaseModel() { } +QMap 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 &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) { } diff --git a/cockatrice/src/carddatabasemodel.h b/cockatrice/src/carddatabasemodel.h index a89c1f30..5400c1f4 100644 --- a/cockatrice/src/carddatabasemodel.h +++ b/cockatrice/src/carddatabasemodel.h @@ -73,6 +73,9 @@ private: FilterTree *filterTree; int loadedRowCount; + /** The translation table that will be used for sanitizeCardName. */ + static QMap 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 &_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 &table); }; class TokenDisplayModel : public CardDatabaseDisplayModel