From 3fac2c5c98015bd591dbf358c6b43c3aa35f577e Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Tue, 1 Jun 2021 10:41:40 -0500 Subject: [PATCH] Always use preferred intl prefix if present --- .../PhoneNumbers.Test/TestBuildMetadataFromXml.cs | 4 ++-- csharp/PhoneNumbers.Test/TestPhoneNumberUtil.cs | 3 +++ csharp/PhoneNumbers/PhoneNumberUtil.cs | 15 ++++++++------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/csharp/PhoneNumbers.Test/TestBuildMetadataFromXml.cs b/csharp/PhoneNumbers.Test/TestBuildMetadataFromXml.cs index 61e6016c9..3b029a112 100644 --- a/csharp/PhoneNumbers.Test/TestBuildMetadataFromXml.cs +++ b/csharp/PhoneNumbers.Test/TestBuildMetadataFromXml.cs @@ -92,7 +92,7 @@ public void TestLoadTerritoryTagMetadata() { var xmlInput = "" + @@ -103,7 +103,7 @@ public void TestLoadTerritoryTagMetadata() Assert.Equal(33, phoneMetadata.CountryCode); Assert.Equal("2", phoneMetadata.LeadingDigits); Assert.Equal("00", phoneMetadata.InternationalPrefix); - Assert.Equal("0011", phoneMetadata.PreferredInternationalPrefix); + Assert.Equal("00~11", phoneMetadata.PreferredInternationalPrefix); Assert.Equal("0", phoneMetadata.NationalPrefixForParsing); Assert.Equal("9$1", phoneMetadata.NationalPrefixTransformRule); Assert.Equal("0", phoneMetadata.NationalPrefix); diff --git a/csharp/PhoneNumbers.Test/TestPhoneNumberUtil.cs b/csharp/PhoneNumbers.Test/TestPhoneNumberUtil.cs index e67e6c6a5..28641b1c7 100644 --- a/csharp/PhoneNumbers.Test/TestPhoneNumberUtil.cs +++ b/csharp/PhoneNumbers.Test/TestPhoneNumberUtil.cs @@ -609,6 +609,9 @@ public void TestFormatOutOfCountryWithPreferredIntlPrefix() // are accepted as possible international prefixes in our test metadta.) Assert.Equal("0011 39 02 3661 8300", phoneUtil.FormatOutOfCountryCallingNumber(ITNumber, RegionCode.AU)); + + // Testing preferred international prefixes with ~ are supported (designates waiting). + Assert.Equal("8~10 39 02 3661 8300", phoneUtil.FormatOutOfCountryCallingNumber(ITNumber, RegionCode.UZ)); } [Fact] diff --git a/csharp/PhoneNumbers/PhoneNumberUtil.cs b/csharp/PhoneNumbers/PhoneNumberUtil.cs index b6a98b519..8e5b004a2 100644 --- a/csharp/PhoneNumbers/PhoneNumberUtil.cs +++ b/csharp/PhoneNumbers/PhoneNumberUtil.cs @@ -1462,18 +1462,19 @@ public string FormatOutOfCountryCallingNumber(PhoneNumber number, string regionC var metadataForRegionCallingFrom = GetMetadataForRegion(regionCallingFrom); var internationalPrefix = metadataForRegionCallingFrom.InternationalPrefix; - // For regions that have multiple international prefixes, the international format of the - // number is returned, unless there is a preferred international prefix. + // In general, if there is a preferred international prefix, use that. Otherwise, for regions + // that have multiple international prefixes, the international format of the number is + // returned since we would not know which one to use. var internationalPrefixForFormatting = ""; - if (UniqueInternationalPrefix.IsMatchAll(internationalPrefix)) - { - internationalPrefixForFormatting = internationalPrefix; - } - else if (metadataForRegionCallingFrom.HasPreferredInternationalPrefix) + if (metadataForRegionCallingFrom.HasPreferredInternationalPrefix) { internationalPrefixForFormatting = metadataForRegionCallingFrom.PreferredInternationalPrefix; } + else if (UniqueInternationalPrefix.IsMatchAll(internationalPrefix)) + { + internationalPrefixForFormatting = internationalPrefix; + } var regionCode = GetRegionCodeForCountryCode(countryCallingCode); var metadataForRegion =