Skip to content

Commit 7a421be

Browse files
committed
Code cleanup
1 parent 6bc341d commit 7a421be

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/DotNext/Buffers/Binary/SevenBitEncodedInteger.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public struct SevenBitEncodedInteger<T>(T value) : ISupplier<T>, IResettable
2323
public static int MaxSizeInBytes { get; }
2424

2525
private static readonly int MaxSizeInBits;
26-
private static readonly T Ox7FU;
26+
private const byte BitMask = 0x7F;
2727

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

3434
MaxSizeInBytes = bitCount;
35-
MaxSizeInBits = MaxSizeInBytes * 7;
36-
Ox7FU = T.CreateTruncating(0x7FU);
35+
MaxSizeInBits = bitCount * 7;
3736
}
3837

3938
private int shift;
@@ -49,7 +48,7 @@ public bool Append(byte b)
4948
if (shift == MaxSizeInBits)
5049
ThrowInvalidDataException();
5150

52-
value |= (T.CreateTruncating(b) & Ox7FU) << shift;
51+
value |= (T.CreateTruncating(b) & T.CreateTruncating(BitMask)) << shift;
5352
shift += 7;
5453
return (b & 0x80U) is not 0U;
5554

@@ -88,7 +87,6 @@ public void Reset()
8887
[StructLayout(LayoutKind.Auto)]
8988
public struct Enumerator
9089
{
91-
private static readonly T OnesComplement0x7FU = ~Ox7FU;
9290
private T value;
9391
private byte current;
9492
private bool completed;
@@ -109,9 +107,10 @@ public bool MoveNext()
109107
if (completed)
110108
return false;
111109

112-
if (value > Ox7FU)
110+
var allBitsSet = T.CreateTruncating(BitMask);
111+
if (value > allBitsSet)
113112
{
114-
current = byte.CreateTruncating(value | OnesComplement0x7FU);
113+
current = byte.CreateTruncating(value | ~allBitsSet);
115114
value >>>= 7;
116115
}
117116
else

0 commit comments

Comments
 (0)