diff --git a/src/libraries/System.Net.Primitives/src/System/Net/IPAddressParser.cs b/src/libraries/System.Net.Primitives/src/System/Net/IPAddressParser.cs index 2352c4f88397d2..e9485113f75826 100644 --- a/src/libraries/System.Net.Primitives/src/System/Net/IPAddressParser.cs +++ b/src/libraries/System.Net.Primitives/src/System/Net/IPAddressParser.cs @@ -194,6 +194,7 @@ static int FormatByte(uint number, Span addressString) internal static int FormatIPv6Address(ushort[] address, uint scopeId, Span destination) where TChar : unmanaged, IBinaryInteger { + Debug.Assert(typeof(TChar) == typeof(byte) || typeof(TChar) == typeof(char)); int pos = 0; if (IPv6AddressHelper.ShouldHaveIpv4Embedded(address)) @@ -220,18 +221,13 @@ internal static int FormatIPv6Address(ushort[] address, uint scopeId, Spa { destination[pos++] = TChar.CreateTruncating('%'); - // TODO https://github.com/dotnet/runtime/issues/84527: Use UInt32 TryFormat for both char and byte once IUtf8SpanFormattable implementation exists - Span chars = stackalloc TChar[10]; - int bytesPos = 10; - do - { - (scopeId, uint digit) = Math.DivRem(scopeId, 10); - chars[--bytesPos] = TChar.CreateTruncating('0' + digit); - } - while (scopeId != 0); - Span used = chars.Slice(bytesPos); - used.CopyTo(destination.Slice(pos)); - pos += used.Length; + int bytesWritten; + bool formatted = typeof(TChar) == typeof(byte) ? + scopeId.TryFormat(MemoryMarshal.Cast(destination).Slice(pos), out bytesWritten) : + scopeId.TryFormat(MemoryMarshal.Cast(destination).Slice(pos), out bytesWritten); + + Debug.Assert(formatted); + pos += bytesWritten; } return pos;