Repository: microsoft/terminal
File: src/renderer/uia/UiaRenderer.cpp (C++)

Code before:
    if (_cursorChanged)
    {
        try
        {
            _dispatcher->SignalCursorChanged();
        }
        CATCH_LOG();
    }
    if (!_queuedOutput.empty())
    {
        try
        {
            // The speech API is limited to 1000 characters at a time.
            // Break up the output into 1000 character chunks to ensure
            // the output isn't cut off.
            static const size_t sapiLimit{ 1000 };
            while (_queuedOutput.size() > sapiLimit)
            {
                const auto croppedText{ _queuedOutput.substr(0, sapiLimit) };
                _dispatcher->NotifyNewOutput(croppedText);
                _queuedOutput = _queuedOutput.substr(sapiLimit);
            }
            _dispatcher->NotifyNewOutput(_queuedOutput);
        }
        CATCH_LOG();
    }

    _selectionChanged = false;
    _textBufferChanged = false;
    _cursorChanged = false;
    _isPainting = false;
    _queuedOutput.clear();

    return S_OK;
}

// Routine Description:
// - This is currently unused.
// Arguments:
// - <none>
// Return Value:
// - S_FALSE
[[nodiscard]] HRESULT UiaEngine::ScrollFrame() noexcept
{
    return S_FALSE;
}

// Routine Description:
// - Paints the background of the invalid area of the frame.
//  For UIA, this doesn't mean anything. So do nothing.
// Arguments:

Review comment (suggestion):
Use this to avoid copying the entire output string twice in each iteration:
```cpp
try
{
    static constexpr size_t sapiLimit = 1000;

    std::wstring_view output{ _queuedOutput }; // <-- std::wstring::substr returns a string and not a string_view

    for (size_t offset = 0;; offset += sapiLimit) // <-- only a single substr() needed if we use offset instead
    {
        const auto croppedText = output.substr(offset, sapiLimit); // <-- not copying
        if (croppedText.empty())
        {
            break;
        }

        _dispatcher->NotifyNewOutput(croppedText);
    }
}
CATCH_LOG()
```

(You can drop the if condition at the start.)

Code after:
            _dispatcher->SignalTextChanged();
        }
        CATCH_LOG();
    }
    if (_cursorChanged)
    {
        try
        {
            _dispatcher->SignalCursorChanged();
        }
        CATCH_LOG();
    }
    try
    {
        // The speech API is limited to 1000 characters at a time.
        // Break up the output into 1000 character chunks to ensure
        // the output isn't cut off.
        static constexpr size_t sapiLimit{ 1000 };
        const std::wstring_view output{ _queuedOutput };
        for (size_t offset = 0;; offset += sapiLimit)
        {
            const auto croppedText{ output.substr(offset, sapiLimit) };
            if (croppedText.empty())
            {
                break;
            }
            _dispatcher->NotifyNewOutput(croppedText);
        }
    }
    CATCH_LOG();

    _selectionChanged = false;
    _textBufferChanged = false;
    _cursorChanged = false;
    _isPainting = false;
    _queuedOutput.clear();

    return S_OK;
}

// Routine Description:
// - This is currently unused.
// Arguments:
// - <none>
// Return Value:
// - S_FALSE
[[nodiscard]] HRESULT UiaEngine::ScrollFrame() noexcept
{
    return S_FALSE;
}