Skip to content

Commit

Permalink
Area code information lost for all MX numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
twcclegg authored Jul 3, 2024
1 parent 408395c commit 7baab0c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions csharp/PhoneNumbers.Test/TestPhoneNumberUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ public void TestGetLengthOfGeographicalAreaCode()
// Italian numbers - there is no national prefix, but it still has an area code.
Assert.Equal(2, phoneUtil.GetLengthOfGeographicalAreaCode(ITNumber));

// Mexico numbers - there is no national prefix, but it still has an area code.
Assert.Equal(2, phoneUtil.GetLengthOfGeographicalAreaCode(MXNumber1));

// Google Singapore. Singapore has no area code and no national prefix.
Assert.Equal(0, phoneUtil.GetLengthOfGeographicalAreaCode(SGNumber));

Expand Down
14 changes: 11 additions & 3 deletions csharp/PhoneNumbers/PhoneNumberUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public partial class PhoneNumberUtil
private static bool IsGeoMobileCountryWithoutMobileAreaCode(int countryCallingCode)
=> countryCallingCode is 86; // China

// Set of country codes that doesn't have national prefix, but it has area codes.
private static bool IsCountryWithoutNationalPrefixWithAreaCodes(int countryCallingCode)
=> countryCallingCode is 52; // Mexico

// 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 @@ -662,14 +666,18 @@ public int GetLengthOfGeographicalAreaCode(PhoneNumber number)
var regionCode = GetRegionCodeForNumber(number);
if (!IsValidRegionCode(regionCode))
return 0;

var type = GetNumberType(number);
var countryCallingCode = number.CountryCode;
var metadata = GetMetadataForRegion(regionCode);
// 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.HasNumberOfLeadingZeros)
// 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.HasNumberOfLeadingZeros &&
!IsCountryWithoutNationalPrefixWithAreaCodes(countryCallingCode))
return 0;

var type = GetNumberType(number);
var countryCallingCode = number.CountryCode;
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

0 comments on commit 7baab0c

Please sign in to comment.