Don't recreate QRegularExpression for leading zeroes each time

Creating a QRegularExpression is quite expensive and we use the same expression for each run. Sharing it between the runs leads to a significant performance improvement.
This commit is contained in:
Nicolas Fella 2020-04-08 10:23:40 +02:00
parent 43ea8052fe
commit f8674db931

View file

@ -110,13 +110,15 @@ SmsHelper::CountryCode SmsHelper::determineCountryCode(const QString& canonicalN
QString SmsHelper::canonicalizePhoneNumber(const QString& phoneNumber) QString SmsHelper::canonicalizePhoneNumber(const QString& phoneNumber)
{ {
static const QRegularExpression leadingZeroes(QStringLiteral("^0*"));
QString toReturn(phoneNumber); QString toReturn(phoneNumber);
toReturn = toReturn.remove(QStringLiteral(" ")); toReturn = toReturn.remove(QStringLiteral(" "));
toReturn = toReturn.remove(QStringLiteral("-")); toReturn = toReturn.remove(QStringLiteral("-"));
toReturn = toReturn.remove(QStringLiteral("(")); toReturn = toReturn.remove(QStringLiteral("("));
toReturn = toReturn.remove(QStringLiteral(")")); toReturn = toReturn.remove(QStringLiteral(")"));
toReturn = toReturn.remove(QStringLiteral("+")); toReturn = toReturn.remove(QStringLiteral("+"));
toReturn = toReturn.remove(QRegularExpression(QStringLiteral("^0*"))); // Strip leading zeroes toReturn = toReturn.remove(leadingZeroes);
if (toReturn.length() == 0) { if (toReturn.length() == 0) {
// If we have stripped away everything, assume this is a special number (and already canonicalized) // If we have stripped away everything, assume this is a special number (and already canonicalized)