You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Transform regex X| into X? and |X into X?? (#118087)
An alternation with two branches where the second is empty is the same as the first branch just being an optional loop; similarly, when the first branch is empty, it's a lazy optional loop of the second branch. Expressing as an optional is better optimized elsewhere in the regex transforms, e.g. with loop coalescing, so we're better off with the optional representation.
Copy file name to clipboardExpand all lines: src/libraries/System.Text.RegularExpressions/tests/UnitTests/RegexReductionTests.cs
+6Lines changed: 6 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -306,6 +306,12 @@ public class RegexReductionTests
306
306
[InlineData("abcd|aefg","a(?>bcd|efg)")]
307
307
[InlineData("abcd|abc|ab|a","a(?>bcd|bc|b|)")]
308
308
[InlineData("^abcd|^abce","^(?:abc[de])")]
309
+
[InlineData("abc|","(?:abc)?")]
310
+
[InlineData("a|","a?")]
311
+
[InlineData("(?:abc|)d","(?>(?:abc)?)d")]
312
+
[InlineData("(?:a|)a","a{1,2}")]
313
+
[InlineData("(?:a|)a*","a*")]
314
+
[InlineData("a+(?:a|)","a+")]
309
315
// [InlineData("abcde|abcdef", "abcde(?>|f)")] // TODO https://github.com/dotnet/runtime/issues/66031: Need to reorganize optimizations to avoid an extra Empty being left at the end of the tree
0 commit comments