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

Code before:
void COOKED_READ_DATA::_markAsDirty()
{
    _bufferDirty = true;
}

// Draws the contents of _buffer onto the screen.
void COOKED_READ_DATA::_flushBuffer()
{
    // _flushBuffer() is called often and is a good place to assert() that our _bufferCursor is still in bounds.
    assert(_bufferCursor <= _buffer.size());
    _bufferCursor = std::min(_bufferCursor, _buffer.size());

    if (!_bufferDirty)
    {
        return;
    }

    if (WI_IsFlagSet(_pInputBuffer->InputMode, ENABLE_ECHO_INPUT))
    {
        _unwindCursorPosition(_distanceCursor);

        const std::wstring_view view{ _buffer };
        const auto distanceBeforeCursor = _writeChars(view.substr(0, _bufferCursor));
        const auto distanceAfterCursor = _writeChars(view.substr(_bufferCursor));
        const auto distanceEnd = distanceBeforeCursor + distanceAfterCursor;
        const auto eraseDistance = std::max(0, _distanceEnd - distanceEnd);

        // If the contents of _buffer became shorter we'll have to erase the previously printed contents.
        _erase(eraseDistance);
        _unwindCursorPosition(distanceAfterCursor + eraseDistance);

        _distanceCursor = distanceBeforeCursor;
        _distanceEnd = distanceEnd;
    }

    _bufferDirty = false;
}

// This is just a small helper to fill the next N cells starting at the current cursor position with whitespace.
void COOKED_READ_DATA::_erase(const til::CoordType distance) const
{
    if (distance <= 0)
    {
        return;
    }

    std::array<wchar_t, 128> whitespace;
    auto remaining = gsl::narrow_cast<size_t>(distance);
    auto nextWriteSize = std::min(remaining, whitespace.size());

    // If we only need to erase 1 character worth of whitespace,

Review comment (suggestion):
After a lot of back and forth, I went with `ptrdiff_t` after all. That way we can have bigger buffers in the future and make full use of our `int32_t` coordinates.

Code after:
void COOKED_READ_DATA::_markAsDirty()
{
    _bufferDirty = true;
}

// Draws the contents of _buffer onto the screen.
void COOKED_READ_DATA::_flushBuffer()
{
    // _flushBuffer() is called often and is a good place to assert() that our _bufferCursor is still in bounds.
    assert(_bufferCursor <= _buffer.size());
    _bufferCursor = std::min(_bufferCursor, _buffer.size());

    if (!_bufferDirty)
    {
        return;
    }

    if (WI_IsFlagSet(_pInputBuffer->InputMode, ENABLE_ECHO_INPUT))
    {
        _unwindCursorPosition(_distanceCursor);

        const std::wstring_view view{ _buffer };
        const auto distanceBeforeCursor = _writeChars(view.substr(0, _bufferCursor));
        const auto distanceAfterCursor = _writeChars(view.substr(_bufferCursor));
        const auto distanceEnd = distanceBeforeCursor + distanceAfterCursor;
        const auto eraseDistance = std::max<ptrdiff_t>(0, _distanceEnd - distanceEnd);

        // If the contents of _buffer became shorter we'll have to erase the previously printed contents.
        _erase(eraseDistance);
        _unwindCursorPosition(distanceAfterCursor + eraseDistance);

        _distanceCursor = distanceBeforeCursor;
        _distanceEnd = distanceEnd;
    }

    _bufferDirty = false;
}

// This is just a small helper to fill the next N cells starting at the current cursor position with whitespace.
void COOKED_READ_DATA::_erase(ptrdiff_t distance) const
{
    if (distance <= 0)
    {
        return;
    }

    std::array<wchar_t, 128> whitespace;
    auto remaining = gsl::narrow_cast<size_t>(distance);
    auto nextWriteSize = std::min(remaining, whitespace.size());

    // If we only need to erase 1 character worth of whitespace,