Repository: microsoft/terminal
File: src/host/output.cpp (C++)

Code before:

            screenInfo.GetTextBuffer().ScrollRows(source.Top(), source.Height(), delta);

            return;
        }
    }

    // 2. We can move any other scenario in-place without copying. We just have to carefully
    //    choose which direction we walk through filling up the target so it doesn't accidentally
    //    erase the source material before it can be copied/moved to the new location.
    {
        const auto target = Viewport::FromDimensions(targetOrigin, source.Dimensions());
        const auto walkDirection = Viewport::DetermineWalkDirection(source, target);

        auto sourcePos = source.GetWalkOrigin(walkDirection);
        auto targetPos = target.GetWalkOrigin(walkDirection);

        do
        {
            const auto data = OutputCell(*screenInfo.GetCellDataAt(sourcePos));
            screenInfo.Write(OutputCellIterator({ &data, 1 }), targetPos);

            source.WalkInBounds(sourcePos, walkDirection);
        } while (target.WalkInBounds(targetPos, walkDirection));
    }
}

// Routine Description:
// - This routine reads a sequence of attributes from the screen buffer.
// Arguments:
// - screenInfo - reference to screen buffer information.
// - coordRead - Screen buffer coordinate to begin reading from.
// - amountToRead - the number of elements to read
// Return Value:
// - vector of attribute data
std::vector<WORD> ReadOutputAttributes(const SCREEN_INFORMATION& screenInfo,
                                       const til::point coordRead,
                                       const size_t amountToRead)
{
    // Short circuit. If nothing to read, leave early.
    if (amountToRead == 0)
    {
        return {};
    }

    // Short circuit, if reading out of bounds, leave early.
    if (!screenInfo.GetBufferSize().IsInBounds(coordRead))
    {
        return {};
    }


Review comment (suggestion):
Is it possible for this to result in `source` running out of space (somehow?) before target does? I guess we're doing it in the previous code as well, but maybe one fewer time.

Code after:

            screenInfo.GetTextBuffer().ScrollRows(source.Top(), source.Height(), delta);

            return;
        }
    }

    // 2. We can move any other scenario in-place without copying. We just have to carefully
    //    choose which direction we walk through filling up the target so it doesn't accidentally
    //    erase the source material before it can be copied/moved to the new location.
    {
        const auto target = Viewport::FromDimensions(targetOrigin, source.Dimensions());
        const auto walkDirection = Viewport::DetermineWalkDirection(source, target);

        auto sourcePos = source.GetWalkOrigin(walkDirection);
        auto targetPos = target.GetWalkOrigin(walkDirection);

        // Note that we read two cells from the source before we start writing
        // to the target, so a two-cell DBCS character can't accidentally delete
        // itself when moving one cell horizontally.
        auto next = OutputCell(*screenInfo.GetCellDataAt(sourcePos));
        do
        {
            const auto current = next;
            source.WalkInBounds(sourcePos, walkDirection);
            next = OutputCell(*screenInfo.GetCellDataAt(sourcePos));
            screenInfo.GetTextBuffer().WriteLine(OutputCellIterator({ &current, 1 }), targetPos);
        } while (target.WalkInBounds(targetPos, walkDirection));
    }
}

// Routine Description:
// - This routine reads a sequence of attributes from the screen buffer.
// Arguments:
// - screenInfo - reference to screen buffer information.
// - coordRead - Screen buffer coordinate to begin reading from.
// - amountToRead - the number of elements to read
// Return Value:
// - vector of attribute data
std::vector<WORD> ReadOutputAttributes(const SCREEN_INFORMATION& screenInfo,
                                       const til::point coordRead,
                                       const size_t amountToRead)
{
    // Short circuit. If nothing to read, leave early.
    if (amountToRead == 0)
    {
        return {};
    }

    // Short circuit, if reading out of bounds, leave early.
    if (!screenInfo.GetBufferSize().IsInBounds(coordRead))