Repository: microsoft/terminal
File: src/buffer/out/textBuffer.cpp (C++)

Code before:
    if (accessibilityMode)
    {
        bufferSize.IncrementInBounds(resultPos, true);
    }
    return resultPos;
}

// Method Description:
// - Update pos to be the beginning of the next glyph/character. This is used for accessibility
// Arguments:
// - pos - a til::point on the word you are currently on
// - allowExclusiveEnd - allow result to be the exclusive limit (one past limit)
// - limit - boundaries for the iterator to operate within
// Return Value:
// - true, if successfully updated pos. False, if we are unable to move (usually due to a buffer boundary)
// - pos - The til::point for the first cell of the current glyph (inclusive)
bool TextBuffer::MoveToNextGlyph(til::point& pos, bool allowExclusiveEnd, std::optional<til::point> limitOptional) const
{
    const auto bufferSize = GetSize();
    bool pastEndInclusive = false;
    const auto limit = [bufferSize, limitOptional](bool& pastEndInclusive) {
        const auto endInclusive{ bufferSize.BottomRightInclusive() };
        const auto val = limitOptional.value_or(endInclusive);
        pastEndInclusive = val > endInclusive;
        return pastEndInclusive ? endInclusive : val;
    }(pastEndInclusive);

    const auto distanceToLimit{ bufferSize.CompareInBounds(pos, limit) + (pastEndInclusive ? 1 : 0) };
    if (distanceToLimit >= 0)
    {
        // Corner Case: we're on/past the limit
        // Clamp us to the limit
        pos = limit;
        return false;
    }
    else if (!allowExclusiveEnd && distanceToLimit == -1)
    {
        // Corner Case: we're just before the limit
        // and we are not allowed onto the exclusive end.
        // Fail to move.
        return false;
    }

    // Try to move forward, but if we hit the buffer boundary, we fail to move.
    auto iter{ GetCellDataAt(pos, bufferSize) };
    const bool success{ ++iter };

    // Move again if we're on a wide glyph
    if (success && iter->DbcsAttr().IsTrailing())
    {
        ++iter;

Review comment (bug):
Hmm I thought I had commented it, but it seems like I forgot...

A more standard way to write this would be this IMHO:
```cpp
bool pastEndInclusive;
til::point limit;
{
    const auto endInclusive = bufferSize.BottomRightInclusive();
    const auto val = limitOptional.value_or(endInclusive);
    pastEndInclusive = val > endInclusive;
    limit = pastEndInclusive ? endInclusive : val;
}
```

I believe that would be easier to parse but also to debug (when you use F10 stepwise debugging it won't "jump around" or unexpectedly "skip" stuff due to the function call).

Another option is:
```cpp
const auto endInclusive = bufferSize.BottomRightInclusive();
auto limit = limitOptional.value_or(endInclusive);
const auto pastEndInclusive = limit > endInclusive;

if (pastEndInclusive)
{
    limit = endInclusive;
}
```

Finally, you can also just write `[&]` and drop the lambda parameter and explicit arguments.

Code after:
    if (accessibilityMode)
    {
        bufferSize.IncrementInBounds(resultPos, true);
    }
    return resultPos;
}

// Method Description:
// - Update pos to be the beginning of the next glyph/character. This is used for accessibility
// Arguments:
// - pos - a til::point on the word you are currently on
// - allowExclusiveEnd - allow result to be the exclusive limit (one past limit)
// - limit - boundaries for the iterator to operate within
// Return Value:
// - true, if successfully updated pos. False, if we are unable to move (usually due to a buffer boundary)
// - pos - The til::point for the first cell of the current glyph (inclusive)
bool TextBuffer::MoveToNextGlyph(til::point& pos, bool allowExclusiveEnd, std::optional<til::point> limitOptional) const
{
    const auto bufferSize = GetSize();
    bool pastEndInclusive;
    til::point limit;
    {
        // if the limit is past the end of the buffer,
        // 1) clamp limit to end of buffer
        // 2) set pastEndInclusive
        const auto endInclusive{ bufferSize.BottomRightInclusive() };
        const auto val = limitOptional.value_or(endInclusive);
        pastEndInclusive = val > endInclusive;
        limit = pastEndInclusive ? endInclusive : val;
    }

    const auto distanceToLimit{ bufferSize.CompareInBounds(pos, limit) + (pastEndInclusive ? 1 : 0) };
    if (distanceToLimit >= 0)
    {
        // Corner Case: we're on/past the limit
        // Clamp us to the limit
        pos = limit;
        return false;
    }
    else if (!allowExclusiveEnd && distanceToLimit == -1)
    {
        // Corner Case: we're just before the limit
        // and we are not allowed onto the exclusive end.
        // Fail to move.
        return false;
    }

    // Try to move forward, but if we hit the buffer boundary, we fail to move.
    auto iter{ GetCellDataAt(pos, bufferSize) };
    const bool success{ ++iter };