Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,19 @@ static bool TryReadSlow(ref SequenceReader<byte> reader, out long value)
}
}

public static long GetInteger(in ReadOnlySequence<byte> buffer, out SequencePosition consumed, out SequencePosition examined)
// If callsite has 'examined', set it to buffer.End if the integer wasn't successfully read, otherwise set examined = consumed.
public static bool TryGetInteger(in ReadOnlySequence<byte> buffer, out SequencePosition consumed, out long integer)
{
var reader = new SequenceReader<byte>(buffer);
if (TryRead(ref reader, out long value))
if (TryRead(ref reader, out integer))
{
consumed = examined = buffer.GetPosition(reader.Consumed);
return value;
consumed = buffer.GetPosition(reader.Consumed);
return true;
}
else
{
consumed = default;
examined = buffer.End;
return -1;
consumed = buffer.Start;
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
Expand Down Expand Up @@ -223,12 +223,12 @@ public void GetInteger_ValidSegmentedSequence()
MemorySegment<byte> memorySegment2 = memorySegment1.Append(new byte[] { 0, 0, 0, 0, 0, 0, 2 });
ReadOnlySequence<byte> readOnlySequence = new ReadOnlySequence<byte>(
memorySegment1, 0, memorySegment2, memorySegment2.Memory.Length);
long result = VariableLengthIntegerHelper.GetInteger(readOnlySequence,
out SequencePosition consumed, out SequencePosition examined);
bool result = VariableLengthIntegerHelper.TryGetInteger(readOnlySequence,
out SequencePosition consumed, out long integer);

Assert.Equal(2, result);
Assert.True(result);
Assert.Equal(2, integer);
Assert.Equal(7, consumed.GetInteger());
Assert.Equal(7, examined.GetInteger());
}

[Fact]
Expand All @@ -238,12 +238,11 @@ public void GetInteger_NotValidSegmentedSequence()
MemorySegment<byte> memorySegment2 = memorySegment1.Append(new byte[] { 0, 0, 0, 0, 0, 2 });
ReadOnlySequence<byte> readOnlySequence = new ReadOnlySequence<byte>(
memorySegment1, 0, memorySegment2, memorySegment2.Memory.Length);
long result = VariableLengthIntegerHelper.GetInteger(readOnlySequence,
out SequencePosition consumed, out SequencePosition examined);
bool result = VariableLengthIntegerHelper.TryGetInteger(readOnlySequence,
out SequencePosition consumed, out long integer);

Assert.Equal(-1, result);
Assert.False(result);
Assert.Equal(0, consumed.GetInteger());
Assert.Equal(6, examined.GetInteger());
}

[Fact]
Expand Down
Loading