Skip to content

Commit

Permalink
lateapexearlyspeed-issue-30778 Create method: TryReadExact(). (#57921)
Browse files Browse the repository at this point in the history
* lateapexearlyspeed-issue-30778 Create method: TryReadExact().

* issue-30778 Add test for SequenceReader.TryReadExact().

* issue-30778 Fix comment: format return xml doc.

Co-authored-by: Jeff Handley <[email protected]>

Co-authored-by: Jeff Handley <[email protected]>
  • Loading branch information
lateapexearlyspeed and jeffhandley authored Oct 13, 2021
1 parent 9dc925d commit 6890b50
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/libraries/System.Memory/ref/System.Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public void Rewind(long count) { }
public bool TryReadTo(out System.ReadOnlySpan<T> span, T delimiter, T delimiterEscape, bool advancePastDelimiter = true) { throw null; }
public bool TryReadToAny(out System.Buffers.ReadOnlySequence<T> sequence, System.ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true) { throw null; }
public bool TryReadToAny(out System.ReadOnlySpan<T> span, System.ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true) { throw null; }
public bool TryReadExact(int count, out System.Buffers.ReadOnlySequence<T> sequence) { throw null; }
}
public readonly partial struct StandardFormat : System.IEquatable<System.Buffers.StandardFormat>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,32 @@ public bool TryReadTo(out ReadOnlySequence<T> sequence, ReadOnlySpan<T> delimite
return false;
}

/// <summary>
/// Try to read data with given <paramref name="count"/>.
/// </summary>
/// <param name="count">Read count.</param>
/// <param name="sequence">The read data, if successfully read requested <paramref name="count"/> data.</param>
/// <returns><c>true</c> if remaining items in current <see cref="SequenceReader{T}" /> is enough for <paramref name="count"/>.</returns>
public bool TryReadExact(int count, out ReadOnlySequence<T> sequence)
{
if (count < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count);
}
if (count > Remaining)
{
sequence = default;
return false;
}

sequence = Sequence.Slice(Position, count);
if (count != 0)
{
Advance(count);
}
return true;
}

/// <summary>
/// Advance until the given <paramref name="delimiter"/>, if found.
/// </summary>
Expand Down
33 changes: 33 additions & 0 deletions src/libraries/System.Memory/tests/SequenceReader/ReadTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Linq;
using System.Buffers;
using Xunit;

Expand Down Expand Up @@ -107,6 +108,38 @@ public void TryReadTo_Sequence(bool advancePastDelimiter, bool useEscapeOverload
}
}

[Fact]
public void TryReadExact_Sequence()
{
ReadOnlySequence<int> data = SequenceFactory.Create(new int[][] {
new int[] { 0 },
new int[] { 1, 2 },
new int[] { },
new int[] { 3, 4 }
});

var sequenceReader = new SequenceReader<int>(data);

Assert.True(sequenceReader.TryReadExact(0, out ReadOnlySequence<int> sequence));
Assert.Equal(0, sequence.Length);

for (int i = 0; i < 2; i++)
{
Assert.True(sequenceReader.TryReadExact(2, out sequence));
Assert.Equal(Enumerable.Range(i * 2, 2), sequence.ToArray());
}

// There is only 1 item in sequence reader
Assert.False(sequenceReader.TryReadExact(2, out _));

// The last 1 item was not advanced so still can be fetched
Assert.True(sequenceReader.TryReadExact(1, out sequence));
Assert.Equal(1, sequence.Length);
Assert.Equal(4, sequence.FirstSpan[0]);

Assert.True(sequenceReader.End);
}

[Theory,
InlineData(false),
InlineData(true),]
Expand Down

0 comments on commit 6890b50

Please sign in to comment.