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

Code before:
        _focused = false;

        if (_uiaEngine.get())
        {
            THROW_IF_FAILED(_uiaEngine->Disable());
        }

        if (TSFInputControl() != nullptr)
        {
            TSFInputControl().NotifyFocusLeave();
        }

        if (_cursorTimer.has_value())
        {
            _cursorTimer.value().Stop();
            _terminal->SetCursorOn(false);
        }
    }

    // Method Description:
    // - Writes the given sequence as input to the active terminal connection,
    // Arguments:
    // - wstr: the string of characters to write to the terminal connection.
    // Return Value:
    // - <none>
    void TermControl::_SendInputToConnection(const winrt::param::hstring& wstr)
    {
        _connection.WriteInput(wstr);
    }

    // Method Description:
    // - Pre-process text pasted (presumably from the clipboard)
    //   before sending it over the terminal's connection, converting
    //   Windows-space \r\n line-endings to \r line-endings
    void TermControl::_SendPastedTextToConnection(const std::wstring& wstr)
    {
        // Some notes on this implementation:
        //
        // - std::regex can do this in a single line, but is somewhat
        //   overkill for a simple search/replace operation (and its
        //   performance guarantees aren't exactly stellar)
        // - The STL doesn't have a simple string search/replace method.
        //   This fact is lamentable.
        // - This line-ending conversion is intentionally fairly
        //   conservative, to avoid stripping out lone \n characters
        //   where they could conceivably be intentional.

        std::wstring stripped{ wstr };

        std::wstring::size_type pos = 0;


Review comment (bug):
unfortunately ☹️ we can't actually use the `winrt::param` namespace _even though it is literally the best namespace for taking in anything that can convert to hstring_

We might have to prefer `std::wstring_view`, which can be natively converted to an hstring (though it could also crash because hstring expects string_views to be null-terminated)

Code after:

        _focused = false;

        if (_uiaEngine.get())
        {
            THROW_IF_FAILED(_uiaEngine->Disable());
        }

        if (TSFInputControl() != nullptr)
        {
            TSFInputControl().NotifyFocusLeave();
        }

        if (_cursorTimer.has_value())
        {
            _cursorTimer.value().Stop();
            _terminal->SetCursorOn(false);
        }
    }

    // Method Description:
    // - Writes the given sequence as input to the active terminal connection.
    // - This method has been overloaded to allow zero-copy winrt::param::hstring optimizations.
    // Arguments:
    // - wstr: the string of characters to write to the terminal connection.
    // Return Value:
    // - <none>
    void TermControl::_SendInputToConnection(const winrt::hstring& wstr)
    {
        _connection.WriteInput(wstr);
    }

    void TermControl::_SendInputToConnection(std::wstring_view wstr)
    {
        _connection.WriteInput(wstr);
    }

    // Method Description:
    // - Pre-process text pasted (presumably from the clipboard)
    //   before sending it over the terminal's connection, converting
    //   Windows-space \r\n line-endings to \r line-endings
    void TermControl::_SendPastedTextToConnection(const std::wstring& wstr)
    {
        // Some notes on this implementation:
        //
        // - std::regex can do this in a single line, but is somewhat
        //   overkill for a simple search/replace operation (and its
        //   performance guarantees aren't exactly stellar)
        // - The STL doesn't have a simple string search/replace method.
        //   This fact is lamentable.
        // - This line-ending conversion is intentionally fairly