in src/Common/SequenceOfT.cs [124:174]
public void AdvanceTo(SequencePosition position)
{
var firstSegment = (SequenceSegment?)position.GetObject();
if (firstSegment == null)
{
// Emulate PipeReader behavior which is to just return for default(SequencePosition)
return;
}
if (ReferenceEquals(firstSegment, SequenceSegment.Empty) && this.Length == 0)
{
// We were called with our own empty buffer segment.
return;
}
int firstIndex = position.GetInteger();
// Before making any mutations, confirm that the block specified belongs to this sequence.
var current = this.first;
while (current != firstSegment && current != null)
{
current = current.Next;
}
if (current == null)
{
throw new ArgumentException("Position does not represent a valid position in this sequence.", nameof(position));
}
// Also confirm that the position is not a prior position in the block.
if (firstIndex < current.Start)
{
throw new ArgumentException("Position must not be earlier than current position.", nameof(position));
}
// Now repeat the loop, performing the mutations.
current = this.first;
while (current != firstSegment)
{
current = this.RecycleAndGetNext(current!);
}
firstSegment.AdvanceTo(firstIndex);
this.first = firstSegment.Length == 0 ? this.RecycleAndGetNext(firstSegment) : firstSegment;
if (this.first == null)
{
this.last = null;
}
}