From 3b7ad7eb37943682f059a0ca3dcdf07e5b3077cd Mon Sep 17 00:00:00 2001 From: mandlil <138015259+mandlil@users.noreply.github.com> Date: Thu, 20 Jun 2024 08:48:50 +0000 Subject: [PATCH] Area code information lost for all MX numbers in v8.13.38 metadata update (#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 --- cpp/src/phonenumbers/phonenumberutil.cc | 19 ++++++++++++++++--- cpp/test/phonenumbers/phonenumberutil_test.cc | 6 ++++++ .../i18n/phonenumbers/PhoneNumberUtil.java | 18 +++++++++++++++--- .../phonenumbers/PhoneNumberUtilTest.java | 3 +++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/cpp/src/phonenumbers/phonenumberutil.cc b/cpp/src/phonenumbers/phonenumberutil.cc index 01fe6f02cc..9a872930ab 100644 --- a/cpp/src/phonenumbers/phonenumberutil.cc +++ b/cpp/src/phonenumbers/phonenumberutil.cc @@ -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 @@ -696,6 +697,10 @@ class PhoneNumberRegExpsAndMappings { // to be an area code. std::set geo_mobile_countries_without_mobile_area_codes_; + // Set of country codes that doesn't have national prefix, but it has area + // codes. + std::set 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. @@ -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( @@ -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) != diff --git a/cpp/test/phonenumbers/phonenumberutil_test.cc b/cpp/test/phonenumbers/phonenumberutil_test.cc index fa05a4d53f..4f69903492 100644 --- a/cpp/test/phonenumbers/phonenumberutil_test.cc +++ b/cpp/test/phonenumbers/phonenumberutil_test.cc @@ -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); diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index c49189fa2e..e85cb65b52 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -85,6 +85,9 @@ public class PhoneNumberUtil { // considered to be an area code. private static final Set 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 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 @@ -127,6 +130,11 @@ public class PhoneNumberUtil { GEO_MOBILE_COUNTRIES_WITHOUT_MOBILE_AREA_CODES = Collections.unmodifiableSet(geoMobileCountriesWithoutMobileAreaCodes); + HashSet countriesWithoutNationalPrefixWithAreaCodes = new HashSet<>(); + countriesWithoutNationalPrefixWithAreaCodes.add(52); // Mexico + COUNTRIES_WITHOUT_NATIONAL_PREFIX_WITH_AREA_CODES = + Collections.unmodifiableSet(countriesWithoutNationalPrefixWithAreaCodes); + HashSet geoMobileCountries = new HashSet<>(); geoMobileCountries.add(52); // Mexico geoMobileCountries.add(54); // Argentina @@ -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 diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index cb56077e4a..a8bfec34cf 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -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));