Skip to content

Commit 0a447f8

Browse files
Sync shared code from aspnetcore (#114498)
Co-authored-by: BrennanConroy <[email protected]>
1 parent 94bf131 commit 0a447f8

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

src/libraries/Common/src/System/Net/Http/aspnetcore/Http3/Helpers/VariableLengthIntegerHelper.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,19 @@ static bool TryReadSlow(ref SequenceReader<byte> reader, out long value)
128128
}
129129
}
130130

131-
public static long GetInteger(in ReadOnlySequence<byte> buffer, out SequencePosition consumed, out SequencePosition examined)
131+
// If callsite has 'examined', set it to buffer.End if the integer wasn't successfully read, otherwise set examined = consumed.
132+
public static bool TryGetInteger(in ReadOnlySequence<byte> buffer, out SequencePosition consumed, out long integer)
132133
{
133134
var reader = new SequenceReader<byte>(buffer);
134-
if (TryRead(ref reader, out long value))
135+
if (TryRead(ref reader, out integer))
135136
{
136-
consumed = examined = buffer.GetPosition(reader.Consumed);
137-
return value;
137+
consumed = buffer.GetPosition(reader.Consumed);
138+
return true;
138139
}
139140
else
140141
{
141-
consumed = default;
142-
examined = buffer.End;
143-
return -1;
142+
consumed = buffer.Start;
143+
return false;
144144
}
145145
}
146146

src/libraries/Common/tests/Tests/System/Net/aspnetcore/Http3/VariableLengthIntegerHelperTests.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

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

229-
Assert.Equal(2, result);
229+
Assert.True(result);
230+
Assert.Equal(2, integer);
230231
Assert.Equal(7, consumed.GetInteger());
231-
Assert.Equal(7, examined.GetInteger());
232232
}
233233

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

244-
Assert.Equal(-1, result);
244+
Assert.False(result);
245245
Assert.Equal(0, consumed.GetInteger());
246-
Assert.Equal(6, examined.GetInteger());
247246
}
248247

249248
[Fact]

0 commit comments

Comments
 (0)