Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ static int FormatByte(uint number, Span<TChar> addressString)
internal static int FormatIPv6Address<TChar>(ushort[] address, uint scopeId, Span<TChar> destination)
where TChar : unmanaged, IBinaryInteger<TChar>
{
Debug.Assert(typeof(TChar) == typeof(byte) || typeof(TChar) == typeof(char));
int pos = 0;

if (IPv6AddressHelper.ShouldHaveIpv4Embedded(address))
Expand All @@ -220,18 +221,13 @@ internal static int FormatIPv6Address<TChar>(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<TChar> 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<TChar> used = chars.Slice(bytesPos);
used.CopyTo(destination.Slice(pos));
pos += used.Length;
int bytesWritten;
bool formatted = typeof(TChar) == typeof(byte) ?
scopeId.TryFormat(MemoryMarshal.Cast<TChar, byte>(destination).Slice(pos), out bytesWritten) :
scopeId.TryFormat(MemoryMarshal.Cast<TChar, char>(destination).Slice(pos), out bytesWritten);

Debug.Assert(formatted);
pos += bytesWritten;
}

return pos;
Expand Down
Loading