Browse Source

JAVA/CPP: Handle phone number input in RFC3966 with missing "tel:" prefix.

Review URL: https://codereview.appspot.com/96510044
pull/567/head
Shaopeng Jia 12 years ago
committed by Mihaela Rosca
parent
commit
ebb02ef276
4 changed files with 28 additions and 8 deletions
  1. +9
    -5
      cpp/src/phonenumbers/phonenumberutil.cc
  2. +8
    -0
      cpp/test/phonenumbers/phonenumberutil_test.cc
  3. +7
    -3
      java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
  4. +4
    -0
      java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java

+ 9
- 5
cpp/src/phonenumbers/phonenumberutil.cc View File

@ -1750,13 +1750,17 @@ void PhoneNumberUtil::BuildNationalNumberForParsing(
// Now append everything between the "tel:" prefix and the phone-context. // Now append everything between the "tel:" prefix and the phone-context.
// This should include the national number, an optional extension or // 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( StrAppend(
national_number, 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 { } else {
// Extract a possible number from the string passed in (this strips leading // Extract a possible number from the string passed in (this strips leading
// characters that could not be the start of a phone number.) // characters that could not be the start of a phone number.)


+ 8
- 0
cpp/test/phonenumbers/phonenumberutil_test.cc View File

@ -2981,6 +2981,10 @@ TEST_F(PhoneNumberUtilTest, ParseNationalNumber) {
phone_util_.Parse("tel:331-6005;phone-context=+64-3", phone_util_.Parse("tel:331-6005;phone-context=+64-3",
RegionCode::US(), &test_number)); RegionCode::US(), &test_number));
EXPECT_EQ(nz_number, 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 // Test parsing RFC3966 format with optional user-defined parameters. The
// parameters will appear after the context if present. // parameters will appear after the context if present.
EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR,
@ -2996,6 +3000,10 @@ TEST_F(PhoneNumberUtilTest, ParseNationalNumber) {
phone_util_.Parse("tel:+64-3-331-6005;isub=12345", phone_util_.Parse("tel:+64-3-331-6005;isub=12345",
RegionCode::US(), &test_number)); RegionCode::US(), &test_number));
EXPECT_EQ(nz_number, 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. // Testing international prefixes.
// Should strip country code. // Should strip country code.
EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR,


+ 7
- 3
java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java View File

@ -3021,9 +3021,13 @@ public class PhoneNumberUtil {
} }
// Now append everything between the "tel:" prefix and the phone-context. This should include // 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 { } else {
// Extract a possible number from the string passed in (this strips leading characters that // Extract a possible number from the string passed in (this strips leading characters that
// could not be the start of a phone number.) // could not be the start of a phone number.)


+ 4
- 0
java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java View File

@ -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: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.NZ));
assertEquals(NZ_NUMBER, phoneUtil.parse("tel:331-6005;phone-context=+64-3", RegionCode.US)); 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 // Test parsing RFC3966 format with optional user-defined parameters. The parameters will appear
// after the context if present. // after the context if present.
assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;phone-context=+64;a=%A1", 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", assertEquals(NZ_NUMBER, phoneUtil.parse("tel:03-331-6005;isub=12345;phone-context=+64",
RegionCode.NZ)); RegionCode.NZ));
assertEquals(NZ_NUMBER, phoneUtil.parse("tel:+64-3-331-6005;isub=12345", 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. // Testing international prefixes.
// Should strip country calling code. // Should strip country calling code.
assertEquals(NZ_NUMBER, phoneUtil.parse("0064 3 331 6005", RegionCode.NZ)); assertEquals(NZ_NUMBER, phoneUtil.parse("0064 3 331 6005", RegionCode.NZ));


Loading…
Cancel
Save