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

Code before:

    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 auto 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;
        }
    }

Review comment (suggestion):
this will blow up for `{10}`, but if we have a resource string that complicated we should probably rethink our lives.

Code after:
    size_t current = 0;
    while (current < resourceString.size())
    {
        const auto nextPlaceholder = resourceString.find(L"{", current);
        if (nextPlaceholder == std::wstring::npos)
        {
            result.push_back(std::wstring{ resourceString.substr(current) });
            break;
        }
        else
        {
            const auto length = nextPlaceholder - current;
            result.push_back(std::wstring{ resourceString.substr(current, length) });

            // Get the placeholder number (this code assumes that the placeholder is just 1 digit,
            // i.e. a number between 0-9)
            const auto placeholderNumber = std::stoi(std::wstring{ resourceString.substr(nextPlaceholder + 1, 1) });

            // Obtain the correct string from the span
            // The reason we need to obtain the correct index here is because different languages might end up ordering
            // the placeholders differently (for example, a string of the form "cc{0}cc{1}cc" might end up as
            // "c{1}cc{0}" in another language)
            // The span ensures that the correct placeholder is placed in the correct place in the final string
            result.push_back(gsl::at(placeholderStringsSpan, placeholderNumber));

            // Search for the next one, so increment by the length of 3 (i.e. the length of "{n}")
            current += length + 3;

            // The next index is larger than string size, which means the string
            // is in the format of "part1{0}part2{1}"
            // Add the last part which is an empty string
            if (current >= resourceString.size())
            {
                result.push_back(L"");
            }
        }
    }

    return result;
}

// Routine Description:
// - Pre-process text pasted (presumably from the clipboard) with provided option.
// Arguments:
// - wstr - String to process.
// - option - option to use.
// Return Value:
// - The result string.
std::wstring Utils::FilterStringForPaste(const std::wstring_view wstr, const FilterOption option)
{
    std::wstring filtered;