From 049da931ee9058057a53da1beddb025adef3c03b Mon Sep 17 00:00:00 2001 From: Trevor Pilley Date: Thu, 21 Nov 2024 10:05:11 +0000 Subject: [PATCH] Fix --- .../Formatters/PhoneNumberFormatter.cs | 15 ++------------- .../FrameworkExtensions/SpanExtensions.cs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 src/PhoneNumbers/FrameworkExtensions/SpanExtensions.cs diff --git a/src/PhoneNumbers/Formatters/PhoneNumberFormatter.cs b/src/PhoneNumbers/Formatters/PhoneNumberFormatter.cs index c98c56662..b9ee9f42c 100644 --- a/src/PhoneNumbers/Formatters/PhoneNumberFormatter.cs +++ b/src/PhoneNumbers/Formatters/PhoneNumberFormatter.cs @@ -50,20 +50,9 @@ protected static string FormatInternational( Span ar = stackalloc char[arSize]; var arPos = 0; - if (outputPrefix is not null) - { - foreach (var c in outputPrefix) - { - ar[arPos++] = c; - } - } - + ar.Append(ref arPos, outputPrefix); ar[arPos++] = Chars.Plus; - - foreach (var c in phoneNumber.Country.CallingCode) - { - ar[arPos++] = c; - } + ar.Append(ref arPos, phoneNumber.Country.CallingCode); if (charBetweenCallingCodeAndNsn != Chars.Null) { diff --git a/src/PhoneNumbers/FrameworkExtensions/SpanExtensions.cs b/src/PhoneNumbers/FrameworkExtensions/SpanExtensions.cs new file mode 100644 index 000000000..49d9b015d --- /dev/null +++ b/src/PhoneNumbers/FrameworkExtensions/SpanExtensions.cs @@ -0,0 +1,18 @@ +namespace PhoneNumbers; + +/// +/// A class containing extension methods for the class. +/// +internal static class SpanExtensions +{ + internal static void Append(this ref Span ar, ref int arPos, IEnumerable? input) + { + if (input is not null) + { + foreach (var value in input) + { + ar[arPos++] = value; + } + } + } +}