Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[qtcontacts-sqlite] Skip invalid alphabetic characters in phone numbers
Alphabetic characters in the DMTF character set are only valid if they
occur after some numeric component of the number.  If they occur
prior to any numeric component, they should be skipped in normalization.

This is to prevent non-number input such as 'Plumber' being normalized
into 'P'.
  • Loading branch information
matthewvogt committed Apr 24, 2014
1 parent 4f2fe48 commit ce88e62
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions src/extensions/qtcontacts-extensions_impl.h
Expand Up @@ -62,7 +62,8 @@ QString normalize(const QString &input, int flags, int maxCharacters)
QString subset;
subset.reserve(number.length());

QChar initialDigit;
QChar initialChar;
bool numericComponent = false;
int firstDtmfIndex = -1;

QString::const_iterator it = number.constBegin(), end = number.constEnd();
Expand All @@ -71,14 +72,15 @@ QString normalize(const QString &input, int flags, int maxCharacters)
// Convert to ASCII, capturing unicode digit values
const QChar digit(QChar::fromLatin1('0' + (*it).digitValue()));
subset.append(digit);
if (initialDigit.isNull()) {
initialDigit = digit;
numericComponent = true;
if (initialChar.isNull()) {
initialChar = digit;
}
} else if (*it == plus) {
if (initialDigit.isNull()) {
if (initialChar.isNull()) {
// This is the start of the diallable number
subset.append(*it);
initialDigit = *it;
initialChar = *it;
} else if (flags & QtContactsSqliteExtensions::ValidatePhoneNumber) {
// Not valid in this location
return QString();
Expand All @@ -88,30 +90,39 @@ QString normalize(const QString &input, int flags, int maxCharacters)
subset.append(*it);
}
} else if (dtmfChars.contains(*it)) {
if ((flags & QtContactsSqliteExtensions::KeepPhoneNumberDialString) == 0) {
// No need to continue accumulating
if ((*it).isLetter() && !numericComponent) {
// Alphabetic DTMF chars can only occur after some numeric component
if (flags & QtContactsSqliteExtensions::ValidatePhoneNumber) {
// Ensure the remaining characters are permissible
while (++it != end) {
if ((!(*it).isDigit()) && !allowedSeparators.contains(*it) && !dtmfChars.contains(*it)) {
// Invalid character
return QString();
return QString();
} else {
// Skip this character, not valid in this position
}
} else {
if ((flags & QtContactsSqliteExtensions::KeepPhoneNumberDialString) == 0) {
// No need to continue accumulating
if (flags & QtContactsSqliteExtensions::ValidatePhoneNumber) {
// Ensure the remaining characters are permissible
while (++it != end) {
if ((!(*it).isDigit()) && !allowedSeparators.contains(*it) && !dtmfChars.contains(*it)) {
// Invalid character
return QString();
}
}
}
break;
} else if (firstDtmfIndex == -1) {
firstDtmfIndex = subset.length();
}
break;
} else if (firstDtmfIndex == -1) {
firstDtmfIndex = subset.length();
subset.append(*it);
}
subset.append(*it);
} else if (flags & QtContactsSqliteExtensions::ValidatePhoneNumber) {
// Invalid character
return QString();
}
}

if ((flags & QtContactsSqliteExtensions::ValidatePhoneNumber) &&
(initialDigit == plus) && (firstDtmfIndex != -1)) {
(initialChar == plus) && (firstDtmfIndex != -1)) {
// If this number starts with '+', it mustn't contain control codes
if ((subset.indexOf(hashControl, firstDtmfIndex) != -1) ||
(subset.indexOf(starControl, firstDtmfIndex) != -1)) {
Expand Down

0 comments on commit ce88e62

Please sign in to comment.