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

Code before:
        // yet. More details in TerminalPage::_WindowRenamerKeyDown.
        _renamerPressedEnter = false;
        WindowRenamer().IsOpen(true);

        args.Handled(true);
    }

    void TerminalPage::_HandleSearchForText(const IInspectable& /*sender*/,
                                            const ActionEventArgs& args)
    {
        if (args)
        {
            if (const auto& realArgs = args.ActionArgs().try_as<SearchForTextArgs>())
            {
                const auto queryUrl = realArgs.QueryUrl();
                if (const auto termControl{ _GetActiveControl() })
                {
                    if (termControl.HasSelection())
                    {
                        const auto selections{ termControl.SelectedText(true) };

                        // concatenate the selection into a single line
                        auto searchText = std::accumulate(selections.begin(), selections.end(), std::wstring());

                        // make it compact by replacing consecutive whitespaces with a single space
                        searchText = std::regex_replace(searchText, std::wregex(LR"(\s+)"), L" ");

                        if (realArgs.WrapWithQuotes())
                        {
                            searchText = L"\"" + searchText + L"\"";
                        }

                        const auto finalString = queryUrl + Windows::Foundation::Uri::EscapeComponent(searchText);
                        winrt::Microsoft::Terminal::Control::OpenHyperlinkEventArgs shortcut{ finalString };
                        _OpenHyperlinkHandler(termControl, shortcut);
                        args.Handled(true);
                    }
                }
            }
        }
    }

    void TerminalPage::_HandleGlobalSummon(const IInspectable& /*sender*/,
                                           const ActionEventArgs& args)
    {
        // Manually return false. These shouldn't ever get here, except for when
        // we fail to register for the global hotkey. In that case, returning
        // false here will let the underlying terminal still process the key, as
        // if it wasn't bound at all.
        args.Handled(false);
    }

Review comment (performance):
I'm personally hoping to fully replace `std::regex` usage in this project with ICU in the future. One reason for this is because it uses ctype facets which are not optimized for `wchar_t` usage _at all_. It simply calls `GetStringTypeW` with `CT_CTYPE1` for every single individual input character, which is of course not quite optimal. (They aren't optimized for `char` usage either, because it doesn't even support UTF-8, but that's beside the point.)

Considering that Unicode whitespace is relatively rare in Terminals, we could simply erase 0x20 whitespace only. This is fairly easy to achieve:
```cpp
searchText.erase(std::unique(searchText.begin(), searchText.end(), [](const wchar_t a, const wchar_t b) {
    return a == L' ' && b == L' ';
}), text.end());
```

I personally believe that would already be sufficient for our use case. Otherwise, we can just keep the regex for now. It's not like this is a performance bottleneck anyways...

Code after:
        // yet. More details in TerminalPage::_WindowRenamerKeyDown.
        _renamerPressedEnter = false;
        WindowRenamer().IsOpen(true);

        args.Handled(true);
    }

    void TerminalPage::_HandleSearchForText(const IInspectable& /*sender*/,
                                            const ActionEventArgs& args)
    {
        if (const auto termControl{ _GetActiveControl() })
        {
            if (termControl.HasSelection())
            {
                const auto selections{ termControl.SelectedText(true) };

                // concatenate the selection into a single line
                auto searchText = std::accumulate(selections.begin(), selections.end(), std::wstring());

                // make it compact by replacing consecutive whitespaces with a single space
                searchText = std::regex_replace(searchText, std::wregex(LR"(\s+)"), L" ");

                std::wstring queryUrl;
                if (args)
                {
                    if (const auto& realArgs = args.ActionArgs().try_as<SearchForTextArgs>())
                    {
                        queryUrl = realArgs.QueryUrl().c_str();
                    }
                }

                // use global default if query URL is unspecified
                if (queryUrl.empty())
                {
                    queryUrl = _settings.GlobalSettings().SearchWebDefaultQueryUrl().c_str();
                }

                constexpr std::wstring_view queryToken{ L"%s" };
                if (const auto pos{ queryUrl.find(queryToken) }; pos != std::wstring_view::npos)
                {
                    queryUrl.replace(pos, queryToken.length(), Windows::Foundation::Uri::EscapeComponent(searchText));
                }

                winrt::Microsoft::Terminal::Control::OpenHyperlinkEventArgs shortcut{ queryUrl };
                _OpenHyperlinkHandler(termControl, shortcut);
                args.Handled(true);
            }
        }
    }

    void TerminalPage::_HandleGlobalSummon(const IInspectable& /*sender*/,