From f81c0ee69a26de213941cea815d0d3c125e00227 Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Mon, 16 Sep 2019 15:14:28 -0700 Subject: [PATCH] v8.10.18 --- README.md | 2 + csharp/PhoneNumbers/PhoneNumberUtil.cs | 119 ++++++++++++++------ resources/PhoneNumberMetaData.xml | 46 +++++--- resources/PhoneNumberMetaDataForTesting.xml | 13 --- resources/ShortNumberMetadata.xml | 29 +++-- resources/carrier/en/65.txt | 11 ++ resources/carrier/en/996.txt | 8 +- 7 files changed, 149 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index b26549287..e238c69ce 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ [![Build status](https://ci.appveyor.com/api/projects/status/76abbk0qveot0mbo/branch/master?svg=true)](https://ci.appveyor.com/project/twcclegg/libphonenumber-csharp/branch/master) [![codecov](https://codecov.io/gh/twcclegg/libphonenumber-csharp/branch/master/graph/badge.svg)](https://codecov.io/gh/twcclegg/libphonenumber-csharp) +[![NuGet](https://img.shields.io/nuget/dt/libphonenumber-csharp.svg)](https://www.nuget.org/packages/libphonenumber-csharp/) + # Overview diff --git a/csharp/PhoneNumbers/PhoneNumberUtil.cs b/csharp/PhoneNumbers/PhoneNumberUtil.cs index a805cbffa..b206bb2d9 100644 --- a/csharp/PhoneNumbers/PhoneNumberUtil.cs +++ b/csharp/PhoneNumbers/PhoneNumberUtil.cs @@ -1341,53 +1341,98 @@ public string FormatNumberForMobileDialing(PhoneNumber number, string regionCall var countryCallingCode = number.CountryCode; if (!HasValidCountryCallingCode(countryCallingCode)) { - return number.HasRawInput ? number.RawInput : ""; + return number.RawInput ?? ""; } - string formattedNumber; + var formattedNumber = ""; // Clear the extension, as that part cannot normally be dialed together with the main number. var numberNoExt = new PhoneNumber.Builder().MergeFrom(number).ClearExtension().Build(); - var numberType = GetNumberType(numberNoExt); var regionCode = GetRegionCodeForCountryCode(countryCallingCode); - if (regionCode.Equals("CO") && regionCallingFrom.Equals("CO")) - { - formattedNumber = numberType == PhoneNumberType.FIXED_LINE - ? FormatNationalNumberWithCarrierCode(numberNoExt, COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX) - : Format(numberNoExt, PhoneNumberFormat.NATIONAL); - } - else if (regionCode.Equals("PE") && regionCallingFrom.Equals("PE")) - { - // In Peru, numbers cannot be dialled using E164 format from a mobile phone for Movistar. - // Instead they must be dialled in national format. - formattedNumber = Format(numberNoExt, PhoneNumberFormat.NATIONAL); - } - else if (regionCode.Equals("BR") && regionCallingFrom.Equals("BR") && - ((numberType == PhoneNumberType.FIXED_LINE) || (numberType == PhoneNumberType.MOBILE) || - (numberType == PhoneNumberType.FIXED_LINE_OR_MOBILE))) - { - // Historically, we set this to an empty string when parsing with raw input if none was - // found in the input string. However, this doesn't result in a number we can dial. For this - // reason, we treat the empty string the same as if it isn't set at all. - formattedNumber = numberNoExt.PreferredDomesticCarrierCode.Length > 0 - ? FormatNationalNumberWithPreferredCarrierCode(numberNoExt, "") - // Brazilian fixed line and mobile numbers need to be dialed with a carrier code when - // called within Brazil. Without that, most of the carriers won't connect the call. - // Because of that, we return an empty string here. - : ""; + var numberType = GetNumberType(numberNoExt); + var isValidNumber = numberType != PhoneNumberType.UNKNOWN; + if (regionCallingFrom == regionCode) + { + var isFixedLineOrMobile = + numberType == PhoneNumberType.FIXED_LINE || numberType == PhoneNumberType.MOBILE + || numberType == PhoneNumberType.FIXED_LINE_OR_MOBILE; + // Carrier codes may be needed in some countries. We handle this here. + if (regionCode == "CO" && numberType == PhoneNumberType.FIXED_LINE) + { + formattedNumber = + FormatNationalNumberWithCarrierCode(numberNoExt, COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX); + } + else if (regionCode == "BR" && isFixedLineOrMobile) + { + // Historically, we set this to an empty string when parsing with raw input if none was + // found in the input string. However, this doesn't result in a number we can dial. For this + // reason, we treat the empty string the same as if it isn't set at all. + formattedNumber = numberNoExt.PreferredDomesticCarrierCode.Length > 0 + ? formattedNumber = FormatNationalNumberWithPreferredCarrierCode(numberNoExt, "") + // Brazilian fixed line and mobile numbers need to be dialed with a carrier code when + // called within Brazil. Without that, most of the carriers won't connect the call. + // Because of that, we return an empty string here. + : ""; + } + else if + (countryCallingCode == NANPA_COUNTRY_CODE) + { + // For NANPA countries, we output international format for numbers that can be dialed + // internationally, since that always works, except for numbers which might potentially be + // short numbers, which are always dialled in national format. + var regionMetadata = GetMetadataForRegion(regionCallingFrom); + if (CanBeInternationallyDialled(numberNoExt) + && TestNumberLength(GetNationalSignificantNumber(numberNoExt), regionMetadata) + != ValidationResult.TOO_SHORT) + { + formattedNumber = Format(numberNoExt, PhoneNumberFormat.INTERNATIONAL); + } + else + { + formattedNumber = Format(numberNoExt, PhoneNumberFormat.NATIONAL); + } + } + else + { + // For non-geographical countries, and Mexican, Chilean, and Uzbek fixed line and mobile + // numbers, we output international format for numbers that can be dialed internationally as + // that always works. + if (regionCode == REGION_CODE_FOR_NON_GEO_ENTITY + // MX fixed line and mobile numbers should always be formatted in international format, + // even when dialed within MX. For national format to work, a carrier code needs to be + // used, and the correct carrier code depends on if the caller and callee are from the + // same local area. It is trickier to get that to work correctly than using + // international format, which is tested to work fine on all carriers. + // CL fixed line numbers need the national prefix when dialing in the national format, + // but don't have it when used for display. The reverse is true for mobile numbers. As + // a result, we output them in the international format to make it work. + // UZ mobile and fixed-line numbers have to be formatted in international format or + // prefixed with special codes like 03, 04 (for fixed-line) and 05 (for mobile) for + // dialling successfully from mobile devices. As we do not have complete information on + // special codes and to be consistent with formatting across all phone types we return + // the number in international format here. + || (regionCode == "MX" || regionCode == "CL" + || regionCode == "UZ" && isFixedLineOrMobile) + && CanBeInternationallyDialled(numberNoExt)) + { + formattedNumber = Format(numberNoExt, PhoneNumberFormat.INTERNATIONAL); + } + else + { + formattedNumber = Format(numberNoExt, PhoneNumberFormat.NATIONAL); + } + } } - else if (CanBeInternationallyDialled(numberNoExt)) + else if (isValidNumber && CanBeInternationallyDialled(numberNoExt)) { - return withFormatting ? Format(numberNoExt, PhoneNumberFormat.INTERNATIONAL) + // We assume that short numbers are not diallable from outside their region, so if a number + // is not a valid regular length phone number, we treat it as if it cannot be internationally + // dialled. + return withFormatting + ? Format(numberNoExt, PhoneNumberFormat.INTERNATIONAL) : Format(numberNoExt, PhoneNumberFormat.E164); } - else - { - formattedNumber = (regionCallingFrom == regionCode) - ? Format(numberNoExt, PhoneNumberFormat.NATIONAL) : ""; - } return withFormatting ? formattedNumber - : NormalizeHelper(formattedNumber, DiallableCharMappings, - true /* remove non matches */); + : NormalizeDiallableCharsOnly(formattedNumber); } /// diff --git a/resources/PhoneNumberMetaData.xml b/resources/PhoneNumberMetaData.xml index 140894b29..6576561c1 100644 --- a/resources/PhoneNumberMetaData.xml +++ b/resources/PhoneNumberMetaData.xml @@ -4248,6 +4248,7 @@ 8[015]| 9[0-47-9] )| + 321| 610 $1 @@ -11627,17 +11628,16 @@ - + - + 1 $1 $2 $3 - + [2-9] $1 $2 $3 @@ -15588,7 +15588,10 @@ $1 $2 - [235-79] + + [235-79]| + 88 + $1 $2 $3 @@ -15598,11 +15601,11 @@ + 8\d{9}| (?: - [235-7]\d| + [235-8]\d| 99 - )\d{7}| - 800\d{6,7} + )\d{7} @@ -15663,6 +15666,7 @@ 700123456 + 8801\d{5}| (?: 2(?: 0[0-35]| @@ -15676,7 +15680,7 @@ [07]\d| 55 )| - 99[69] + 99[05-9] )\d{6} @@ -16229,8 +16233,14 @@ - 1000000000 - 1[0-26-9]\d{7,8} + 1020000000 + + 10[01]\d{6}| + 1(?: + 0[2-9]| + [126-9]\d + )\d{6,7} + @@ -19361,7 +19371,7 @@ [3467]| - 9[14-9] + 9[13-9] $1-$2 @@ -19411,7 +19421,7 @@ 46[46]\d{4}| (?: 7[2-9]| - 9[14-9] + 9[13-9] )\d{5} @@ -23764,7 +23774,7 @@ [369]| - 8[1-8] + 8[1-9] $1 $2 @@ -23802,6 +23812,7 @@ 81234567 + 89[01]\d{5}| (?: 8[1-8]| 9[0-8] @@ -27610,7 +27621,8 @@ + unable to receive SMS so for now supporting in both Mobile and Fixed-line. Range 289 is + added based on user report. --> 2101234567 @@ -27624,7 +27636,7 @@ 5[124-9]| 6[0-39]| 7[0-7]| - 8[2-7]| + 8[2-79]| 9[0-4679] )\d{7} diff --git a/resources/PhoneNumberMetaDataForTesting.xml b/resources/PhoneNumberMetaDataForTesting.xml index ded9b9075..2936bdd23 100644 --- a/resources/PhoneNumberMetaDataForTesting.xml +++ b/resources/PhoneNumberMetaDataForTesting.xml @@ -561,19 +561,6 @@ - - - - - 30\d{7} - - - 30\d{7} - - 301234567 - - - diff --git a/resources/ShortNumberMetadata.xml b/resources/ShortNumberMetadata.xml index 11004ac3e..2899ce769 100644 --- a/resources/ShortNumberMetadata.xml +++ b/resources/ShortNumberMetadata.xml @@ -1648,7 +1648,7 @@ - [124-69]\d{2,5} + [1-69]\d{2,5} @@ -1697,6 +1697,10 @@ )| 85959? )| + (?: + 32| + 91 + )1| 4(?: 0404?| 57| @@ -1707,7 +1711,6 @@ 0\d{4}| 10000 )| - 911| (?: 133| 411 @@ -1740,7 +1743,8 @@ 102 102| - 273\d\d + 273\d\d| + 321 @@ -1797,10 +1801,11 @@ - - 4820 + + 321 285\d{2,3}| + 321| 40404| (?: 27[38]\d| @@ -6175,23 +6180,25 @@ - 1\d{2,4} + [12]\d{2,4} 100 1(?: - 0(?: - [0-2]| - 400 - )| + 0[0-2]| 1(?: [013-9]\d| 2 )| [2-9]\d\d - ) + )| + 2407| + (?: + 104| + 27 + )00 diff --git a/resources/carrier/en/65.txt b/resources/carrier/en/65.txt index a8fbb7628..9e22c1e34 100644 --- a/resources/carrier/en/65.txt +++ b/resources/carrier/en/65.txt @@ -283,6 +283,17 @@ 658869|SingTel 658876|SingTel 658877|M1 +658900|TPG +658901|TPG +658902|TPG +658903|TPG +658904|TPG +658905|SingTel +658906|SingTel +658907|SingTel +658908|SingTel +658909|SingTel +65891|TPG 659001|StarHub 659002|StarHub 659003|StarHub diff --git a/resources/carrier/en/996.txt b/resources/carrier/en/996.txt index 6f8bbbd80..836b653da 100644 --- a/resources/carrier/en/996.txt +++ b/resources/carrier/en/996.txt @@ -25,4 +25,10 @@ 99670|Nur Telecom 99675|ALFA Telecom 99677|Sky mobile -9969|Sky mobile +99688|ALFA Telecom +996990|ALFA Telecom +996995|ALFA Telecom +996996|Sky mobile +996997|ALFA Telecom +996998|ALFA Telecom +996999|Sky mobile