[Companion] Move CHECK_IN_ARRAY() to macros.h; Change DIM() return type; Refactor/fix removeAccents() to handle uppercase.

This commit is contained in:
Max Paperno
2018-09-19 06:47:36 -04:00
parent b4d992f490
commit 681bc82185
5 changed files with 41 additions and 35 deletions
+22 -21
View File
@@ -28,29 +28,30 @@
QString removeAccents(const QString & str)
{
QString result = str;
// UTF-8 ASCII Table
const QString tA[] = { "á", "â", "ã", "à", "ä" };
const QString tE[] = { "é", "è", "ê", "ě" };
const QString tI[] = { "í" };
const QString tO[] = { "ó", "ô", "õ", "ö" };
const QString tU[] = { "ú", "ü" };
const QString tC[] = { "ç" };
const QString tY[] = { "ý" };
const QString tS[] = { "š" };
const QString tR[] = { "ř" };
for (unsigned int i = 0; i < DIM(tA); i++) result.replace(tA[i], "a");
for (unsigned int i = 0; i < DIM(tE); i++) result.replace(tE[i], "e");
for (unsigned int i = 0; i < DIM(tI); i++) result.replace(tI[i], "i");
for (unsigned int i = 0; i < DIM(tO); i++) result.replace(tO[i], "o");
for (unsigned int i = 0; i < DIM(tU); i++) result.replace(tU[i], "u");
for (unsigned int i = 0; i < DIM(tC); i++) result.replace(tC[i], "c");
for (unsigned int i = 0; i < DIM(tY); i++) result.replace(tY[i], "y");
for (unsigned int i = 0; i < DIM(tS); i++) result.replace(tS[i], "s");
for (unsigned int i = 0; i < DIM(tR); i++) result.replace(tR[i], "r");
static const QHash<QString, QVariant> map = {
{"a", QRegularExpression("[áâãàä]")},
{"A", QRegularExpression("[ÁÂÃÀÄ]")},
{"e", QRegularExpression("[éèêě]")},
{"E", QRegularExpression("[ÉÈÊĚ]")},
{"o", QRegularExpression("[óôõö]")},
{"O", QRegularExpression("[ÓÔÕÖ]")},
{"u", QRegularExpression("[úü]")},
{"U", QRegularExpression("[ÚÜ]")},
{"i", "í"}, {"I", "Í"},
{"c", "ç"}, {"C", "Ç"},
{"y", "ý"}, {"Y", "Ý"},
{"s", "š"}, {"S", "Š"},
{"r", "ř"}, {"R", "Ř"}
};
QString result(str);
for (QHash<QString, QVariant>::const_iterator it = map.cbegin(), en = map.cend(); it != en; ++it) {
if (it.value().canConvert<QRegularExpression>())
result.replace(it.value().toRegularExpression(), it.key());
else
result.replace(it.value().toString(), it.key());
}
return result;
}