Navigation Menu

Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
[libcontacts] Prefer complete phone number match over partial match
Browse files Browse the repository at this point in the history
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'.
  • Loading branch information
matthewvogt committed Oct 24, 2013
1 parent b9d4941 commit d8d356d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/seasidecache.cpp
Expand Up @@ -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())
Expand All @@ -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;
}
}
Expand All @@ -376,6 +384,9 @@ int bestPhoneNumberMatchLength(const QContact &contact, const QString &match)

foreach (const QContactPhoneNumber& phone, contact.details<QContactPhoneNumber>()) {
bestMatchLength = qMax(bestMatchLength, matchLength(SeasideCache::normalizePhoneNumber(phone.number()), match));
if (bestMatchLength == ExactMatch) {
return ExactMatch;
}
}

return bestMatchLength;
Expand Down Expand Up @@ -2769,6 +2780,8 @@ SeasideCache::CacheItem *SeasideCache::itemMatchingPhoneNumber(const QString &nu
if (matchLength > bestMatchLength) {
bestMatchLength = matchLength;
matchItem = item;
if (bestMatchLength == ExactMatch)
break;
}
}
}
Expand Down

0 comments on commit d8d356d

Please sign in to comment.