From d8d356da9d734af7c0208ccf8dd161970188c037 Mon Sep 17 00:00:00 2001 From: Matt Vogt Date: Thu, 24 Oct 2013 14:44:39 +1000 Subject: [PATCH] [libcontacts] Prefer complete phone number match over partial match If we are matching against the number '321', then a 3-digit match to the number '321' is superior to a 3-digit match with '33321'. --- src/seasidecache.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/seasidecache.cpp b/src/seasidecache.cpp index 0771571..d7dc877 100644 --- a/src/seasidecache.cpp +++ b/src/seasidecache.cpp @@ -325,6 +325,8 @@ QString::const_iterator firstDtmfChar(QString::const_iterator it, QString::const return end; } +const int ExactMatch = 100; + int matchLength(const QString &lhs, const QString &rhs) { if (lhs.isEmpty() || rhs.isEmpty()) @@ -348,8 +350,14 @@ int matchLength(const QString &lhs, const QString &rhs) --lit; --rit; if ((lit == lbegin) || (rit == rbegin)) { - if (*lit == *rit) + if (*lit == *rit) { ++matchLength; + + if ((lit == lbegin) && (rit == rbegin)) { + // We have a complete, exact match - this must be the best match + return ExactMatch; + } + } break; } } @@ -376,6 +384,9 @@ int bestPhoneNumberMatchLength(const QContact &contact, const QString &match) foreach (const QContactPhoneNumber& phone, contact.details()) { bestMatchLength = qMax(bestMatchLength, matchLength(SeasideCache::normalizePhoneNumber(phone.number()), match)); + if (bestMatchLength == ExactMatch) { + return ExactMatch; + } } return bestMatchLength; @@ -2769,6 +2780,8 @@ SeasideCache::CacheItem *SeasideCache::itemMatchingPhoneNumber(const QString &nu if (matchLength > bestMatchLength) { bestMatchLength = matchLength; matchItem = item; + if (bestMatchLength == ExactMatch) + break; } } }