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; + } + } + } +}