You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[API Proposal]: Consider support for IParsable<Uri> on System.Uri #92285
api-approvedAPI was approved in API review, it can be implementedarea-System.Netin-prThere is an active PR which will close this issue when it is merged
Edited by @MihaZupan on Sep 22nd to reflect the latest API proposal shape.
Background and motivation
Since it was introduced, IParsable is implemented by most framework types to support converting from a string to the type. In Blazor we support deserializing IParsable types when processing form data, and this is one of the types we need to special case support for because they don't implement IParsable
API Proposal
namespaceSystem;publicclassUri:IParsable<Uri>// New interface with an explicit implementation{staticboolIParsable<Uri>.TryParse(string?s,IFormatProvider?provider,outUriresult);staticUriIParsable<Uri>.Parse(strings,IFormatProvider?provider);}
The IFormatProvider will be ignored.
The implementation will delegate to new Uri(s)/Uri.TryCreate(s).
The options used will be UriKind = UriKind.RelativeOrAbsolute, AllowImplicitFilePaths = false.
API Usage
Allows Uri to use the same generic paths in existing implementations.
Same idea as the example given for IParsable<String>:
ISpanParsable and IUtf8SpanParsable were mentioned as potential additions, but were currently rejected on the grounds that they would not provide any performance advantages over IParsable.
If the input is a valid Uri, we will be forced to allocate the original input string due to the existing API surface (OriginalString).
As such, being able to parse from a span would only benefit cases where it's common for the input not to be valid.
But since we're saying that the methods should work in terms of UriKind.RelativeOrAbsolute, that means almost all inputs will be valid (with rare exceptions like a scheme with over 1024 characters, an invalid hostname, ...).
In other words, having the overload that takes a span would never help perf-wise because of the existing API shape, at which point there's no real reason to include it.
IUtf8SpanParsable<Uri>
What would be the use cases for this? Also, it runs into the same limitations as ISpanParsable.
What would be the use cases for this? Also, it runs into the same limitations as ISpanParsable.
Can avoid an additional transcoding step. Allows it to support parsing from generic contexts. Etc
It's basically all the same stuff everywhere. There are potential perf and usability opportunities. If those don't exist for the type, then we don't need to expose the additional interfaces. But, exposing them can often simplify things.
Not if it has to create a string, anyway, in support of Uri.OriginalString.
That depends on how it's implemented.
If you have a UTF8 span and only expose a string parse API, then for something like TryParse you have to first transcode the UTF8 span to a UTF16 string (also incurring an allocation) and then validate it's correct.
If you expose TryParse(ROSpan<byte>) then you can perform the transcoding and validation simultaneously or separately (doing validation first) and only allocate the backing string on success, which is effectively a memcpy operation at that point.
This is why Miha stated "being able to parse from a span would only benefit cases where it's common for the input not to be valid". Neither of us believe that to be common.
Looks good as proposed. Depends on #59099 being implemented first.
When discussing if the TryParse and Parse should be implicit or explicit, we feel that having both TryCreate and TryParse is sub-optimal.
namespaceSystem;publicclassUri:IParsable<Uri>// New interface with an explicit implementation{staticboolIParsable<Uri>.TryParse(string?s,IFormatProvider?provider,outUriresult)=>TryCreate(uriString,RelativeOrAbsolute,NoImplicitFilePaths,outresult);staticUriIParsable<Uri>.Parse(strings,IFormatProvider?provider)=>newUri(s,RelativeOrAbsolute,NoImplicitFilePaths);}
@javiercn@bartonjs
I made first draft of proposed changed, please review - #93479.
I'm not sure how to properly handle nullability in TryParse method, so decided to just create new instance (it looks little bit better than ! usage).
And as I see NoImplicitFilePaths is not able to be implemented right now.
api-approvedAPI was approved in API review, it can be implementedarea-System.Netin-prThere is an active PR which will close this issue when it is merged
Activity
Sergio0694 commentedon Sep 19, 2023
Shouldn't we also suggest it implementing
ISpanParsable<Uri>while at it?MihaZupan commentedon Sep 19, 2023
This seems reasonable to me, but the interface implementation should be explicit (no reason to have both
Uri.TryCreateandUri.TryParseexposed).We should consider having the behavior be such that it uses
AllowImplicitFilePaths = falseinstead (once #59099 is implemented).Note that
Urihas astring OriginalStringproperty on it, so we'd have to materialize the string either way.neon-sunset commentedon Sep 19, 2023
...and
IUtf8SpanParsable<Uri>too.MihaZupan commentedon Sep 21, 2023
If the input is a valid Uri, we will be forced to allocate the original input string due to the existing API surface (
OriginalString).As such, being able to parse from a span would only benefit cases where it's common for the input not to be valid.
But since we're saying that the methods should work in terms of
UriKind.RelativeOrAbsolute, that means almost all inputs will be valid (with rare exceptions like a scheme with over 1024 characters, an invalid hostname, ...).In other words, having the overload that takes a span would never help perf-wise because of the existing API shape, at which point there's no real reason to include it.
What would be the use cases for this? Also, it runs into the same limitations as
ISpanParsable.MihaZupan commentedon Sep 22, 2023
Marking this one as ready for review with only explicit implementations of
IParsable<Uri>.tannergooding commentedon Sep 22, 2023
Can avoid an additional transcoding step. Allows it to support parsing from generic contexts. Etc
It's basically all the same stuff everywhere. There are potential perf and usability opportunities. If those don't exist for the type, then we don't need to expose the additional interfaces. But, exposing them can often simplify things.
stephentoub commentedon Sep 22, 2023
Not if it has to create a string, anyway, in support of Uri.OriginalString.
tannergooding commentedon Sep 22, 2023
That depends on how it's implemented.
If you have a UTF8 span and only expose a
stringparse API, then for something likeTryParseyou have to first transcode the UTF8 span to a UTF16 string (also incurring an allocation) and then validate it's correct.If you expose
TryParse(ROSpan<byte>)then you can perform the transcoding and validation simultaneously or separately (doing validation first) and only allocate the backing string on success, which is effectively a memcpy operation at that point.stephentoub commentedon Sep 22, 2023
This is why Miha stated "being able to parse from a span would only benefit cases where it's common for the input not to be valid". Neither of us believe that to be common.
bartonjs commentedon Sep 26, 2023
Video
Looks good as proposed. Depends on #59099 being implemented first.
When discussing if the TryParse and Parse should be implicit or explicit, we feel that having both TryCreate and TryParse is sub-optimal.
KonH commentedon Oct 13, 2023
@javiercn @bartonjs
I made first draft of proposed changed, please review - #93479.
I'm not sure how to properly handle nullability in TryParse method, so decided to just create new instance (it looks little bit better than ! usage).
And as I see NoImplicitFilePaths is not able to be implemented right now.