summaryrefslogtreecommitdiff
path: root/android/PhoneNumberUtils.cpp
diff options
context:
space:
mode:
authorWei Huang <weih@google.com>2009-08-31 19:18:23 -0700
committerWei Huang <weih@google.com>2009-08-31 20:08:39 -0700
commit0d04c4caa9c0464f555eed458abf2ac7d997ad7d (patch)
treecffa20a5af1f3ce5036a62354d9658b8d3b1a153 /android/PhoneNumberUtils.cpp
parentb020ccd8772182ddfccba30b5adbc500982b59c1 (diff)
fix bug 2015059: where the native sqlite comparison method phone_number_compare() treated all alpha addresses as the same phone number.
- changed phone_number_compare() to handle alpha chars. Before this change the algorithm skipped over all non-dialable chars for comparison, and thus treating "foo" and "bar" as the same addresses. now we don't skip any char that's alpha. This would treat "foo" and "bar" as separate addresses, as well as treating "1-800-flowers" and "800-flowers" as the same number.
Diffstat (limited to 'android/PhoneNumberUtils.cpp')
-rw-r--r--android/PhoneNumberUtils.cpp36
1 files changed, 23 insertions, 13 deletions
diff --git a/android/PhoneNumberUtils.cpp b/android/PhoneNumberUtils.cpp
index 47dd279..d469ed7 100644
--- a/android/PhoneNumberUtils.cpp
+++ b/android/PhoneNumberUtils.cpp
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+#include <ctype.h>
#include <string.h>
namespace android {
@@ -84,12 +85,21 @@ static int tryGetISODigit (char ch)
}
}
-/** True if c is ISO-LATIN characters 0-9, *, # , + */
-static bool isNonSeparator(char ch)
+/**
+ * True if ch is ISO-LATIN characters 0-9, *, # , +
+ * Note this method current does not account for the WILD char 'N'
+ */
+static bool isDialable(char ch)
{
return ('0' <= ch && ch <= '9') || ch == '*' || ch == '#' || ch == '+';
}
+/** Returns true if ch is not dialable or alpha char */
+static bool isSeparator(char ch)
+{
+ return !isDialable(ch) && (isalpha(ch) == 0);
+}
+
/**
* Try to store the pointer to "new_ptr" which does not have trunk prefix.
*
@@ -114,7 +124,7 @@ static bool tryGetTrunkPrefixOmittedStr(const char *str, size_t len,
*new_len = len - (i + 1);
}
return true;
- } else if (isNonSeparator(ch)) {
+ } else if (isDialable(ch)) {
return false;
}
}
@@ -146,18 +156,18 @@ static int tryGetCountryCallingCode(const char *str, size_t len,
if (ch == '+') state = 1;
else if (ch == '0') state = 2;
else if (ch == '1') state = 8;
- else if (isNonSeparator(ch)) return -1;
+ else if (isDialable(ch)) return -1;
break;
case 2:
if (ch == '0') state = 3;
else if (ch == '1') state = 4;
- else if (isNonSeparator(ch)) return -1;
+ else if (isDialable(ch)) return -1;
break;
case 4:
if (ch == '1') state = 5;
- else if (isNonSeparator(ch)) return -1;
+ else if (isDialable(ch)) return -1;
break;
case 1:
@@ -183,14 +193,14 @@ static int tryGetCountryCallingCode(const char *str, size_t len,
} else {
state++;
}
- } else if (isNonSeparator(ch)) {
+ } else if (isDialable(ch)) {
return -1;
}
}
break;
case 8:
if (ch == '6') state = 9;
- else if (isNonSeparator(ch)) return -1;
+ else if (isDialable(ch)) return -1;
break;
case 9:
if (ch == '6') {
@@ -230,7 +240,7 @@ static bool checkPrefixIsIgnorable(const char* ch, int i) {
// Ignore just one digit, assuming it is trunk prefix.
trunk_prefix_was_read = true;
}
- } else if (isNonSeparator(ch[i])) {
+ } else if (isDialable(ch[i])) {
// Trunk prefix is a digit, not "*", "#"...
return false;
}
@@ -321,11 +331,11 @@ bool phone_number_compare(const char* a, const char* b)
bool skip_compare = false;
char ch_a = a[i_a];
char ch_b = b[i_b];
- if (!isNonSeparator(ch_a)) {
+ if (isSeparator(ch_a)) {
i_a--;
skip_compare = true;
}
- if (!isNonSeparator(ch_b)) {
+ if (isSeparator(ch_b)) {
i_b--;
skip_compare = true;
}
@@ -357,7 +367,7 @@ bool phone_number_compare(const char* a, const char* b)
bool may_be_namp = true;
while (i_a >= 0) {
const char ch_a = a[i_a];
- if (isNonSeparator(ch_a)) {
+ if (isDialable(ch_a)) {
if (may_be_namp && tryGetISODigit(ch_a) == 1) {
may_be_namp = false;
} else {
@@ -368,7 +378,7 @@ bool phone_number_compare(const char* a, const char* b)
}
while (i_b >= 0) {
const char ch_b = b[i_b];
- if (isNonSeparator(ch_b)) {
+ if (isDialable(ch_b)) {
if (may_be_namp && tryGetISODigit(ch_b) == 1) {
may_be_namp = false;
} else {