Repository: microsoft/terminal
File: src/cascadia/TerminalControl/TermControlAutomationPeer.cpp (C++)

Code before:
        if (!dispatcher)
        {
            return;
        }
        dispatcher.RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [weakThis{ get_weak() }]() {
            if (auto strongThis{ weakThis.get() })
            {
                // The event that is raised when the text was changed in an edit control.
                // Do NOT fire a TextEditTextChanged. Generally, an app on the other side
                //    will expect more information. Though you can dispatch that event
                //    on its own, it may result in a nullptr exception on the other side
                //    because no additional information was provided. Crashing the screen
                //    reader.
                strongThis->RaiseAutomationEvent(AutomationEvents::TextPatternOnTextSelectionChanged);
            }
        });
    }

    void TermControlAutomationPeer::NotifyNewOutput(std::wstring newOutput)
    {
        // Try to suppress any events (or event data)
        // that is just the keypress the user made
        auto sanitized{ Sanitize(newOutput) };
        while (!_keyEvents.empty() && IsReadable(sanitized))
        {
            if (towupper(sanitized.front()) == _keyEvents.back())
            {
                sanitized = sanitized.substr(1);
                _keyEvents.pop_front();
            }
            else
            {
                // The output doesn't match,
                // so clear the input stack and
                // move on to fire the event.
                _keyEvents.clear();
                break;
            }
        }

        // Suppress event if the remaining text is not readable
        if (!IsReadable(sanitized))
        {
            return;
        }

        auto dispatcher{ Dispatcher() };
        if (!dispatcher)
        {
            return;
        }

Review comment (suggestion):
Do you want to uppercase unicode here? In that case `towupper` won't be accurate, because some languages need more or less code points when transforming to upper case (e.g. ß to SS).
If you only want to convert ASCII you can use `til::toupper_ascii`.

To be honest I don't quite get what `_keyEvents` does here. If I'm the only one, don't mind me, but if others feel the same way, a more comprehensive comment would probably be helpful.

Code after:
            }
        });
    }

    // Method Description:
    // - Signals the ui automation client that the cursor's state has changed and should be updated
    // Arguments:
    // - <none>
    // Return Value:
    // - <none>
    void TermControlAutomationPeer::SignalCursorChanged()
    {
        UiaTracing::Signal::CursorChanged();
        auto dispatcher{ Dispatcher() };
        if (!dispatcher)
        {
            return;
        }
        dispatcher.RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [weakThis{ get_weak() }]() {
            if (auto strongThis{ weakThis.get() })
            {
                // The event that is raised when the text was changed in an edit control.
                // Do NOT fire a TextEditTextChanged. Generally, an app on the other side
                //    will expect more information. Though you can dispatch that event
                //    on its own, it may result in a nullptr exception on the other side
                //    because no additional information was provided. Crashing the screen
                //    reader.
                strongThis->RaiseAutomationEvent(AutomationEvents::TextPatternOnTextSelectionChanged);
            }
        });
    }

    void TermControlAutomationPeer::NotifyNewOutput(std::wstring_view newOutput)
    {
        // Try to suppress any events (or event data)
        // that is just the keypress the user made
        auto sanitized{ Sanitize(newOutput) };
        while (!_keyEvents.empty() && IsReadable(sanitized))
        {
            if (til::toupper_ascii(sanitized.front()) == _keyEvents.front())
            {
                // the key event's character (i.e. the "A" key) matches
                // the output character (i.e. "a" or "A" text).
                // We can assume that the output character resulted from
                // the pressed key, so we can ignore it.
                sanitized = sanitized.substr(1);
                _keyEvents.pop_front();
            }
            else
            {
                // The output doesn't match,