Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Dec 4, 2024
1 parent 6bc341d commit 7a421be
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/DotNext/Buffers/Binary/SevenBitEncodedInteger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct SevenBitEncodedInteger<T>(T value) : ISupplier<T>, IResettable
public static int MaxSizeInBytes { get; }

private static readonly int MaxSizeInBits;
private static readonly T Ox7FU;
private const byte BitMask = 0x7F;

static SevenBitEncodedInteger()
{
Expand All @@ -32,8 +32,7 @@ static SevenBitEncodedInteger()
bitCount += Unsafe.BitCast<bool, byte>(remainder is not 0);

MaxSizeInBytes = bitCount;
MaxSizeInBits = MaxSizeInBytes * 7;
Ox7FU = T.CreateTruncating(0x7FU);
MaxSizeInBits = bitCount * 7;
}

private int shift;
Expand All @@ -49,7 +48,7 @@ public bool Append(byte b)
if (shift == MaxSizeInBits)
ThrowInvalidDataException();

value |= (T.CreateTruncating(b) & Ox7FU) << shift;
value |= (T.CreateTruncating(b) & T.CreateTruncating(BitMask)) << shift;
shift += 7;
return (b & 0x80U) is not 0U;

Expand Down Expand Up @@ -88,7 +87,6 @@ public void Reset()
[StructLayout(LayoutKind.Auto)]
public struct Enumerator
{
private static readonly T OnesComplement0x7FU = ~Ox7FU;
private T value;
private byte current;
private bool completed;
Expand All @@ -109,9 +107,10 @@ public bool MoveNext()
if (completed)
return false;

if (value > Ox7FU)
var allBitsSet = T.CreateTruncating(BitMask);
if (value > allBitsSet)
{
current = byte.CreateTruncating(value | OnesComplement0x7FU);
current = byte.CreateTruncating(value | ~allBitsSet);
value >>>= 7;
}
else
Expand Down

0 comments on commit 7a421be

Please sign in to comment.