-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add overloads to decompress from ReadOnlySequence<byte> (#105)
- Loading branch information
1 parent
712659b
commit 5a01bad
Showing
3 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Buffers; | ||
|
||
#nullable enable | ||
|
||
namespace Snappier.Tests; | ||
|
||
public static class SequenceHelpers | ||
{ | ||
public static ReadOnlySequence<byte> CreateSequence(ReadOnlyMemory<byte> source, int maxSegmentSize) | ||
{ | ||
ReadOnlySequenceSegment<byte>? lastSegment = null; | ||
ReadOnlySequenceSegment<byte>? currentSegment = null; | ||
|
||
while (source.Length > 0) | ||
{ | ||
int index = Math.Max(source.Length - maxSegmentSize, 0); | ||
|
||
currentSegment = new Segment( | ||
source.Slice(index), | ||
currentSegment, | ||
index); | ||
|
||
lastSegment ??= currentSegment; | ||
source = source.Slice(0, index); | ||
} | ||
|
||
if (currentSegment is null) | ||
{ | ||
return default; | ||
} | ||
|
||
return new ReadOnlySequence<byte>(currentSegment, 0, lastSegment!, lastSegment!.Memory.Length); | ||
} | ||
|
||
private sealed class Segment : ReadOnlySequenceSegment<byte> | ||
{ | ||
public Segment(ReadOnlyMemory<byte> memory, ReadOnlySequenceSegment<byte>? next, long runningIndex) | ||
{ | ||
Memory = memory; | ||
Next = next; | ||
RunningIndex = runningIndex; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters