-
Notifications
You must be signed in to change notification settings - Fork 5k
ReadOnlySpan<char> overload of Uri.UnescapeDataString #54828
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
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsBackground and MotivationFor context, this emerged in ASP.NET Core at dotnet/aspnetcore#33840 (comment) Currently, Proposed APInamespace System
{
public class Uri
{
+ ReadOnlySpan<char> UnescapeDataString(ReadOnlySpan<char> stringToUnescape) { ... }
}
} Of course, if the input data contains escaped characters, then the implementation will need to allocate in order to provide a return value. However the existing logic within Usage Examplesvar unescapedValue = Uri.UnescapeDataString(someCharSpanFromAUri);
|
We try to avoid APIs which allocate then return the allocated memory as a One way to address this could be to return a |
Thanks @GrabYourPitchforks. I wasn't aware of the convention to avoid allocating and returning
TBH I don't think the caller would want to supply a buffer given that it's hard to predict the correct size (without an understanding of the decoding rules), and because it leads the caller into a complex world of needing to pool buffers etc.
It's slightly unusual, but I agree that does match the scenario. Perhaps the API would be something like: bool DecodeIfRequired(ReadOnlySpan<char> value, [MaybeNullWhen(false)] out string result) We're not blocked on this for 6.0 (at least, we're not in any way counting on this happening for 6.0!) but is potentially an opportunity for the future. |
wouldn't that more naturally fit with the try pattern? bool TryDecode(ReadOnlySpan<char> value, [MaybeNullWhen(false)] out string result) or even/also (bool Success, string DecodedValue) TryDecode(ReadOnlySpan<char> value) so usage would become: public void DoSomethingWithUri(ReadOnlySpan<char> urlChars)
{
if (Uri.TryDecode(uriChars) is { Success: true, DecodedValue: var decoded })
{
uriChars = decoded;
}
// carry on and use uriChars
} At a later date you could add an overload which has a |
I'll admit I'm not the biggest far of value-tuples in public APIs in general, but still I'm not sure I get how would: if (Uri.TryDecode(uriChars) is { Success: true, DecodedValue: var decoded })
{
} be in any way better than: if (Uri.TryDecode(uriChars, out var decoded)
{
} Isn't it just more verbose and awkward compared to the usual |
I think so, but I also prefer the purity of separate input/output that using a value tuple allows. If they're going to be added I think the out param one is most likely with the valuetuple one a possibility but I wanted to make sure it was there for consideration. |
CC. @karelz, this is another API proposal that impacts |
Tagging subscribers to this area: @dotnet/ncl Issue DetailsBackground and MotivationFor context, this emerged in ASP.NET Core at dotnet/aspnetcore#33840 (comment) Currently, Proposed APInamespace System
{
public class Uri
{
+ ReadOnlySpan<char> UnescapeDataString(ReadOnlySpan<char> stringToUnescape) { ... }
}
} Of course, if the input data contains escaped characters, then the implementation will need to allocate in order to provide a return value. However the existing logic within Usage Examplesvar unescapedValue = Uri.UnescapeDataString(someCharSpanFromAUri);
|
Looks like a need for span overload -- @MihaZupan any thoughts? |
@tannergooding System.Net is the area for System.Uri -- it is called out in area-owners - feel free to move it area-System.Net in future. Thanks! |
Duplicate of #40603 |
Uh oh!
There was an error while loading. Please reload this page.
Background and Motivation
For context, this emerged in ASP.NET Core at dotnet/aspnetcore#33840 (comment)
Currently,
Uri.UnescapeDataString
both accepts and returnsstring
. If the source data you're working with is aReadOnlySpan<char>
, then the caller is forced to allocate astring
. This is especially inefficient if the caller only needs back aReadOnlySpan<char>
, since in most cases this seems like it should be achievable without any allocations (because the original argument could be returned directly if no transformation was required - see below).Proposed API
namespace System { public class Uri { + ReadOnlySpan<char> UnescapeDataString(ReadOnlySpan<char> stringToUnescape) { ... } } }
Of course, if the input data contains escaped characters, then the implementation will need to allocate in order to provide a return value. However the existing logic within
string UnescapeDataString(string)
recognizes the extremely common case where the input string does not contain escaped characters and knows to return the originalstring
directly. Similarly, the newReadOnlySpan<char>
overload could return the original argument value in this case.Usage Examples
The text was updated successfully, but these errors were encountered: