2
* Copyright (C) 2006 The Android Open Source Project
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
16
* Original source code available at: http://androidxref.com/4.0.4/xref/frameworks/base/telephony/java/android/telephony/PhoneNumberUtils.java
19
#ifndef PHONENUMBERUTILS_H
20
#define PHONENUMBERUTILS_H
22
namespace PhoneNumberUtils
25
/** True if c is ISO-LATIN characters 0-9, *, # , +, WILD, WAIT, PAUSE */
26
bool isNonSeparator(char c)
28
return (c >= '0' && c <= '9') || c == '*' || c == '#' || c == '+'
29
|| c == 'N' || c == ';' || c == ',';
32
/** True if c is ISO-LATIN characters 0-9, *, # , +, WILD */
33
bool isDialable(char c)
35
return (c >= '0' && c <= '9') || c == '*' || c == '#' || c == '+' || c == 'N';
38
/** True if c is ISO-LATIN characters 0-9 */
39
bool isISODigit (char c) {
40
return c >= '0' && c <= '9';
43
/** or -1 if both are negative */
44
int minPositive (int a, int b)
46
if (a >= 0 && b >= 0) {
47
return (a < b) ? a : b;
48
} else if (a >= 0) { /* && b < 0 */
50
} else if (b >= 0) { /* && a < 0 */
52
} else { /* a < 0 && b < 0 */
57
/** index of the last character of the network portion
58
* (eg anything after is a post-dial string)
60
int indexOfLastNetworkChar(const QString &a)
66
origLength = a.length();
68
pIndex = a.indexOf(',');
69
wIndex = a.indexOf(';');
71
trimIndex = minPositive(pIndex, wIndex);
74
return origLength - 1;
80
/** all of a up to len must be an international prefix or
81
* separators/non-dialing digits
83
bool matchIntlPrefix(const QString &a, int len)
85
/* '([^0-9*#+pwn]\+[^0-9*#+pwn] | [^0-9*#+pwn]0(0|11)[^0-9*#+pwn] )$' */
89
for (int i = 0 ; i < len ; i++) {
90
char c = a.at(i).toLatin1();
94
if (c == '+') state = 1;
95
else if (c == '0') state = 2;
96
else if (isNonSeparator(c)) return false;
100
if (c == '0') state = 3;
101
else if (c == '1') state = 4;
102
else if (isNonSeparator(c)) return false;
106
if (c == '1') state = 5;
107
else if (isNonSeparator(c)) return false;
111
if (isNonSeparator(c)) return false;
117
return state == 1 || state == 3 || state == 5;
120
/** all of 'a' up to len must match non-US trunk prefix ('0') */
121
bool matchTrunkPrefix(const QString &a, int len) {
126
for (int i = 0 ; i < len ; i++) {
127
char c = a.at(i).toLatin1();
129
if (c == '0' && !found) {
131
} else if (isNonSeparator(c)) {
139
/** all of 'a' up to len must be a (+|00|011)country code)
140
* We're fast and loose with the country code. Any \d{1,3} matches */
141
bool matchIntlPrefixAndCC(const QString &a, int len) {
142
/* [^0-9*#+pwn]*(\+|0(0|11)\d\d?\d? [^0-9*#+pwn] $ */
143
/* 0 1 2 3 45 6 7 8 */
146
for (int i = 0 ; i < len ; i++ ) {
147
char c = a.at(i).toLatin1();
151
if (c == '+') state = 1;
152
else if (c == '0') state = 2;
153
else if (isNonSeparator(c)) return false;
157
if (c == '0') state = 3;
158
else if (c == '1') state = 4;
159
else if (isNonSeparator(c)) return false;
163
if (c == '1') state = 5;
164
else if (isNonSeparator(c)) return false;
170
if (isISODigit(c)) state = 6;
171
else if (isNonSeparator(c)) return false;
176
if (isISODigit(c)) state++;
177
else if (isNonSeparator(c)) return false;
181
if (isNonSeparator(c)) return false;
185
return state == 6 || state == 7 || state == 8;
190
* Compare phone numbers a and b, return true if they're identical
191
* enough for caller ID purposes.
193
* - Compares from right to left
194
* - requires MIN_MATCH (7) characters to match
195
* - handles common trunk prefixes and international prefixes
196
* (basically, everything except the Russian trunk prefix)
198
* Note that this method does not return false even when the two phone numbers
199
* are not exactly same; rather; we can call this method "similar()", not "equals()".
203
bool compareLoosely(const QString &a, const QString &b)
207
int numNonDialableCharsInA = 0;
208
int numNonDialableCharsInB = 0;
210
if (a.length() == 0 || b.length() == 0) {
218
ia = indexOfLastNetworkChar (a);
219
ib = indexOfLastNetworkChar (b);
222
while (ia >= 0 && ib >=0) {
224
bool skipCmp = false;
226
ca = a.at(ia).toLatin1();
228
if (!isDialable(ca)) {
231
numNonDialableCharsInA++;
234
cb = b.at(ib).toLatin1();
236
if (!isDialable(cb)) {
239
numNonDialableCharsInB++;
243
if (cb != ca && ca != 'N' && cb != 'N') {
246
ia--; ib--; matched++;
251
int effectiveALen = a.length() - numNonDialableCharsInA;
252
int effectiveBLen = b.length() - numNonDialableCharsInB;
255
// if the number of dialable chars in a and b match, but the matched chars < MIN_MATCH,
256
// treat them as equal (i.e. 404-04 and 40404)
257
if (effectiveALen == effectiveBLen && effectiveALen == matched) {
264
// At least one string has matched completely;
265
if (matched >= 7 && (ia < 0 || ib < 0)) {
270
* Now, what remains must be one of the following for a
273
* - a '+' on one and a '00' or a '011' on the other
274
* - a '0' on one and a (+,00)<country code> on the other
275
* (for this, a '0' and a '00' prefix would have succeeded above)
278
if (matchIntlPrefix(a, ia + 1)
279
&& matchIntlPrefix (b, ib +1)
284
if (matchTrunkPrefix(a, ia + 1)
285
&& matchIntlPrefixAndCC(b, ib +1)
290
if (matchTrunkPrefix(b, ib + 1)
291
&& matchIntlPrefixAndCC(a, ia +1)