Adding a custom function to support legacy API compatibility
Bug: 3210604
Change-Id: I6e9a20ec7887375b67c42a28cff54e2e9d399ea6
diff --git a/android/PhoneNumberUtils.cpp b/android/PhoneNumberUtils.cpp
index 775e33f..a753f42 100644
--- a/android/PhoneNumberUtils.cpp
+++ b/android/PhoneNumberUtils.cpp
@@ -433,4 +433,40 @@
return phone_number_compare_inter(a, b, true);
}
+/**
+ * Imitates the Java method PhoneNumberUtils.getStrippedReversed.
+ * Used for API compatibility with Android 1.6 and earlier.
+ */
+bool phone_number_stripped_reversed_inter(const char* in, char* out, const int len, int *outlen) {
+ int in_len = strlen(in);
+ int out_len = 0;
+ bool have_seen_plus = false;
+ for (int i = in_len; --i >= 0;) {
+ char c = in[i];
+ if ((c >= '0' && c <= '9') || c == '*' || c == '#' || c == 'N') {
+ if (out_len < len) {
+ out[out_len++] = c;
+ }
+ } else {
+ switch (c) {
+ case '+':
+ if (!have_seen_plus) {
+ if (out_len < len) {
+ out[out_len++] = c;
+ }
+ have_seen_plus = true;
+ }
+ break;
+ case ',':
+ case ';':
+ out_len = 0;
+ break;
+ }
+ }
+ }
+
+ *outlen = out_len;
+ return true;
+}
+
} // namespace android
diff --git a/android/PhoneNumberUtils.h b/android/PhoneNumberUtils.h
index b8d9939..58b9ab8 100644
--- a/android/PhoneNumberUtils.h
+++ b/android/PhoneNumberUtils.h
@@ -22,6 +22,7 @@
bool phone_number_compare_loose(const char* a, const char* b);
bool phone_number_compare_strict(const char* a, const char* b);
+bool phone_number_stripped_reversed_inter(const char* in, char* out, const int len, int *outlen);
} // namespace android
diff --git a/android/PhoneNumberUtilsTest.cpp b/android/PhoneNumberUtilsTest.cpp
index 3445652..cf67286 100644
--- a/android/PhoneNumberUtilsTest.cpp
+++ b/android/PhoneNumberUtilsTest.cpp
@@ -29,6 +29,8 @@
using namespace android;
+#define PHONE_NUMBER_BUFFER_SIZE 6
+
#define EXPECT(function, input1, input2, expected, total, error) \
({ \
const char *i1_cache = input1; \
@@ -47,14 +49,30 @@
})
#define EXPECT_EQ(input1, input2) \
- EXPECT(phone_number_compare_strict, (input1), (input2), true, \
+ EXPECT(phone_number_compare_strict, (input1), (input2), true, \
(total), (error))
#define EXPECT_NE(input1, input2) \
- EXPECT(phone_number_compare_strict, (input1), (input2), false, \
+ EXPECT(phone_number_compare_strict, (input1), (input2), false, \
(total), (error))
+#define ASSERT_STRIPPED_REVERSE(input, expected) \
+ ({ \
+ char out[PHONE_NUMBER_BUFFER_SIZE]; \
+ int outlen; \
+ (total)++; \
+ phone_number_stripped_reversed_inter((input), \
+ out, \
+ PHONE_NUMBER_BUFFER_SIZE, \
+ &outlen); \
+ out[outlen] = 0; \
+ if (strcmp((expected), (out)) != 0) { \
+ printf("Expected: %s actual: %s\n", (expected), (out)); \
+ (error)++; \
+ } \
+ })
+
int main() {
int total = 0;
int error = 0;
@@ -152,6 +170,22 @@
// but there is no sensible way to know it now (as of 2009-6-12)...
// EXPECT_NE("290-1234-5678", "+819012345678");
+ ASSERT_STRIPPED_REVERSE("", "");
+ ASSERT_STRIPPED_REVERSE("123", "321");
+ ASSERT_STRIPPED_REVERSE("123*N#", "#N*321");
+
+ // Buffer overflow
+ ASSERT_STRIPPED_REVERSE("1234567890", "098765");
+
+ // Only one plus is copied
+ ASSERT_STRIPPED_REVERSE("1+2+", "+21");
+
+ // Pause/wait in the phone number
+ ASSERT_STRIPPED_REVERSE("12;34", "21");
+
+ // Ignoring non-dialable
+ ASSERT_STRIPPED_REVERSE("1A2 3?4", "4321");
+
printf("total: %d, error: %d\n\n", total, error);
if (error == 0) {
printf("Success!\n");
diff --git a/android/sqlite3_android.cpp b/android/sqlite3_android.cpp
index ab52c5c..5cc39d8 100644
--- a/android/sqlite3_android.cpp
+++ b/android/sqlite3_android.cpp
@@ -34,6 +34,7 @@
#define ENABLE_ANDROID_LOG 0
#define SMALL_BUFFER_SIZE 10
+#define PHONE_NUMBER_BUFFER_SIZE 40
static int collate16(void *p, int n1, const void *v1, int n2, const void *v2)
{
@@ -153,6 +154,27 @@
}
}
+static void phone_number_stripped_reversed(sqlite3_context * context, int argc,
+ sqlite3_value ** argv)
+{
+ if (argc != 1) {
+ sqlite3_result_int(context, 0);
+ return;
+ }
+
+ char const * number = (char const *)sqlite3_value_text(argv[0]);
+ if (number == NULL) {
+ sqlite3_result_null(context);
+ return;
+ }
+
+ char out[PHONE_NUMBER_BUFFER_SIZE];
+ int outlen = 0;
+ android::phone_number_stripped_reversed_inter(number, out, PHONE_NUMBER_BUFFER_SIZE, &outlen);
+ sqlite3_result_text(context, (const char*)out, outlen, SQLITE_TRANSIENT);
+}
+
+
#if ENABLE_ANDROID_LOG
static void android_log(sqlite3_context * context, int argc, sqlite3_value ** argv)
{
@@ -582,5 +604,17 @@
return err;
}
+ // Register the _PHONE_NUMBER_STRIPPED_REVERSED function, which imitates
+ // PhoneNumberUtils.getStrippedReversed. This function is not public API,
+ // it is only used for compatibility with Android 1.6 and earlier.
+ err = sqlite3_create_function(handle,
+ "_PHONE_NUMBER_STRIPPED_REVERSED",
+ 1, SQLITE_UTF8, NULL,
+ phone_number_stripped_reversed,
+ NULL, NULL);
+ if (err != SQLITE_OK) {
+ return err;
+ }
+
return SQLITE_OK;
}