/// Peeks at the next value at specific offset without advancing the reader.
/// </summary>
/// <param name="offset">The offset from current position.</param>
- /// <param name="value">The next value or default if at the end.</param>
- /// <returns>False if at the end of the reader.</returns>
+ /// <param name="value">The next value, or the default value if at the end of the reader.</param>
+ /// <returns><c>true</c> if the reader is not at its end and the peek operation succeeded; <c>false</c> if at the end of the reader.</returns>
public readonly bool TryPeek(long offset, out T value)
{
if (offset < 0)
return false;
}
+ // Sum CurrentSpanIndex + offset could overflow as is but the value of offset should be very large
+ // because we check Remaining <= offset above so to overflow we should have a ReadOnlySequence close to 8 exabytes
+ Debug.Assert(CurrentSpanIndex + offset >= 0);
+
// If offset doesn't fall inside current segment move to next until we find correct one
if ((CurrentSpanIndex + offset) <= CurrentSpan.Length - 1)
{
+ Debug.Assert(offset <= int.MaxValue);
+
value = CurrentSpan[CurrentSpanIndex + (int)offset];
return true;
}
SequencePosition nextPosition = _nextPosition;
ReadOnlyMemory<T> currentMemory = default;
- while (Sequence.TryGet(ref nextPosition, out currentMemory, true))
+ while (Sequence.TryGet(ref nextPosition, out currentMemory, advance: true))
{
// Skip empty segment
if (currentMemory.Length > 0)
{
- if (remainingOffset > currentMemory.Length - 1)
+ if (remainingOffset >= currentMemory.Length)
{
// Subtract current non consumed data
remainingOffset -= currentMemory.Length;