Skip to content

Commit

Permalink
Area code information lost for all MX numbers in v8.13.38 metadata up…
Browse files Browse the repository at this point in the history
…date (#3535)

* Area code information lost for all MX numbers in v8.13.38 metadata update.

* add mexico to new variable

* C++ changes for Mexico to show area code
  • Loading branch information
mandlil authored Jun 20, 2024
1 parent 31a5a03 commit 3b7ad7e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
19 changes: 16 additions & 3 deletions cpp/src/phonenumbers/phonenumberutil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ class PhoneNumberRegExpsAndMappings {
}

mobile_token_mappings_.insert(std::make_pair(54, '9'));
countries_without_national_prefix_with_area_codes_.insert(52); // Mexico
geo_mobile_countries_without_mobile_area_codes_.insert(86); // China
geo_mobile_countries_.insert(52); // Mexico
geo_mobile_countries_.insert(54); // Argentina
Expand Down Expand Up @@ -696,6 +697,10 @@ class PhoneNumberRegExpsAndMappings {
// to be an area code.
std::set<int> geo_mobile_countries_without_mobile_area_codes_;

// Set of country codes that doesn't have national prefix, but it has area
// codes.
std::set<int> countries_without_national_prefix_with_area_codes_;

// Set of country calling codes that have geographically assigned mobile
// numbers. This may not be complete; we add calling codes case by case, as we
// find geographical mobile numbers or hear from user reports.
Expand Down Expand Up @@ -795,6 +800,7 @@ class PhoneNumberRegExpsAndMappings {
alpha_phone_mappings_(),
all_plus_number_grouping_symbols_(),
mobile_token_mappings_(),
countries_without_national_prefix_with_area_codes_(),
geo_mobile_countries_without_mobile_area_codes_(),
geo_mobile_countries_(),
single_international_prefix_(regexp_factory_->CreateRegExp(
Expand Down Expand Up @@ -2676,15 +2682,22 @@ int PhoneNumberUtil::GetLengthOfGeographicalAreaCode(
if (!metadata) {
return 0;
}

PhoneNumberType type = GetNumberType(number);
int country_calling_code = number.country_code();

// If a country doesn't use a national prefix, and this number doesn't have an
// Italian leading zero, we assume it is a closed dialling plan with no area
// codes.
if (!metadata->has_national_prefix() && !number.italian_leading_zero()) {
// Note:this is our general assumption, but there are exceptions which are
// tracked in COUNTRIES_WITHOUT_NATIONAL_PREFIX_WITH_AREA_CODES.
if (!metadata->has_national_prefix() && !number.italian_leading_zero() &&
reg_exps_->countries_without_national_prefix_with_area_codes_.find(
country_calling_code) ==
reg_exps_->countries_without_national_prefix_with_area_codes_.end()) {
return 0;
}

PhoneNumberType type = GetNumberType(number);
int country_calling_code = number.country_code();
if (type == PhoneNumberUtil::MOBILE &&
reg_exps_->geo_mobile_countries_without_mobile_area_codes_.find(
country_calling_code) !=
Expand Down
6 changes: 6 additions & 0 deletions cpp/test/phonenumbers/phonenumberutil_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,12 @@ TEST_F(PhoneNumberUtilTest, GetLengthOfGeographicalAreaCode) {
number.set_national_number(uint64{293744000});
EXPECT_EQ(1, phone_util_.GetLengthOfGeographicalAreaCode(number));

// Mexico numbers - there is no national prefix, but it still has an area
// code.
number.set_country_code(52);
number.set_national_number(uint64_t{3312345678});
EXPECT_EQ(2, phone_util_.GetLengthOfGeographicalAreaCode(number));

// Italian numbers - there is no national prefix, but it still has an area
// code.
number.set_country_code(39);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public class PhoneNumberUtil {
// considered to be an area code.
private static final Set<Integer> GEO_MOBILE_COUNTRIES_WITHOUT_MOBILE_AREA_CODES;

// Set of country codes that doesn't have national prefix, but it has area codes.
private static final Set<Integer> COUNTRIES_WITHOUT_NATIONAL_PREFIX_WITH_AREA_CODES;

// Set of country calling codes that have geographically assigned mobile numbers. This may not be
// complete; we add calling codes case by case, as we find geographical mobile numbers or hear
// from user reports. Note that countries like the US, where we can't distinguish between
Expand Down Expand Up @@ -127,6 +130,11 @@ public class PhoneNumberUtil {
GEO_MOBILE_COUNTRIES_WITHOUT_MOBILE_AREA_CODES =
Collections.unmodifiableSet(geoMobileCountriesWithoutMobileAreaCodes);

HashSet<Integer> countriesWithoutNationalPrefixWithAreaCodes = new HashSet<>();
countriesWithoutNationalPrefixWithAreaCodes.add(52); // Mexico
COUNTRIES_WITHOUT_NATIONAL_PREFIX_WITH_AREA_CODES =
Collections.unmodifiableSet(countriesWithoutNationalPrefixWithAreaCodes);

HashSet<Integer> geoMobileCountries = new HashSet<>();
geoMobileCountries.add(52); // Mexico
geoMobileCountries.add(54); // Argentina
Expand Down Expand Up @@ -893,14 +901,18 @@ public int getLengthOfGeographicalAreaCode(PhoneNumber number) {
if (metadata == null) {
return 0;
}

PhoneNumberType type = getNumberType(number);
int countryCallingCode = number.getCountryCode();
// If a country doesn't use a national prefix, and this number doesn't have an Italian leading
// zero, we assume it is a closed dialling plan with no area codes.
if (!metadata.hasNationalPrefix() && !number.isItalianLeadingZero()) {
// Note:this is our general assumption, but there are exceptions which are tracked in
// COUNTRIES_WITHOUT_NATIONAL_PREFIX_WITH_AREA_CODES.
if (!metadata.hasNationalPrefix() && !number.isItalianLeadingZero()
&& !COUNTRIES_WITHOUT_NATIONAL_PREFIX_WITH_AREA_CODES.contains(countryCallingCode)) {
return 0;
}

PhoneNumberType type = getNumberType(number);
int countryCallingCode = number.getCountryCode();
if (type == PhoneNumberType.MOBILE
// Note this is a rough heuristic; it doesn't cover Indonesia well, for example, where area
// codes are present for some mobile phones but not for others. We have no better way of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ public void testGetLengthOfGeographicalAreaCode() {
// Italian numbers - there is no national prefix, but it still has an area code.
assertEquals(2, phoneUtil.getLengthOfGeographicalAreaCode(IT_NUMBER));

// Mexico numbers - there is no national prefix, but it still has an area code.
assertEquals(2, phoneUtil.getLengthOfGeographicalAreaCode(MX_NUMBER1));

// Google Singapore. Singapore has no area code and no national prefix.
assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(SG_NUMBER));

Expand Down

0 comments on commit 3b7ad7e

Please sign in to comment.