diff options
author | Dmitri Plotnikov <dplotnikov@google.com> | 2011-01-06 13:48:50 -0800 |
---|---|---|
committer | Dmitri Plotnikov <dplotnikov@google.com> | 2011-01-06 13:49:18 -0800 |
commit | 46bdb5c49f8374539ec89e85cbb1a9ee7b3a2b10 (patch) | |
tree | 9967bac9881d54cf6d5690dd1a7be2b002c7e225 | |
parent | 95c34edce550d0869113085e0cd1b6b09e8fe38b (diff) |
DO NOT MERGE. Adding a custom function to support legacy API compatibility
Bug: 3210604
Change-Id: Ie65c053ec5178d6cd991f69a5a2b6d5d51938ade
-rw-r--r-- | android/PhoneNumberUtils.cpp | 36 | ||||
-rw-r--r-- | android/PhoneNumberUtils.h | 1 | ||||
-rw-r--r-- | android/PhoneNumberUtilsTest.cpp | 38 | ||||
-rw-r--r-- | android/sqlite3_android.cpp | 34 |
4 files changed, 107 insertions, 2 deletions
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 @@ bool phone_number_compare_strict(const char* a, const char* b) 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 @@ namespace android { 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 @@ using namespace android; }) #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 @@ int main() { // 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_numbers_equal(sqlite3_context * context, int argc, sqlite3_val } } +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 @@ extern "C" int register_android_functions(sqlite3 * handle, int utf16Storage) 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; } |