Repository: microsoft/terminal
File: src/types/utils.cpp (C++)

Code before:
std::wstring Utils::ConvertPasteString(const std::wstring& wstr, const PasteOption option)
{
    std::wstring converted;
    converted.reserve(wstr.length());

    const auto isControlCode = [](wchar_t c) {
        if (c >= L'\x20' && c <= L'\x7f')
        {
            // Printable ASCII characters + DEL.
            return false;
        }

        if (c > L'\x7f')
        {
            // Not a control character.
            return false;
        }

        // All ASCII control characters will be removed except HT(0x09), LF(0x0a),
        // CR(0x0d) and DEL(0x7F).
        return c >= L'\x00' && c <= L'\x08' ||
               c >= L'\x0b' && c <= L'\x0c' ||
               c >= L'\x0e' && c <= L'\x1f';
    };

    std::wstring::size_type pos = 0;
    std::wstring::size_type begin = 0;

    if (WI_IsFlagSet(option, PasteOption::Bracketed))
    {
        converted.append(L"\x1b[200~");
    }

    while (pos < wstr.size())
    {
        const wchar_t c = wstr.at(pos);

        if (WI_IsFlagSet(option, PasteOption::CarriageReturnNewline) && c == L'\n')
        {
            // copy up to but not including the \n
            converted.append(wstr.cbegin() + begin, wstr.cbegin() + pos);
            if (!(pos > 0 && (wstr.at(pos - 1) == L'\r')))
            {
                // there was no \r before the \n we did not copy,
                // so append our own \r (this effectively replaces the \n
                // with a \r)
                converted.push_back(L'\r');
            }
            ++pos;
            begin = pos;
        }

Review comment (performance):
This is what I got for best performance. No more extra scaning.

I had to sacrifice extensibility for performance. Adding other options is still possible but also presumably harder than the lambda approach. As the saying goes, you win some you lose some.

Code after:
std::wstring Utils::FilterStringForPaste(const std::wstring_view wstr, const FilterOption option)
{
    std::wstring filtered;
    filtered.reserve(wstr.length());

    const auto isControlCode = [](wchar_t c) {
        if (c >= L'\x20' && c < L'\x7f')
        {
            // Printable ASCII characters.
            return false;
        }

        if (c > L'\x9f')
        {
            // Not a control code.
            return false;
        }

        // All C0 & C1 control codes will be removed except HT(0x09), LF(0x0a) and CR(0x0d).
        return c != L'\x09' && c != L'\x0a' && c != L'\x0d';
    };

    std::wstring::size_type pos = 0;
    std::wstring::size_type begin = 0;

    while (pos < wstr.size())
    {
        const wchar_t c = til::at(wstr, pos);

        if (WI_IsFlagSet(option, FilterOption::CarriageReturnNewline) && c == L'\n')
        {
            // copy up to but not including the \n
            filtered.append(wstr.cbegin() + begin, wstr.cbegin() + pos);
            if (!(pos > 0 && (til::at(wstr, pos - 1) == L'\r')))
            {
                // there was no \r before the \n we did not copy,
                // so append our own \r (this effectively replaces the \n
                // with a \r)
                filtered.push_back(L'\r');
            }
            ++pos;
            begin = pos;
        }
        else if (WI_IsFlagSet(option, FilterOption::ControlCodes) && isControlCode(c))
        {
            // copy up to but not including the control code
            filtered.append(wstr.cbegin() + begin, wstr.cbegin() + pos);
            ++pos;
            begin = pos;
        }
        else