Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorPilley committed Nov 21, 2024
1 parent d28453f commit 3fa8493
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/PhoneNumbers/Formatters/PhoneNumberFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ protected static string FormatInternational(
bool wrapNdc = false,
char nonDigitSubstitute = Chars.Null)
{
var shouldWrapNdc = wrapNdc && phoneNumber.Country.HasNationalDestinationCodes;
var shouldWrapNdc = wrapNdc && phoneNumber.HasNationalDestinationCode;
var nsnMask = phoneNumber.Country.FormatProvider.GetFormat(phoneNumber, international: true);

var arSize =
(outputPrefix?.Length ?? 0)
+ 1 // add one for the + appended to the calling code
+ phoneNumber.Country.CallingCode.Length
+ (charBetweenCallingCodeAndNsn != Chars.Null ? 1 : 0)
+ (wrapNdc ? 2 : 0) // add 2 for the parenthesis if wrapping the NDC
+ (shouldWrapNdc ? 2 : 0) // add 2 for the parenthesis if wrapping the NDC
+ nsnMask.Length;

Span<char> ar = stackalloc char[arSize];
Expand Down
6 changes: 6 additions & 0 deletions test/PhoneNumbers.Tests/PhoneNumber_ToString_Africa_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class PhoneNumber_ToString_Africa_Tests
[InlineData("+20235344239", "E.123", "+20 2 3534 4239")]
[InlineData("+20235344239", "N", "02 3534 4239")]
[InlineData("+20235344239", "RFC3966", "tel:+20-2-3534-4239")]
[InlineData("+20235344239", "TAPI", "+20 (2) 3534 4239")]
[InlineData("+20235344239", "U", "0235344239")]
public void Egypt_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));
Expand All @@ -17,6 +18,8 @@ public void Egypt_Numbers(string input, string format, string expected) =>
[InlineData("+254703042000", "N", "0703 042000")]
[InlineData("+25420424200", "RFC3966", "tel:+254-20-424200")]
[InlineData("+254703042000", "RFC3966", "tel:+254-703-042000")]
[InlineData("+25420424200", "TAPI", "+254 (20) 424200")]
[InlineData("+254703042000", "TAPI", "+254 (703) 042000")]
[InlineData("+25420424200", "U", "020424200")]
[InlineData("+254703042000", "U", "0703042000")]
public void Kenya_Numbers(string input, string format, string expected) =>
Expand All @@ -26,6 +29,7 @@ public void Kenya_Numbers(string input, string format, string expected) =>
[InlineData("+2342094617000", "E.123", "+234 209 461 7000")]
[InlineData("+2342094617000", "N", "(0209) 461 7000")]
[InlineData("+2342094617000", "RFC3966", "tel:+234-209-461-7000")]
[InlineData("+2342094617000", "TAPI", "+234 (209) 461 7000")]
[InlineData("+2342094617000", "U", "02094617000")]
public void Nigeria_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));
Expand All @@ -34,6 +38,7 @@ public void Nigeria_Numbers(string input, string format, string expected) =>
[InlineData("+27215616800", "E.123", "+27 21 561 6800")]
[InlineData("+27215616800", "N", "021 561 6800")]
[InlineData("+27215616800", "RFC3966", "tel:+27-21-561-6800")]
[InlineData("+27215616800", "TAPI", "+27 (21) 561 6800")]
[InlineData("+27215616800", "U", "0215616800")]
public void SouthAfrica_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));
Expand All @@ -42,6 +47,7 @@ public void SouthAfrica_Numbers(string input, string format, string expected) =>
[InlineData("+255222199760", "E.123", "+255 22 219 9760")]
[InlineData("+255222199760", "N", "022 219 9760")]
[InlineData("+255222199760", "RFC3966", "tel:+255-22-219-9760")]
[InlineData("+255222199760", "TAPI", "+255 (22) 219 9760")]
[InlineData("+255222199760", "U", "0222199760")]
public void Tanzania_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));
Expand Down
5 changes: 5 additions & 0 deletions test/PhoneNumbers.Tests/PhoneNumber_ToString_Asia_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class PhoneNumber_ToString_Asia_Tests
[InlineData("+85229616333", "E.123", "+852 2961 6333")]
[InlineData("+85229616333", "N", "2961 6333")]
[InlineData("+85229616333", "RFC3966", "tel:+852-2961-6333")]
[InlineData("+85229616333", "TAPI", "+852 2961 6333")]
[InlineData("+85229616333", "U", "29616333")]
public void HongKong_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));
Expand All @@ -14,6 +15,7 @@ public void HongKong_Numbers(string input, string format, string expected) =>
[InlineData("+85328000000", "E.123", "+853 2800 0000")]
[InlineData("+85328000000", "N", "2800 0000")]
[InlineData("+85328000000", "RFC3966", "tel:+853-2800-0000")]
[InlineData("+85328000000", "TAPI", "+853 2800 0000")]
[InlineData("+85328000000", "U", "28000000")]
public void Macau_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));
Expand All @@ -22,6 +24,7 @@ public void Macau_Numbers(string input, string format, string expected) =>
[InlineData("+6563773800", "E.123", "+65 6377 3800")]
[InlineData("+6563773800", "N", "6377 3800")]
[InlineData("+6563773800", "RFC3966", "tel:+65-6377-3800")]
[InlineData("+6563773800", "TAPI", "+65 6377 3800")]
[InlineData("+6563773800", "U", "63773800")]
public void Singapore_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));
Expand All @@ -33,6 +36,8 @@ public void Singapore_Numbers(string input, string format, string expected) =>
[InlineData("+903122947200", "N", "(0312) 294 72 00")]
[InlineData("+904441234", "RFC3966", "tel:+90-444-12-34")]
[InlineData("+903122947200", "RFC3966", "tel:+90-312-294-72-00")]
[InlineData("+904441234", "TAPI", "+90 444 12 34")]
[InlineData("+903122947200", "TAPI", "+90 (312) 294 72 00")]
[InlineData("+904441234", "U", "04441234")]
[InlineData("+903122947200", "U", "03122947200")]
public void Turkey_Numbers(string input, string format, string expected) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class PhoneNumber_ToString_SouthAmerica_Tests
[InlineData("+5541732649808", "N", "(041) 73264-9808")]
[InlineData("+556123122026", "RFC3966", "tel:+55-61-2312-2026")]
[InlineData("+5541732649808", "RFC3966", "tel:+55-41-73264-9808")]
[InlineData("+556123122026", "TAPI", "+55 (61) 2312-2026")]
[InlineData("+5541732649808", "TAPI", "+55 (41) 73264-9808")]
[InlineData("+556123122026", "U", "06123122026")]
[InlineData("+5541732649808", "U", "041732649808")]
public void Brazil_Numbers(string input, string format, string expected) =>
Expand All @@ -18,6 +20,7 @@ public void Brazil_Numbers(string input, string format, string expected) =>
[InlineData("+576013198300", "E.123", "+57 601 3198300")]
[InlineData("+576013198300", "N", "601 3198300")]
[InlineData("+576013198300", "RFC3966", "tel:+57-601-3198300")]
[InlineData("+576013198300", "TAPI", "+57 (601) 3198300")]
[InlineData("+576013198300", "U", "6013198300")]
public void Colombia_Numbers(string input, string format, string expected) =>
Assert.Equal(expected, PhoneNumber.Parse(input).ToString(format));
Expand Down

0 comments on commit 3fa8493

Please sign in to comment.