Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
IntegerEncoder.cs
Go to the documentation of this file.
2
3internal static class IntegerEncoder
4{
5 public static bool Encode(int value, int numBits, Span<byte> destination, out int bytesWritten)
6 {
7 if (destination.Length == 0)
8 {
9 bytesWritten = 0;
10 return false;
11 }
12 destination[0] &= MaskHigh(8 - numBits);
13 if (value < (1 << numBits) - 1)
14 {
15 destination[0] |= (byte)value;
16 bytesWritten = 1;
17 return true;
18 }
19 destination[0] |= (byte)((1 << numBits) - 1);
20 if (1 == destination.Length)
21 {
22 bytesWritten = 0;
23 return false;
24 }
25 value -= (1 << numBits) - 1;
26 int num = 1;
27 while (value >= 128)
28 {
29 destination[num++] = (byte)(value % 128 + 128);
30 if (num >= destination.Length)
31 {
32 bytesWritten = 0;
33 return false;
34 }
35 value /= 128;
36 }
37 destination[num++] = (byte)value;
38 bytesWritten = num;
39 return true;
40 }
41
42 private static byte MaskHigh(int n)
43 {
44 return (byte)(-128 >> n - 1);
45 }
46}
static bool Encode(int value, int numBits, Span< byte > destination, out int bytesWritten)