Skip to content

Use uint.TryFormat to fix TODO in IPAddressParser #117300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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) ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to switch to TryFormat in the local functions FormatByte and AppendHex in this file too?

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