Repository navigation
System.Text.Json: replace enum integer regex with a lexical scan - #137586
RutulPatel8 wants to merge 1 commit into
Conversation
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @dotnet/area-system-text-json |
|
|
||
| foreach (char c in source) | ||
| { | ||
| if ((uint)(c - '0') > 9) |
There was a problem hiding this comment.
| if ((uint)(c - '0') > 9) | |
| if (!char.IsAsciiDigit(c)) |
There was a problem hiding this comment.
Thanks. System.Text.Json also targets netstandard2.0 and .NET Framework, where char.IsAsciiDigit is unavailable. The unsigned subtraction keeps this check valid across those targets without a conditional compilation branch. I agree with the concern about using a span search for these typically short enum tokens, so I am keeping the simple loop for now.
There was a problem hiding this comment.
I agree with @MihaZupan that we should use char.IsAsciiDigit, even if it means conditional compilation. I don't know whether it changes the generated code here, but it makes the intent clearer and lets us benefit from any future optimizations to that API, including in the JIT.
When we drop the older targets, we can search for #if NET and remove the fallback branches. Keeping the fallback as the only implementation makes it much harder to notice that this code can be simplified later.
We could also use a polyfill, following CharPolyfills.cs, which already has IsAsciiHexDigit. Adding IsAsciiDigit there and including it for the older targets would keep the call site simple.
There was a problem hiding this comment.
Polyfills are the preferred way to solve this problem.
Fixes #136184
EnumConvertercurrently uses a regex to decide whether a quoted enum value is an integer. The check only needs optional sign and ASCII digit recognition, so this replaces it with a small span-based scan and removes the regex setup.The scanner retains the existing whitespace handling and leaves named enum values and
AllowNumbersbehavior intact. Tests cover signed values, leading zeros, Unicode whitespace, disabled numeric values, and invalid integer-like strings.Validation:
git diff --checkpassed. Full unit tests could not run locally because this checkout requires SDK 12.0.100-alpha.1.26557.102, which is not installed and could not be downloaded in this environment. CI will provide build and test results.