Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lateapexearlyspeed-issue-30778 Create method: TryReadExact(). #57921

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -291,6 +291,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>True if remaining items in current SequenceReader is enough for <paramref name="count"/>.</returns>
lateapexearlyspeed marked this conversation as resolved.
Show resolved Hide resolved
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