Repository: microsoft/terminal
File: src/cascadia/TerminalApp/TerminalPage.cpp (C++)

Code before:
        // will crash on the UI thread, because the main thread is a STA.
        co_await winrt::resume_background();

        try
        {
            hstring text = L"";
            if (data.Contains(StandardDataFormats::Text()))
            {
                text = co_await data.GetTextAsync();
            }
            // Windows Explorer's "Copy address" menu item stores a StorageItem in the clipboard, and no text.
            else if (data.Contains(StandardDataFormats::StorageItems()))
            {
                Windows::Foundation::Collections::IVectorView<Windows::Storage::IStorageItem> items = co_await data.GetStorageItemsAsync();
                if (items.Size() > 0)
                {
                    Windows::Storage::IStorageItem item = items.GetAt(0);
                    text = item.Path();
                }
            }

            bool trimTrailingWhitespace = _settings.GlobalSettings().TrimPaste();
            if (trimTrailingWhitespace)
            {
                std::wstring buffer{ text };
                buffer.erase(std::find_if(buffer.rbegin(), buffer.rend(), [](auto c) { return !std::isspace(c); }).base(), buffer.end());
                text = hstring(buffer);
            }

            bool warnMultiLine = _settings.GlobalSettings().WarnAboutMultiLinePaste();
            if (warnMultiLine)
            {
                const auto isNewLineLambda = [](auto c) { return c == L'\n' || c == L'\r'; };
                const auto hasNewLine = std::find_if(text.cbegin(), text.cend(), isNewLineLambda) != text.cend();
                warnMultiLine = hasNewLine;
            }

            constexpr const std::size_t minimumSizeForWarning = 1024 * 5; // 5 KiB
            const bool warnLargeText = text.size() > minimumSizeForWarning &&
                                       _settings.GlobalSettings().WarnAboutLargePaste();

            if (warnMultiLine || warnLargeText)
            {
                co_await winrt::resume_foreground(Dispatcher());

                if (warnMultiLine)
                {
                    const auto focusedTab = _GetFocusedTabImpl();
                    // Do not warn about multi line pasting if the current tab has bracketed paste enabled.
                    warnMultiLine = warnMultiLine && !focusedTab->GetActiveTerminalControl().BracketedPasteEnabled();
                }

Review comment (suggestion):
`std::iswspace` must be used here, as `std::isspace` is only defined to work for `char`, however we're using `wchar_t`s here. Additionally you can use `std::find_if_not` here and then probably pass `std::iswspace` directly without the lambda.

Your change will be the first time we need to modify the `text` string. If you want to you could modify the code in this function to work without copying `text` into `buffer` first.
It work look like this:
```cpp
if (_settings.GlobalSettings().TrimPaste())
{
    std::wstring_view textView{ text };
    if(const auto pos = textView.find_last_not_of(L"\t\n\v\f\r "); pos != textView.npos)
    {
        textView.remove_suffix(v.size() - pos);
        text = { textView };
    }
}
```

Technically the entire function could work with a string view, but you'd have to change `PasteFromClipboardEventArgs` to accept a `std::wstring_view` instead, etc. and there's no real reason for you to go through all that.

Code after:
        // will crash on the UI thread, because the main thread is a STA.
        co_await winrt::resume_background();

        try
        {
            hstring text = L"";
            if (data.Contains(StandardDataFormats::Text()))
            {
                text = co_await data.GetTextAsync();
            }
            // Windows Explorer's "Copy address" menu item stores a StorageItem in the clipboard, and no text.
            else if (data.Contains(StandardDataFormats::StorageItems()))
            {
                Windows::Foundation::Collections::IVectorView<Windows::Storage::IStorageItem> items = co_await data.GetStorageItemsAsync();
                if (items.Size() > 0)
                {
                    Windows::Storage::IStorageItem item = items.GetAt(0);
                    text = item.Path();
                }
            }

            if (_settings.GlobalSettings().TrimPaste())
            {
                std::wstring_view textView{ text };
                const auto pos = textView.find_last_not_of(L"\t\n\v\f\r ");
                if (pos == textView.npos)
                {
                    // Text is all white space, nothing to paste
                    co_return;
                }
                else if (const auto toRemove = textView.size() - 1 - pos; toRemove > 0)
                {
                    textView.remove_suffix(toRemove);
                    text = { textView };
                }
            }

            bool warnMultiLine = _settings.GlobalSettings().WarnAboutMultiLinePaste();
            if (warnMultiLine)
            {
                const auto isNewLineLambda = [](auto c) { return c == L'\n' || c == L'\r'; };
                const auto hasNewLine = std::find_if(text.cbegin(), text.cend(), isNewLineLambda) != text.cend();
                warnMultiLine = hasNewLine;
            }

            constexpr const std::size_t minimumSizeForWarning = 1024 * 5; // 5 KiB
            const bool warnLargeText = text.size() > minimumSizeForWarning &&
                                       _settings.GlobalSettings().WarnAboutLargePaste();

            if (warnMultiLine || warnLargeText)
            {