Skip to content

Commit

Permalink
v8.10.18
Browse files Browse the repository at this point in the history
  • Loading branch information
twcclegg committed Sep 16, 2019
1 parent 805f8bc commit f81c0ee
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 79 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
119 changes: 82 additions & 37 deletions csharp/PhoneNumbers/PhoneNumberUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down
46 changes: 29 additions & 17 deletions resources/PhoneNumberMetaData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4248,6 +4248,7 @@
8[015]|
9[0-47-9]
)|
321|
610
</leadingDigits>
<format>$1</format>
Expand Down Expand Up @@ -11627,17 +11628,16 @@

<!-- Hungary (HU) -->
<!-- http://www.itu.int/oth/T0202000061/en -->
<!-- Although the national prefix is necessary for dialling, the preferred format (confirmed
by a Hungarian person and following the yellow pages) is to omit this when formatting.
Yellow pages: http://www.aranyoldalak.hu -->
<!-- As per Wikipedia https://en.wikipedia.org/wiki/Telephone_numbers_in_Hungary format
for both fixed line and mobile number should be preceded with national prefix "06". -->
<territory id="HU" countryCode="36" internationalPrefix="00" nationalPrefix="06"
mobileNumberPortableRegion="true">
<availableFormats>
<numberFormat pattern="(\d)(\d{3})(\d{4})" nationalPrefixFormattingRule="($FG)">
<numberFormat pattern="(\d)(\d{3})(\d{4})" nationalPrefixFormattingRule="($NP $FG)">
<leadingDigits>1</leadingDigits>
<format>$1 $2 $3</format>
</numberFormat>
<numberFormat pattern="(\d{2})(\d{3})(\d{3,4})" nationalPrefixFormattingRule="($FG)">
<numberFormat pattern="(\d{2})(\d{3})(\d{3,4})" nationalPrefixFormattingRule="$NP $FG">
<leadingDigits>[2-9]</leadingDigits>
<format>$1 $2 $3</format>
</numberFormat>
Expand Down Expand Up @@ -15588,7 +15588,10 @@
<format>$1 $2</format>
</numberFormat>
<numberFormat pattern="(\d{3})(\d{3})(\d{3})" nationalPrefixFormattingRule="$NP$FG">
<leadingDigits>[235-79]</leadingDigits>
<leadingDigits>
[235-79]|
88
</leadingDigits>
<format>$1 $2 $3</format>
</numberFormat>
<numberFormat pattern="(\d{3})(\d{3})(\d)(\d{2,3})" nationalPrefixFormattingRule="$NP$FG">
Expand All @@ -15598,11 +15601,11 @@
</availableFormats>
<generalDesc>
<nationalNumberPattern>
8\d{9}|
(?:
[235-7]\d|
[235-8]\d|
99
)\d{7}|
800\d{6,7}
)\d{7}
</nationalNumberPattern>
</generalDesc>
<!-- Extra area codes found on Web Search: 3147. -->
Expand Down Expand Up @@ -15663,6 +15666,7 @@
<possibleLengths national="9"/>
<exampleNumber>700123456</exampleNumber>
<nationalNumberPattern>
8801\d{5}|
(?:
2(?:
0[0-35]|
Expand All @@ -15676,7 +15680,7 @@
[07]\d|
55
)|
99[69]
99[05-9]
)\d{6}
</nationalNumberPattern>
</mobile>
Expand Down Expand Up @@ -16229,8 +16233,14 @@
</fixedLine>
<mobile>
<possibleLengths national="9,10"/>
<exampleNumber>1000000000</exampleNumber>
<nationalNumberPattern>1[0-26-9]\d{7,8}</nationalNumberPattern>
<exampleNumber>1020000000</exampleNumber>
<nationalNumberPattern>
10[01]\d{6}|
1(?:
0[2-9]|
[126-9]\d
)\d{6,7}
</nationalNumberPattern>
</mobile>
<pager>
<possibleLengths national="9,10"/>
Expand Down Expand Up @@ -19361,7 +19371,7 @@
<numberFormat pattern="(\d{3})(\d{4})">
<leadingDigits>
[3467]|
9[14-9]
9[13-9]
</leadingDigits>
<format>$1-$2</format>
</numberFormat>
Expand Down Expand Up @@ -19411,7 +19421,7 @@
46[46]\d{4}|
(?:
7[2-9]|
9[14-9]
9[13-9]
)\d{5}
</nationalNumberPattern>
</mobile>
Expand Down Expand Up @@ -23764,7 +23774,7 @@
<numberFormat pattern="(\d{4})(\d{4})">
<leadingDigits>
[369]|
8[1-8]
8[1-9]
</leadingDigits>
<format>$1 $2</format>
</numberFormat>
Expand Down Expand Up @@ -23802,6 +23812,7 @@
<possibleLengths national="8"/>
<exampleNumber>81234567</exampleNumber>
<nationalNumberPattern>
89[01]\d{5}|
(?:
8[1-8]|
9[0-8]
Expand Down Expand Up @@ -27610,7 +27621,8 @@
</noInternationalDialling>
<!-- While 24 and 28 are the new area codes for Hanoi and Ho Chi Minh respectively, currently
only 24[2-8] and 28[2-7] are in use. Reporter mentioned prefix 866 is Mobile but was
unable to receive SMS so for now supporting in both Mobile and Fixed-line -->
unable to receive SMS so for now supporting in both Mobile and Fixed-line. Range 289 is
added based on user report. -->
<fixedLine>
<possibleLengths national="10"/>
<exampleNumber>2101234567</exampleNumber>
Expand All @@ -27624,7 +27636,7 @@
5[124-9]|
6[0-39]|
7[0-7]|
8[2-7]|
8[2-79]|
9[0-4679]
)\d{7}
</nationalNumberPattern>
Expand Down
13 changes: 0 additions & 13 deletions resources/PhoneNumberMetaDataForTesting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -561,19 +561,6 @@
</fixedLine>
</territory>

<!-- Hungary -->
<!-- This country has special logic in formatNumberForMobileDialing which must be tested. -->
<territory id="HU" countryCode="36" internationalPrefix="00" nationalPrefix="06">
<generalDesc>
<nationalNumberPattern>30\d{7}</nationalNumberPattern>
</generalDesc>
<mobile>
<nationalNumberPattern>30\d{7}</nationalNumberPattern>
<possibleLengths national="9"/>
<exampleNumber>301234567</exampleNumber>
</mobile>
</territory>

<!-- Italy -->
<!-- http://en.wikipedia.org/wiki/%2B39 -->
<territory id="IT" countryCode="39" internationalPrefix="00">
Expand Down
29 changes: 18 additions & 11 deletions resources/ShortNumberMetadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@
<!-- http://www.anatel.gov.br/Portal/exibirPortalPaginaEspecial.do?codItemCanal=746&codCanal=277 -->
<territory id="BR">
<generalDesc>
<nationalNumberPattern>[124-69]\d{2,5}</nationalNumberPattern>
<nationalNumberPattern>[1-69]\d{2,5}</nationalNumberPattern>
</generalDesc>
<shortCode>
<possibleLengths national="[3-6]"/>
Expand Down Expand Up @@ -1697,6 +1697,10 @@
)|
85959?
)|
(?:
32|
91
)1|
4(?:
0404?|
57|
Expand All @@ -1707,7 +1711,6 @@
0\d{4}|
10000
)|
911|
(?:
133|
411
Expand Down Expand Up @@ -1740,7 +1743,8 @@
<exampleNumber>102</exampleNumber>
<nationalNumberPattern>
102|
273\d\d
273\d\d|
321
</nationalNumberPattern>
</standardRate>
<!-- https://support.twitter.com/articles/20170024 -->
Expand Down Expand Up @@ -1797,10 +1801,11 @@
<!-- Added short codes 28595, 285959 and 4828 after a report that it is used for SMS
services. We can't find range-related information, despite emailing Anatel. -->
<smsServices>
<possibleLengths national="[4-6]"/>
<exampleNumber>4820</exampleNumber>
<possibleLengths national="[3-6]"/>
<exampleNumber>321</exampleNumber>
<nationalNumberPattern>
285\d{2,3}|
321|
40404|
(?:
27[38]\d|
Expand Down Expand Up @@ -6175,23 +6180,25 @@
<!-- http://en.wikipedia.org/wiki/%2B972 -->
<territory id="IL">
<generalDesc>
<nationalNumberPattern>1\d{2,4}</nationalNumberPattern>
<nationalNumberPattern>[12]\d{2,4}</nationalNumberPattern>
</generalDesc>
<shortCode>
<possibleLengths national="[3-5]"/>
<exampleNumber>100</exampleNumber>
<nationalNumberPattern>
1(?:
0(?:
[0-2]|
400
)|
0[0-2]|
1(?:
[013-9]\d|
2
)|
[2-9]\d\d
)
)|
2407|
(?:
104|
27
)00
</nationalNumberPattern>
</shortCode>
<tollFree>
Expand Down
Loading

0 comments on commit f81c0ee

Please sign in to comment.