Repository: dotnet/runtime
File: src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs (C#)

Code before:
            // Emits the code for an alternation.
            void EmitAlternation(RegexNode node)
            {
                Debug.Assert(node.Kind is RegexNodeKind.Alternate, $"Unexpected type: {node.Kind}");
                Debug.Assert(node.ChildCount() >= 2, $"Expected at least 2 children, found {node.ChildCount()}");

                int childCount = node.ChildCount();
                Debug.Assert(childCount >= 2);

                string originalDoneLabel = doneLabel;

                // Both atomic and non-atomic are supported.  While a parent RegexNode.Atomic node will itself
                // successfully prevent backtracking into this child node, we can emit better / cheaper code
                // for an Alternate when it is atomic, so we still take it into account here.
                Debug.Assert(node.Parent is not null);
                bool isAtomic = rm.Analysis.IsAtomicByAncestor(node);

                // If no child branch overlaps with another child branch, we can emit more streamlined code
                // that avoids checking unnecessary branches, e.g. with abc|def|ghi if the next character in
                // the input is 'a', we needn't try the def or ghi branches.  A simple, relatively common case
                // of this is if every branch begins with a specific, unique character, in which case
                // the whole alternation can be treated as a simple switch, so we special-case that. However,
                // we can't goto _into_ switch cases, which means we can't use this approach if there's any
                // possibility of backtracking into the alternation.
                if ((node.Options & RegexOptions.RightToLeft) == 0 &&
                    TryEmitAlternationAsSwitch())
                {
                    return;
                }

                EmitAllBranches();
                return;

                // Tries to emit an alternation as a switch on the first character of each branch.
                // Returns true if the optimization was applied, false otherwise.
                bool TryEmitAlternationAsSwitch()
                {
                    // We can't use switched branches if there's any possibility of backtracking into the alternation.
                    if (!isAtomic)
                    {
                        for (int i = 0; i < childCount; i++)
                        {
                            if (rm.Analysis.MayBacktrack(node.Child(i)))
                            {
                                return false;
                            }
                        }
                    }

                    // Detect whether every branch begins with one or more unique characters.
                    if (!node.TryGetAlternationStartingChars(out _))

Review comment (suggestion):
@copilot, invert this so that the logic is more like:
```csharp
if ((node.Options & RegexOptions.RightToLeft) != 0 ||
    !TryEmitAlternationAsSwitch())
{
    EmitAllBranches();
}

return;
```

Code after:
            // Emits the code for an alternation.
            void EmitAlternation(RegexNode node)
            {
                Debug.Assert(node.Kind is RegexNodeKind.Alternate, $"Unexpected type: {node.Kind}");
                Debug.Assert(node.ChildCount() >= 2, $"Expected at least 2 children, found {node.ChildCount()}");

                int childCount = node.ChildCount();
                Debug.Assert(childCount >= 2);

                string originalDoneLabel = doneLabel;

                // Both atomic and non-atomic are supported.  While a parent RegexNode.Atomic node will itself
                // successfully prevent backtracking into this child node, we can emit better / cheaper code
                // for an Alternate when it is atomic, so we still take it into account here.
                Debug.Assert(node.Parent is not null);
                bool isAtomic = rm.Analysis.IsAtomicByAncestor(node);

                // If no child branch overlaps with another child branch, we can emit more streamlined code
                // that avoids checking unnecessary branches, e.g. with abc|def|ghi if the next character in
                // the input is 'a', we needn't try the def or ghi branches.  A simple, relatively common case
                // of this is if every branch begins with a specific, unique character, in which case
                // the whole alternation can be treated as a simple switch, so we special-case that. However,
                // we can't goto _into_ switch cases, which means we can't use this approach if there's any
                // possibility of backtracking into the alternation.
                if ((node.Options & RegexOptions.RightToLeft) != 0 ||
                    !TryEmitAlternationAsSwitch())
                {
                    EmitAllBranches();
                }

                return;

                // Tries to emit an alternation as a switch on the first character of each branch.
                // Returns true if the optimization was applied, false otherwise.
                bool TryEmitAlternationAsSwitch()
                {
                    // We can't use switched branches if there's any possibility of backtracking into the alternation.
                    if (!isAtomic)
                    {
                        for (int i = 0; i < childCount; i++)
                        {
                            if (rm.Analysis.MayBacktrack(node.Child(i)))
                            {
                                return false;
                            }
                        }
                    }

                    // Detect whether every branch begins with one or more unique characters.
                    if (!node.TryGetAlternationStartingChars(out _))
                    {