-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Add RegexGenerator support for RegexOptions.NonBacktracking #61903
Comments
Tagging subscribers to this area: @eerhardt, @dotnet/area-system-text-regularexpressions Issue DetailsCurrently, if you specify RegexOptions.NonBacktracking with the Regex source generator, e.g. [RegexGenerator("abc", RegexOptions.NonBacktracking)]
private static partial Regex Example(); you get code like this: private static partial Regex Example() => GeneratedRegex_Example_A6B1D425.Instance;
private sealed class GeneratedRegex_Example_A6B1D425 : Regex
{
public static Regex Instance { get; } = new Regex("abc", RegexOptions.NonBacktracking);
} In other words, you just get a cached instance of constructing runtime/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs Lines 107 to 110 in 69b5d67
due to it not yet being implemented: runtime/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs Lines 676 to 680 in 69b5d67
We should implement it 😄, at least for a subset of expressions, e.g. ones where we can fully construct a reasonably-sized DFA graph at build time and emit an executable representation of it into the source. cc: @olsaarik, @veanes, @joperezr, @danmoseley
|
has any headway happened on this subject? |
No |
@LennoxP90 can you share what motivates your interest? |
just the non backtracking feature so we can have the best of both worlds fast startup and non backtracking support |
@LennoxP90 just curious what is your use case for non backtracking - defense in depth against flawed patterns, use of untrusted patterns, better perf in particular case, etc.. |
(Your post makes complete sense of course just interested to learn more about users of this engine) |
i may not need it but i have a regex that determines if an input is a link or not here is my regex: |
The That said, for the vast majority of cases a backtracking engine will be much faster into looking for a match (especially for the cases when a match exists) which is why most people that care about performance and know what their pattern is at compile time should opt for the source generated or compiled engine. For your specific pattern called above, there are only two loops, the optional I believe this is why @danmoseley above asked if your use case involved running unkown or untrusted patterns, which is a very valid reason why to use |
Currently, if you specify RegexOptions.NonBacktracking with the Regex source generator, e.g.
you get code like this:
In other words, you just get a cached instance of constructing
Regex
rather than having the implementation actually output in the source. That's because we currently special-case this flag:runtime/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs
Lines 107 to 110 in 69b5d67
due to it not yet being implemented:
runtime/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs
Lines 676 to 680 in 69b5d67
We should implement it 😄, at least for a subset of expressions, e.g. ones where we can fully construct a reasonably-sized DFA graph at build time and emit an executable representation of it into the source.
cc: @olsaarik, @veanes, @joperezr, @danmoseley
The text was updated successfully, but these errors were encountered: