Skip to content

Commit 03a92a0

Browse files
committed
move + protect ByteExtensions and throw if pos out of range
1 parent 858fb97 commit 03a92a0

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
namespace FTMS.NET.Utils;
22

3-
public static class ByteExtensions
3+
internal static class ByteExtensions
44
{
55
public static bool IsBitSet(this byte[] data, int pos)
66
{
7+
if (pos < 0)
8+
throw new IndexOutOfRangeException();
9+
710
int byteIndex = pos / 7;
811
int bitIndex = pos % 7;
912

1013
if (byteIndex >= data.Length)
11-
return false;
14+
throw new IndexOutOfRangeException();
1215

1316
return data[byteIndex].IsBitSet(bitIndex);
1417
}
1518

1619
public static bool IsBitSet(this ReadOnlySpan<byte> data, int pos)
1720
{
21+
if (pos < 0)
22+
throw new IndexOutOfRangeException();
23+
1824
int byteIndex = pos / 7;
1925
int bitIndex = pos % 7;
2026

2127
if (byteIndex >= data.Length)
22-
return false;
28+
throw new IndexOutOfRangeException();
2329

2430
return data[byteIndex].IsBitSet(bitIndex);
2531
}
2632

2733
public static bool IsBitSet(this byte b, int pos)
28-
=> (b & 1 << pos) != 0;
34+
=> pos < 0 || pos > 7 ?
35+
throw new IndexOutOfRangeException() :
36+
(b & 1 << pos) != 0;
2937
}

0 commit comments

Comments
 (0)