-
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]: Convert.TryFromHexString #78472
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 motivation
Like byte[] myBytes;
try
{
myBytes = Convert.FromHexString(inputString);
}
catch (FormatException ex)
{
throw new InvalidXxx(ex);
} I think It would be much nicer to do if (!Convert.TryFromHexString(inputString, out var myBytes))
throw new InvalidXxx(ex); API Proposalnamespace System;
public class Convert
{
public static bool TryFromHexString(string s, out byte[] bytes);
} API Usageif (!Convert.TryFromHexString(inputString, out var myBytes))
throw new InvalidXxx(ex); Alternative Designs
RisksI don't see any
|
This is a reasonable proposal. We would want to match existing patterns (the version called out in your Alternative Designs section), mostly because |
This issue has been marked |
This issue has been automatically marked |
I've updated my proposal |
namespace System;
public partial class Convert
{
public static bool TryFromHexString(string source, Span<byte> destination, out int bytesWritten);
public static bool TryFromHexString(ReadOnlySpan<char> source, Span<byte> destination, out int bytesWritten);
public static bool TryToHexString(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten);
} |
Having a return value of false mean something additional to "the destination buffer is too small" is a problem. It means an algorithm like: while (true)
{
if (TryFromHexString(..., destination, ...) break;
destination = new byte[destination.Length * 2];
} will OOM if given invalid input. Is the intent you're that this is never supposed to be used that way and a dev is supposed to precompute the required size? |
What's the final decision here? Is one more API review meeting required? |
namespace System;
public partial class Convert
{
public static OperationStatus FromHexString(string source, Span<byte> destination, out int charsConsumed, out int bytesWritten);
public static OperationStatus FromHexString(ReadOnlySpan<char> source, Span<byte> destination, out int charsConsumed, out int bytesWritten);
public static bool TryToHexString(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten);
} |
This isn't going to make .NET 8. There is a pending PR, but its still in draft mode. We'll be able to land it sometime after the repo opens for .NET 9 changes and the PR is no longer marked draft |
Is there anything we can do support you to get the PR past "draft" status? |
We're still locking down on .NET 8 cleanup and other issues. The |
Thanks. I want it because of the possibility to provide a span as destination, this saves me a temporary array allocation and copying, at the non-cost of shorter code. :-) |
Background and motivation
Convert.FromHexString("not-hex");
is throwing aSystem.FormatException
when input string is not a valid hex.Like
Convert.TryFromBase64Chars()
Convert.TryFromBase64String()
it would be nice to haveConvert.TryFromHexString();
when validating user input it's not great to be force to put a whole try/catch block arround for readabilityI think It would be much nicer to do
API Proposal
API Usage
Alternative Designs
TryFromBase64String
returns a Spanbytes
instead with the numberbytesWritten
, so maybe it should be the same, but since the number of bytes are easy to guest with hex string (just divide string length by 2) I'm not sure it's usefulRisks
I don't see any
The text was updated successfully, but these errors were encountered: