-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: Implement IUtf8SpanParsable on IPAddress & IPNetwork #103111
Comments
Tagging subscribers to this area: @dotnet/ncl |
Looks good as proposed, so marked @dotnet/ncl please scream if you disagree with either of these decisions. |
namespace System.Net;
public partial class IPAddress
: ISpanFormattable, ISpanParsable<IPAddress>, IUtf8SpanFormattable
+ , IUtf8SpanParsable<IPAddress>
{
+ static IPAddress IUtf8SpanParsable<IPAddress>.Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
+ public static IPAddress Parse(ReadOnlySpan<byte> utf8Text);
+ static bool IUtf8SpanParsable<IPAddress>.TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider,
+ [NotNullWhen(true)] out IPAddress? result);
+ public static bool TryParse(ReadOnlySpan<byte> utf8Text, [NotNullWhen(true)] out IPAddress? result);
}
public readonly partial struct IPNetwork
: ISpanFormattable, ISpanParsable<IPAddress>, IUtf8SpanFormattable
+ , IUtf8SpanParsable<IPNetwork>
{
static IPNetwork ISpanParsable<IPNetwork>.Parse(ReadOnlySpan<char> s, IFormatProvider? provider);
public static IPNetwork Parse(ReadOnlySpan<char> s);
+ static IPNetwork IUtf8SpanParsable<IPNetwork>.Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
+ public static IPNetwork Parse(ReadOnlySpan<byte> utf8Text);
static bool ISpanParsable<IPNetwork>.TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out IPNetwork result);
public static bool TryParse(ReadOnlySpan<char> s, out IPNetwork result);
+ static bool IUtf8SpanParsable<IPNetwork>.TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider,
+ out IPNetwork result);
+ public static bool TryParse(ReadOnlySpan<byte> utf8Text,
+ [MaybeNullWhen(false)] out IPNetwork? result);
} |
@MihaZupan is it fully covered in PR #102144? |
Yes |
Looks like this feature missed the Platform feature complete deadline yesterday. Moving it to .NET 10. |
Background and motivation
This continues the work in #81500, expanding the existing UTF8 support to IPAddress and IPNetwork. I'm trying to tackle the low-hanging from from the list of expected types.
cc @tannergooding and linking this to the earlier PR #102144 - I can see the issue's gone to System.Net rather than System.Runtime.
API Proposal
The original issue expected
IUtf8SpanParsable
to be implemented implicitly, and I've done this onIPAddress
. I've implemented it explicitly onIPNetwork
though, adding an additional pair ofParse
andTryParse
overloads. This is becauseIPNetwork
also implementsISpanParsable<IPAddress>
explicitly, and I wanted to keep the API surface consistent between the two interface implementations. The original justification for it to implementISpanParsable<IPAddress>
explicitly was that it doesn't support custom formatting (API review results here), and I believe the same rationale applies here.API Usage
Alternative Designs
No response
Risks
At present, all parsing of IPAddresses from
ReadOnlySpan<char>
is handled byIPAddressParser
,IPv4AddressHelper
andIPv6AddressHelper
. This works completely withchar
s and performs minimal allocations. I'd prefer to make these classes generic, but their codebases are also used privately inUri
parsing and bySystem.Net.Quic
. The work required would also replace some of the of references tochar*
withReadOnlySpan<char>
inUri
(and might slightly improve GC performance by eliminating a few cases where we pin strings), but the changes cut across more projects.I could alternatively implement the interface by just calling
Encoding.UTF8.GetChars
on the UTF8 input and passing through toISpanParsable<IPAddress>.Parse
, leaving the underlying IP address parsing as char-only. The longest possible IP address would fit in a 64-character stackalloc'd buffer, so I don't think this approach would necessarily need to allocate - it's just a tradeoff between the wider impact of changingIPAddressParser
and any efficiency loss from a separate transcoding stage.The text was updated successfully, but these errors were encountered: