Repository: microsoft/terminal
File: src/terminal/parser/stateMachine.cpp (C++)

Code before:
        _ActuateCurrentRun();
        success = _engine->ActionPassThroughString(_run);
    }

    return success;
}

// Routine Description:
// - Helper for entry to the state machine. Will take an array of characters
//     and print as many as it can without encountering a character indicating
//     a escape sequence, then feed characters into the state machine one at a
//     time until we return to the ground state.
// Arguments:
// - string - Characters to operate upon
// Return Value:
// - <none>
void StateMachine::ProcessString(const std::wstring_view string)
{
    size_t start = 0;
    size_t current = start;

    _currentString = string;
    _runOffset = 0;
    _runSize = 0;

    while (current < string.size())
    {
        // The run will be everything from the start INCLUDING the current one
        // in case we process the current character and it turns into a passthrough
        // fallback that picks up this _run inside `FlushToTerminal` above.
        _runOffset = start;
        _runSize = current - start + 1;

        if (_processingIndividually)
        {
            // If we're processing characters individually, send it to the state machine.
            ProcessCharacter(string.at(current));
            ++current;
            if (_state == VTStates::Ground) // Then check if we're back at ground. If we are, the next character (pwchCurr)
            { //   is the start of the next run of characters that might be printable.
                _processingIndividually = false;
                start = current;
            }
        }
        else
        {
            if (_isActionableFromGround(string.at(current))) // If the current char is the start of an escape sequence, or should be executed in ground state...
            {
                _ActuateCurrentRun();
                if (!_run.empty())
                {

Review comment (performance):
Repeatedly `substr`ing turns out to be quite expensive and takes up about 10%-12% of the total CPU time.

Code after:
        success = _engine->ActionPassThroughString(_CurrentRun());
    }

    return success;
}

// Routine Description:
// - Helper for entry to the state machine. Will take an array of characters
//     and print as many as it can without encountering a character indicating
//     a escape sequence, then feed characters into the state machine one at a
//     time until we return to the ground state.
// Arguments:
// - string - Characters to operate upon
// Return Value:
// - <none>
void StateMachine::ProcessString(const std::wstring_view string)
{
    size_t start = 0;
    size_t current = start;

    _currentString = string;
    _runOffset = 0;
    _runSize = 0;

    while (current < string.size())
    {
        // The run will be everything from the start INCLUDING the current one
        // in case we process the current character and it turns into a passthrough
        // fallback that picks up this _run inside `FlushToTerminal` above.
        _runOffset = start;
        _runSize = current - start + 1;

        if (_processingIndividually)
        {
            // If we're processing characters individually, send it to the state machine.
            ProcessCharacter(til::at(string, current));
            ++current;
            if (_state == VTStates::Ground) // Then check if we're back at ground. If we are, the next character (pwchCurr)
            { //   is the start of the next run of characters that might be printable.
                _processingIndividually = false;
                start = current;
            }
        }
        else
        {
            if (_isActionableFromGround(til::at(string, current))) // If the current char is the start of an escape sequence, or should be executed in ground state...
            {
                if (_runSize > 0)
                {
                    // Because the run above is composed INCLUDING current, we must
                    // trim it off here since we just determined it's actionable