Skip to content

Commit

Permalink
Merge branch 'master' into bunch-of-qol-fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Julius R Friedman <[email protected]>
  • Loading branch information
juliusfriedman authored Nov 12, 2023
2 parents 74a8413 + 0b2973d commit aef8201
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions Common/Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,13 +1055,13 @@ protected internal override void Dispose(bool disposing)
public static Common.MemorySegment ReadDelimitedValue(this System.IO.Stream stream, byte delimit = Common.ASCII.LineFeed, bool includeDelimit = false)
{
//Declare a value which will end up in a register on the stack
int register = -1, count = 0;
int register = -1;

//Indicate when to terminate reading.
bool terminate = false;

//Use a MemoryStream as to not lock the reader
using (var buffer = new System.IO.MemoryStream(128))
using (var buffer = new System.IO.MemoryStream())
{
//While data can be read from the stream
while (false == terminate)
Expand All @@ -1077,15 +1077,13 @@ public static Common.MemorySegment ReadDelimitedValue(this System.IO.Stream stre

//Write the value read from the reader to the MemoryStream
buffer.WriteByte((byte)register);

//Store count
++count;
}

//If terminating then return the array contained in the MemoryStream.
var result = buffer.ToArray();
buffer.TryGetBuffer(out var bufferSegment);

//Return the bytes read from the stream
return new Common.MemorySegment(result, 0, count);
return new Common.MemorySegment(bufferSegment.Array, bufferSegment.Offset, bufferSegment.Count);
}
}

Expand Down

0 comments on commit aef8201

Please sign in to comment.