-
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
Provide a new NumberStyles option to stop parsing after the first invalid character #87171
Comments
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsSummaryUp through .NET 7 we have the provided UTF-16 based parsing functionality in the form of various We have also provided In .NET 8, we have again expanded the core support to provide It is proposed that we then provide a new API Proposalnamespace System.Globalization
{
[Flags]
public partial enum NumberStyles
{
AllowTrailingInvalidCharacters = 0x800
}
}
namespace System.Numerics
{
public static virtual bool TryParse([NotNullWhen(true)] string? s, NumberStyles style, IFormatProvider? provider, [MaybeNullWhen(false)] out TSelf result, out int charsConsumed);
public static virtual bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, [MaybeNullWhen(false)] out TSelf result, out int charsConsumed);
public static virtual bool TryParse(ReadOnlySpan<byte> s, NumberStyles style, IFormatProvider? provider, [MaybeNullWhen(false)] out TSelf result, out int bytesConsumed);
} We would then remove Given that the current requirement is that unsupported/unrecognized Alternative NamesThere are several potential other names that we could choose from, ranging in verbosity and total clarity on what's being allowed:
|
namespace System.Globalization
{
[Flags]
public partial enum NumberStyles
{
AllowTrailingInvalidCharacters = 0x800
}
}
namespace System.Numerics
{
public interface INumberBase<TSelf>
{
public static virtual bool TryParse([NotNullWhen(true)] string? s,
NumberStyles style,
IFormatProvider? provider,
[MaybeNullWhen(false)] out TSelf result,
out int charsConsumed);
public static virtual bool TryParse(ReadOnlySpan<char> s,
NumberStyles style,
IFormatProvider? provider,
[MaybeNullWhen(false)] out TSelf result,
out int charsConsumed);
public static virtual bool TryParse(ReadOnlySpan<byte> s,
NumberStyles style,
IFormatProvider? provider,
[MaybeNullWhen(false)] out TSelf result,
out int bytesConsumed);
}
} |
Not going to have time to land this one for .NET 8, we'll be able to take a PR for it anytime after main opens for .NET 9 changes next month. |
Summary
Up through .NET 7 we have the provided UTF-16 based parsing functionality in the form of various
Parse
andTryParse
APIs exposed as static methods on the respective types. In .NET 7 this support was expanded via the newIParsable
andISpanParsable
interface and some additional numeric specific APIs onINumberBase
that take aNumberStyles
parameter.We have also provided
Utf8Parser
which gives access to UTF-8 based parsing functionality for a limited subset of scenarios and which has a core differing behavior in that rather than treating the input as invalid after the first invalid character, it is instead treated as "end of input".In .NET 8, we have again expanded the core support to provide
IUtf8SpanParsable
and some additional methods onINumberBase
which provides parity to the UTF-16 scenarios. However, there still remains a divergence in thatUTF8Parser
still supports treating the first invalid character specially and there is no equivalent functionality forUTF-16
or forUTF-8
when using the new interfaces/APIs.It is proposed that we then provide a new
NumberStyles
option that allows access to this functionality forUTF-16
orUTF-8
scenarios via the new interfaces/types. This would makeUtf8Parser
andUtf8Formatter
both "functionally obsolete" even if we do not actually mark them as such.API Proposal
We would then remove
api-approved
from proposals such as #73842 given that it provides no new functionality. -- Even if this proposal is rejected, there needs to be a determination if APIs are exposed onUtf8Formatter
given that it is effectively just a mirror forUtf8Parser
and doesn't actually provide access to any functionality that can not be done usingIUtf8SpanFormattable
Given that the current requirement is that unsupported/unrecognized
NumberStyles
throw anArgumentException
, we can reasonably provide a default implementation since the only validbytesConsumed
/charsConsumed
will be "everything" or "nothing". The implementor would be expected to override the DIM if and when they add support forAllowTrailingInvalidCharacters
Alternative Names
There are several potential other names that we could choose from, ranging in verbosity and total clarity on what's being allowed:
The text was updated successfully, but these errors were encountered: