From d7c0a1c882c6578bdad0b1ca39aa677abcc4e6cd Mon Sep 17 00:00:00 2001 From: "jia.shao.peng@gmail.com" Date: Tue, 20 May 2014 12:33:32 +0000 Subject: [PATCH] JAVA/CPP: Handle phone number input in RFC3966 with missing "tel:" prefix. Review URL: https://codereview.appspot.com/96510044 git-svn-id: http://libphonenumber.googlecode.com/svn/trunk@664 ee073f10-1060-11df-b6a4-87a95322a99c --- cpp/src/phonenumbers/phonenumberutil.cc | 14 +++++++++----- cpp/test/phonenumbers/phonenumberutil_test.cc | 8 ++++++++ .../src/com/google/i18n/phonenumbers/PhoneNumberUtil.java | 10 +++++++--- .../com/google/i18n/phonenumbers/PhoneNumberUtilTest.java | 4 ++++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/cpp/src/phonenumbers/phonenumberutil.cc b/cpp/src/phonenumbers/phonenumberutil.cc index 22249e8..3aa82f9 100644 --- a/cpp/src/phonenumbers/phonenumberutil.cc +++ b/cpp/src/phonenumbers/phonenumberutil.cc @@ -1750,13 +1750,17 @@ void PhoneNumberUtil::BuildNationalNumberForParsing( // Now append everything between the "tel:" prefix and the phone-context. // This should include the national number, an optional extension or - // isdn-subaddress component. - int end_of_rfc_prefix = - number_to_parse.find(kRfc3966Prefix) + strlen(kRfc3966Prefix); + // isdn-subaddress component. Note we also handle the case when "tel:" is + // missing, as we have seen in some of the phone number inputs. In that + // case, we append everything from the beginning. + size_t index_of_rfc_prefix = number_to_parse.find(kRfc3966Prefix); + int index_of_national_number = (index_of_rfc_prefix != string::npos) ? + index_of_rfc_prefix + strlen(kRfc3966Prefix) : 0; StrAppend( national_number, - number_to_parse.substr(end_of_rfc_prefix, - index_of_phone_context - end_of_rfc_prefix)); + number_to_parse.substr( + index_of_national_number, + index_of_phone_context - index_of_national_number)); } else { // Extract a possible number from the string passed in (this strips leading // characters that could not be the start of a phone number.) diff --git a/cpp/test/phonenumbers/phonenumberutil_test.cc b/cpp/test/phonenumbers/phonenumberutil_test.cc index 89ef1e2..b5ee703 100644 --- a/cpp/test/phonenumbers/phonenumberutil_test.cc +++ b/cpp/test/phonenumbers/phonenumberutil_test.cc @@ -2981,6 +2981,10 @@ TEST_F(PhoneNumberUtilTest, ParseNationalNumber) { phone_util_.Parse("tel:331-6005;phone-context=+64-3", RegionCode::US(), &test_number)); EXPECT_EQ(nz_number, test_number); + EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, + phone_util_.Parse("My number is tel:03-331-6005;phone-context=+64", + RegionCode::NZ(), &test_number)); + EXPECT_EQ(nz_number, test_number); // Test parsing RFC3966 format with optional user-defined parameters. The // parameters will appear after the context if present. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, @@ -2996,6 +3000,10 @@ TEST_F(PhoneNumberUtilTest, ParseNationalNumber) { phone_util_.Parse("tel:+64-3-331-6005;isub=12345", RegionCode::US(), &test_number)); EXPECT_EQ(nz_number, test_number); + EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, + phone_util_.Parse("03-331-6005;phone-context=+64", + RegionCode::NZ(), &test_number)); + EXPECT_EQ(nz_number, test_number); // Testing international prefixes. // Should strip country code. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index a182fa0..77b32c3 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -3021,9 +3021,13 @@ public class PhoneNumberUtil { } // Now append everything between the "tel:" prefix and the phone-context. This should include - // the national number, an optional extension or isdn-subaddress component. - nationalNumber.append(numberToParse.substring( - numberToParse.indexOf(RFC3966_PREFIX) + RFC3966_PREFIX.length(), indexOfPhoneContext)); + // the national number, an optional extension or isdn-subaddress component. Note we also + // handle the case when "tel:" is missing, as we have seen in some of the phone number inputs. + // In that case, we append everything from the beginning. + int indexOfRfc3966Prefix = numberToParse.indexOf(RFC3966_PREFIX); + int indexOfNationalNumber = (indexOfRfc3966Prefix >= 0) ? + indexOfRfc3966Prefix + RFC3966_PREFIX.length() : 0; + nationalNumber.append(numberToParse.substring(indexOfNationalNumber, indexOfPhoneContext)); } else { // Extract a possible number from the string passed in (this strips leading characters that // could not be the start of a phone number.) diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 4aabe94..30d26cd 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -1701,6 +1701,8 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;phone-context=+64", RegionCode.NZ)); assertEquals(NZ_NUMBER, phoneUtil.parse("tel:331-6005;phone-context=+64-3", RegionCode.NZ)); assertEquals(NZ_NUMBER, phoneUtil.parse("tel:331-6005;phone-context=+64-3", RegionCode.US)); + assertEquals(NZ_NUMBER, phoneUtil.parse( + "My number is tel:03-331-6005;phone-context=+64", RegionCode.NZ)); // Test parsing RFC3966 format with optional user-defined parameters. The parameters will appear // after the context if present. assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;phone-context=+64;a=%A1", @@ -1709,6 +1711,8 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;isub=12345;phone-context=+64", RegionCode.NZ)); assertEquals(NZ_NUMBER, phoneUtil.parse("tel:+64-3-331-6005;isub=12345", RegionCode.NZ)); + // Test parsing RFC3966 with "tel:" missing. + assertEquals(NZ_NUMBER, phoneUtil.parse("03-331-6005;phone-context=+64", RegionCode.NZ)); // Testing international prefixes. // Should strip country calling code. assertEquals(NZ_NUMBER, phoneUtil.parse("0064 3 331 6005", RegionCode.NZ)); -- 2.7.4