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

Code before:
        }
        else
        {
            const auto length = nextDelimiter - current;
            result.push_back(wstr.substr(current, length));
            // Skip this part and the delimiter. Start the next one
            current += length + 1;
            // The next index is larger than string size, which means the string
            // is in the format of "part1;part2;" (assuming use ';' as delimiter).
            // Add the last part which is an empty string.
            if (current >= wstr.size())
            {
                result.push_back(L"");
            }
        }
    }

    return result;
}
catch (...)
{
    LOG_CAUGHT_EXCEPTION();
    return {};
}

// Routine Description:
// - Splits a resource string that contains placeholders (i.e. a string of the form "cc{0}cc...cc{n}cc")
// - Allocates values to the placeholders according to the given map
// Arguments:
// - resourceString - the string that contains placeholders
// - placeholderToStringMap - the map which contains the strings the placeholders should map to
// Return Value:
// - a vector containing the result parts, with the placeholders being replaced by the relevant values according to the provided map
std::vector<std::wstring> Utils::SplitResourceStringWithPlaceholders(std::wstring resourceString, std::unordered_map<size_t, std::wstring> placeholderToStringMap)
{
    std::vector<std::wstring> result;
    size_t pos;
    while ((pos = resourceString.find(L"{")) != std::string::npos)
    {
        // We have found the first placeholder
        // Get the substring of everything up until this point and append it to result
        result.push_back(resourceString.substr(0, pos));

        // 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(resourceString.substr(pos + 1, 1));

        // Obtain the relevant string from the map
        // The reason we need to use a map 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}ccc{0}" in another language)

Review comment (suggestion):
This may not be the correct place to put this function, but I wanted somewhere that both `SettingsEditor` and `QueryExtension` could access. 

Code after:
        }
        else
        {
            const auto length = nextDelimiter - current;
            result.push_back(wstr.substr(current, length));
            // Skip this part and the delimiter. Start the next one
            current += length + 1;
            // The next index is larger than string size, which means the string
            // is in the format of "part1;part2;" (assuming use ';' as delimiter).
            // Add the last part which is an empty string.
            if (current >= wstr.size())
            {
                result.push_back(L"");
            }
        }
    }

    return result;
}
catch (...)
{
    LOG_CAUGHT_EXCEPTION();
    return {};
}

// Routine Description:
// - Splits a resource string that contains placeholders (i.e. a string of the form "cc{0}cc...cc{n}cc")
// - Allocates values to the placeholders according to the given span
// Arguments:
// - resourceString - the string that contains placeholders
// - placeholderStringsSpan - the span which contains the placeholder strings
// Return Value:
// - a vector containing the result parts, with the placeholders being replaced by the relevant values according to the provided map
std::vector<std::wstring> Utils::SplitResourceStringWithPlaceholders(std::wstring_view resourceString, std::span<std::wstring> placeholderStringsSpan)
{
    std::vector<std::wstring> result;
    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,