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

Code before:
                    }
                }

                // Create labels for each branch
                var branchLabels = new Label[childCount];
                for (int i = 0; i < childCount; i++)
                {
                    branchLabels[i] = DefineLabel();
                }

                // Create a label for the case when no branch matches
                Label noMatchLabel = DefineLabel();

                // Build the switch table: an array of labels indexed by (charValue - minValue)
                var switchTable = new Label[range];
                for (int i = 0; i < range; i++)
                {
                    char c = (char)(minValue + i);
                    if (charToBranchIndex.TryGetValue(c, out int branchIndex))
                    {
                        switchTable[i] = branchLabels[branchIndex];
                    }
                    else
                    {
                        switchTable[i] = noMatchLabel;
                    }
                }

                // Load the first character of the slice, subtract minValue, and switch
                // slice[sliceStaticPos]
                Ldloca(slice);
                Ldc(sliceStaticPos);
                Call(SpanGetItemMethod);
                LdindU2();

                // Subtract minValue to get 0-based index
                if (minValue != 0)
                {
                    Ldc(minValue);
                    Sub();
                }

                // Bounds check: if the value is outside the range, jump to noMatch
                // if ((uint)(ch - minValue) >= range) goto noMatchLabel;
                Dup();
                Ldc(range);
                Label boundsCheckPassed = DefineLabel();
                BltUnFar(boundsCheckPassed);
                Pop(); // Pop the duplicated value before jumping to noMatch
                BrFar(noMatchLabel);
                MarkLabel(boundsCheckPassed);

Review comment (suggestion):
@copilot, this could be:
```csharp
switchTable[i] = charToBranchIndex.TryGetValue(c, out int branchIndex) ? branchLabels[branchIndex] : noMatchLabel;
```

Code after:
                    {
                        case RegexNodeKind.One:
                        case RegexNodeKind.Set:
                            // The character was handled entirely by the switch. No additional matching is needed.
                            sliceStaticPos++;
                            break;

                        case RegexNodeKind.Multi:
                            // First character was handled by the switch. Emit matching code for the remainder of the multi string.
                            sliceStaticPos++;
                            EmitNode(CreateSlicedMulti(child));
                            break;

                        case RegexNodeKind.Concatenate when child.Child(0) == startingLiteralNode && (startingLiteralNode.Kind is RegexNodeKind.One or RegexNodeKind.Set or RegexNodeKind.Multi):
                            // This is a concatenation where its first node is the starting literal we found and that starting literal
                            // is one of the nodes above that we know how to handle completely. This is a common
                            // enough case that we want to special-case it to avoid duplicating the processing for that character
                            // unnecessarily. First slice off the first character that was already handled. If that child is a multi, temporarily
                            // replace it with a node that doesn't have the already-matched first character; otherwise, replace it with an empty node
                            // that'll be ignored when rendered. Then emit the new tree, and subsequently restore the original child.
                            sliceStaticPos++;
                            RegexNode originalFirst = child.Child(0);
                            child.ReplaceChild(0,
                                child.Child(0).Kind is RegexNodeKind.Multi ?
                                    CreateSlicedMulti(child.Child(0)) :
                                    new RegexNode(RegexNodeKind.Empty, child.Options));
                            try
                            {
                                EmitNode(child);
                            }
                            finally
                            {
                                child.ReplaceChild(0, originalFirst);
                            }
                            break;

                        default:
                            EmitNode(child);
                            break;
                    }

                    // This is only ever used for alternations where no branch may backtrack
                    // (whether due to being atomic or simply because nothing in the branch
                    // can backtrack), so we can simply reset the doneLabel after emitting the
                    // child, as nothing will backtrack here (and we need to reset it so that
                    // all branches see the original).
                    doneLabel = originalDoneLabel;

                    // If we get here in the generated code, the branch completed successfully.
                    // Before jumping to the end, we need to zero out sliceStaticPos, so that no
                    // matter what the value is after the branch, whatever follows the alternate