// 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.)
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,
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,
}
// 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.)
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",
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));