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

Code before:
    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;
        }
        else if (WI_IsFlagSet(option, PasteOption::FilterControlCodes) && isControlCode(c))
        {
            // copy up to but not including the control character
            converted.append(wstr.cbegin() + begin, wstr.cbegin() + pos);
            ++pos;
            begin = pos;
        }
        else
        {
            ++pos;
        }
    }

    // If we entered the while loop even once, begin would be non-zero
    // (because we set begin = pos right after incrementing pos)
    // So, if begin is still zero at this point it means we never found a newline
    // and we can just write the original string
    if (begin == 0)
    {
        if (WI_IsFlagSet(option, PasteOption::Bracketed))
        {
            converted.append(wstr);
            converted.append(L"\x1b[201~");
            return converted;
        }
        else

Review comment (performance):
This short circuit here should help the performance when the option is not set.

Code after:
    {
        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
        {
            ++pos;
        }
    }

    // If we entered the while loop even once, begin would be non-zero
    // (because we set begin = pos right after incrementing pos)
    // So, if begin is still zero at this point it means we never found a newline
    // and we can just write the original string
    if (begin == 0)
    {
        return std::wstring{ wstr };
    }
    else
    {
        filtered.append(wstr.cbegin() + begin, wstr.cend());
        // we may have removed some characters, so we may not need as much space
        // as we reserved earlier
        filtered.shrink_to_fit();
        return filtered;
    }
}

// Routine Description:
// - Shorthand check if a handle value is null or invalid.
// Arguments: